How to output the current date and time in the AnQiCMS template and specify the format?

Calendar 👁️ 64

Displaying the date and time in the AnQiCMS template, formatted in a specific way, is a common requirement in website content operation.Whether it is to display the publication time of the article, the deadline of the activity, or show the current year in the footer, accurate and beautiful time information can enhance the user experience.AnQiCMS provides concise and efficient template tags, allowing you to easily implement these features.

Next, we will discuss in detail how to output the current date and time in the AnQiCMS template and specify the format you need.


1. UsestampToDateTag format timestamp

In website content management, the creation time, update time, and so on of articles are usually stored in the database in the form of timestamps.stampToDateLabels are designed to make it convenient to convert these timestamps into readable date and time formats.

When to use:When you need to display the date and time obtained from the database, for example, in timestamp formatarchive.CreatedTime(article publication time) orarchive.UpdatedTime(article update time) when,stampToDatetags are your first choice.

Syntax structure:

{{stampToDate(时间戳, "格式")}}

The core here is two parameters:

  1. Timestamp:A 10-digit Unix timestamp (second level). Provided internally by AnQiCMS.CreatedTimeandUpdatedTimeThe field is default 10-digit timestamp.
  2. Format:This is a string that defines the style of date and time output you wish. It is particularly important to note that the formatting rules of AnQiCMS (developed based on Go language) are different from those commonly used in PHP or Java et al.Y-m-dorYYYY-MM-DDPlaceholders differ, it adoptsFixed reference layout timeTo define the format. This reference layout is:2006年01月02日 15时04分05秒. You need to match the date and time format you expect with this reference layout.

Common format examples:

  • Show the year: "2006"
  • Show the month: "01"
  • Show the date: "02"
  • Display hour (24-hour format): "15"
  • Display minutes: "04"
  • Display seconds: "05"
  • Display year-month-date: "2006-01-02"
  • Display year/month/date: "2006/01/02"
  • Show year.month.day: "2006.01.02"
  • Show Chinese year-month-day: "2006年01月02日"
  • Show hour:minute: "15:04"
  • Show full date and time: "2006-01-02 15:04:05"

Code application example:

Assuming you are on the article detail page(archivethe object is available), you need to display the publication and update time of the article:

<p>
    发布时间:{{stampToDate(archive.CreatedTime, "2006年01月02日 星期一 15:04:05")}}
</p>
<p>
    最近更新:{{stampToDate(archive.UpdatedTime, "2006-01-02 15:04")}}
</p>
<p>
    文章年份:{{stampToDate(archive.CreatedTime, "2006")}}
</p>

This code will format the creation time of the article to include the day of the week in detail, format the update time to year-month-date hour:minute, and extract the published year separately.


Second, usenowTag to get the current system time

In addition to formatting the timestamp, you may also need to display the current system date and time directly in the template.For example, display the current year in the footer of the website or show the current time on some special pages.nowThe tag can meet this requirement.

When to use:When you need to display the current server date and time instead of the specific content time obtained from the database,nowtags are very convenient.

Syntax structure:

{% now "格式" %}

The "format" parameter is withstampToDateThe labels are completely the same, and they follow the Golang reference layout time formatting rules.

Code application example:

Assuming you want to display the current year and the complete date and time when the page was generated in the footer of the website:

<footer>
    <p>版权所有 &copy; {% now "2006" %} 您的公司名称。保留所有权利。</p>
    <p>页面生成于:{% now "2006-01-02 15:04:05" %}</p>
</footer>

This code will display the current year (e.g., "2023") and the exact date and time of page loading in the footer.


BystampToDateandnowThese tags, AnQiCMS provides powerful and flexible support for date and time output in templates.Mastering their usage and the unique formatting rules of Golang, you can easily display date and time information that meets various needs on the website.


Frequently Asked Questions (FAQ)

Q1:stampToDateThe "format" parameter looks like a date, I can write it directlyYYYY-MM-DD?

A1: It is not allowed. AnQiCMS is developed based on Go language, its time formatting mechanism is quite special, not usingYYYY-MM-DDSuch a placeholder. It requires you to use a fixed reference date and time2006年01月02日 15时04分05秒To define the format you want. Simply put, if you want to display "year-month-day", you need to write it as"2006-01-02"If you want to display "hour:minute", it should be written as"15:04". You need to match the output format you expect with the corresponding part of this reference time.

Q2: I tried using it in the template.{{ item.CreatedTime|date:"2006-01-02" }}Format the article creation time, but the page reported an error, why is that?

A2: dateThe filter expects the input to be in Go language in the AnQiCMS templatetime.TimeThe type of object. However, in the AnQiCMS templateitem.CreatedTimeis usually a Unix timestamp (integer). Therefore, it is used directly on the timestamp.dateThe filter will cause a type mismatch error. For timestamp variables, you should use specialstampToDatetags for formatting, such as{{stampToDate(item.CreatedTime, "2006-01-02")}}.

Q3:stampToDateWhat is the timestamp in the label, 10 digits or 13 digits? What if I get a 13-digit timestamp?

A3: stampToDateThe label mainly supports10 digitsThe Unix timestamp (seconds since January 1, 1970). It is used internally by the AnQiCMS system.CreatedTimeandUpdatedTimeThe fields also provide a 10-digit timestamp by default. If you receive a 13-digit millisecond timestamp, you need to divide it by 1000 to convert it to seconds (for example, in the backend controller), then you canstampToDatein the tag.

Related articles

What role does the `extends` tag play in the AnQiCMS template inheritance system?

In AnQiCMS template development, the `extends` tag plays a core role, it is the key to building efficient, maintainable and structurally unified website templates.The `extends` tag can be understood as creating a bridge between 'master' and 'child pages', allowing you to easily define a common layout skeleton for the entire website without repeating a lot of the same code on each page.

2025-11-08

How does the `macro` function in AnQiCMS templates help me reduce redundant rendering logic?

In Anqi CMS template development, improving efficiency and maintaining code cleanliness is the goal that every developer is pursuing.web pages often contain many structurally similar but content-wise different areas, such as each article card in the article list, the various features of a product detail page, or each link item in the navigation menu.If each time you have to rewrite this similar rendering logic, it not only takes time and effort, but also, once you need to make modifications, you may face the dilemma of having to make repetitive changes in multiple places.Luckyly, AnQiCMS's powerful template system provides `macro` macro function

2025-11-08

How to pass specific variables or data when including a sub-template?

Advanced AnQiCMS Template: `include` Sub-template Data Passing Techniques and Practices In AnQiCMS template development, the `include` tag is undoubtedly a powerful tool for enhancing template reusability and modularization.It allows us to extract common code snippets (such as page headers, footers, sidebars, etc.) and then introduce them where needed, thus avoiding repetition and making the template structure clearer and maintenance more efficient.However, the sub-templates introduced often need to display different content, which raises a core question: how to introduce sub-templates at the time

2025-11-08

How to use the `include` tag to reuse header, footer, and other common code segments in AnQiCMS templates?

Building website templates in AnQiCMS, efficient code reuse is the key to improving development speed and maintenance efficiency.Imagine, the header (Header) and footer (Footer) of a website appear almost on every page. If the same code is written on each page, it is not only time-consuming, but also, once a modification is needed, it has to be searched and updated on each page, which is undoubtedly a huge amount of work.

2025-11-08

How to output variables in a template and safely escape content to prevent XSS attacks?

In the process of managing and displaying website content, ensuring the security of user data is a crucial link.It is an issue that every website operator needs to pay attention to when the website needs to display user submitted content or data obtained from external sources, how to effectively prevent cross-site scripting (XSS) attacks.AnQiCMS (AnQiCMS) took this into full consideration from the very beginning, providing solid security guarantees for content output through its powerful template engine and flexible filter mechanism.### Cross-Site Scripting Attack

2025-11-08

How to display the latest N articles or products on the homepage and implement pagination control?

## Asecurity CMS: The Practice of Efficiently Displaying the Latest Content on the Homepage and Implementing Pagination The homepage is an important entry point for visitors to understand the site content and obtain the latest information, it is crucial to clearly and effectively display the latest published articles or products.The AnQi CMS provides powerful and flexible template tags, which help us easily achieve this goal, and can also fine-tune content pagination to ensure a smooth user experience.

2025-11-08

How to filter and loop through the document list under a specific category based on the category ID?

It is crucial to organize and present content when building and operating a website.Especially when the content of a website becomes increasingly rich, how to enable visitors to quickly find the information they are interested in and clearly browse all articles under a specific theme has become a carefully designed problem.AnQiCMS (AnQiCMS) provides powerful and flexible content management capabilities, allowing us to easily achieve precise control and display of document lists.

2025-11-08

How to exclude specific categories or multiple categories of articles from the document list?

When managing website content in Anqi CMS, we often need to precisely control the display of articles.Sometimes, we may want certain category articles not to appear in the regular article list, such as for internal notifications, test content, or some promotional information that is only displayed on specific pages.AnQi CMS provides a simple and efficient method to meet this kind of need, allowing you to flexibly exclude articles of specific categories or multiple categories, thereby achieving more accurate content presentation.

2025-11-08