In website operation, we often need to dynamically display the current year, date, or time on the page, such as the copyright information in the footer, the publication or update time of articles, etc.This information, if updated manually, not only takes time and effort, but is also prone to errors.It is fortunate that AnqiCMS provides a concise and efficient method for you to easily obtain and display these dynamic time information in the template.
Dynamically retrieve and display the current year or other time information
In AnqiCMS templates, to retrieve and display the current year or any format of the current time, we can use the built-in{% now %}Label. This label is very direct and it will output the current date and time of the server according to the specified format.
For example, if you want to display the current year at the footer of the website, you can use it like this:
© {% now "2006" %} 您的网站名称。保留所有权利。
Here are the"2006"This is not an arbitrary year, but a special reference format for date definition in Golang. If you want to display the complete current date, for example2023-10-26If it needs to be written like this:
今天日期:{% now "2006-01-02" %}
If you need to display more detailed time information, for example2023-10-26 15:30:00:
当前时间:{% now "2006-01-02 15:04:05" %}
{% now %}The flexibility of the label lies in its ability to output any current time representation you want, based on the Golang time format string you provide.
Format existing timestamps
除了显示当前时间,更常见的需求是格式化网站内容(如文章、产品)中已存储的时间戳。AnqiCMS 在文档详情、列表等标签中,会提供CreatedTimeandUpdatedTimeThis timestamp field. These fields are typically 10-digit Unix timestamps, and we can use them to display in a more readable format for users.{{ stampToDate(时间戳, "格式") }}This template function.
For example, on the detail page of a blog post, you may want to display its publish date and time:
发布于:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}
This will convertarchive.CreatedTimethis timestamp to2023年10月26日 15:30This format. Similarly, if you want to display the update time:
最后更新:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05") }}
Deep understanding of Golang time formatting rules
AnqiCMS template date and time formatting follows the specific rules of the Golang language. This is consistent with what we usually useYYYY-MM-DDThe pattern is different, and it may feel a bit special for the first time of contact.
Golang uses a fixed reference time.2006-01-02 15:04:05.999999999 -0700 MSTDefine the time format. You need to replace the parts of the reference time with the format you want, rather than using placeholders.
Here are some common reference mappings:
- Year:
2006-YYYY(Four-digit year) - Month:
01-MM(Two-digit month, fill with zeros if less) OrJan(English abbreviation) OrJanuary(Full English name) - Date:
02-DD(Two-digit date, fill with zeros if less) - Hour:
15-HH(24-hour format) or03(12-hour format) Or3(12-hour format without leading zeros) - minutes:
04-MM(Two-digit minutes, pad with zeros if necessary) - seconds:
05-SS(Two-digit seconds, pad with zeros if necessary) - Weekday:
Mon(English abbreviation) OrMonday(Full English name) - AM/PM:
PM(With3Hour system combined use)
With this mapping relationship, you can combine various time formats very flexibly. For example:
"2006/01/02"-2023/10/26"01-02"-10-26(Only shows month and day)"15:04"-15:30(Only shows hour and minute)"Monday, Jan 2, 2006"-Thursday, Oct 26, 2023(English weekday, month)
Actual application scenarios
- Footer copyright informationEnsure that the template file is updated automatically every year without manual modification.
<p>© {% now "2006" %} {{ system.SiteName }} 版权所有。</p> - Timestamp on the article or product detail pageClearly display the release or latest update date of the content to enhance user experience and the timeliness of the content.
<p>发布时间:{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}</p> {% if archive.UpdatedTime > archive.CreatedTime %} <p>最后更新:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04") }}</p> {% endif %} - Any display that requires dynamic date informationFor example, display the start date of the activity, system running time, etc.
Pass{% now %}Tags and{{ stampToDate() }}Function, combined with Golang's flexible time formatting rules, AnqiCMS makes the display of dynamic time information simple and powerful.You can easily implement displaying the copyright year at every corner of the website, as well as the precise content publication time down to the second, and ensure that this information is always up-to-date and accurate.
Common Questions (FAQ)
1. Why do I only show a few tags instead of all the tags I expected after using the tag?{% now "YYYY" %}Can't display the current year?
This is because AnqiCMS follows the Golang language rules for date formatting, rather than the commonYYYY-MM-DDorYYYYPlaceholder pattern. In Golang, you need to use a fixed reference time2006-01-02 15:04:05to define your format. So, to display the current year, you should use{% now "2006" %}.
2.{{ stampToDate(item.CreatedTime, "...") }}Error occurred, what is the reason?
This error usually occurs, possiblyitem.CreatedTimeThe variable itself is not a valid timestamp (for example, it is null, a string type, or another non-numeric type), or the first parameter you are trying to pass is not a timestamp. Please ensurestampToDateThe first parameter of the function is a valid 10-digit or 13-digit Unix timestamp.
Can I display the English months or weekdays?
Of course. Golang supports multiple English representations for date formatting. For example, to display the English abbreviation of the month, you can useJanTo display the English abbreviation of the day of the week, you can useMondayorMon. For example:{{ stampToDate(archive.CreatedTime, "Monday, Jan 02, 2006") }}Will display similarThursday, Oct 26, 2023format.