In AnQiCMS template development, we often encounter the need to convert the timestamp data stored in the background into a clear and readable date and time format on the user interface. For example, the original timestamp, such as1609470335This has no meaning for ordinary users. Appropriately displaying time information is particularly important to enhance the user experience of the website.It's lucky that AnQiCMS provides a very practical built-in filter-stampToDateIt can help us easily achieve this goal.
Core function:stampToDateIntroduction to filters
stampToDateThe filter is a tool specifically used for timestamp formatting in the AnQiCMS template engine.Its main function is to convert a series of digital timestamps into a date and time string that is easy to understand according to the specified format.No matter whether you want to display the publication date of the article, the update time of the product, or any other information based on a timestamp,stampToDateAll of them can provide flexible and powerful support.
Master the usage method: easily format timestamps
stampToDateThe filter usage is very intuitive, it requires two key parameters:
Timestamp (Timestamp)This is usually a 10-digit integer representing the number of seconds from the Unix epoch (January 1, 1970, 00:00:00 UTC) to the present. In AnQiCMS templates, this timestamp usually comes from database fields such as article objects
CreatedTimeorUpdatedTime.Format String (Format String)This is the key to determining the date and time display style. Since AnQiCMS is developed based on the Go language, the format string here follows the unique style of Go language.
参考时间Rule. The reference time for the Go language is:2006-01-02 15:04:05 -0700 MST.To understand this format string, you need to treat Go's reference time as a template.What you want to display in the final output, use the corresponding part in the reference time to represent. For example:
2006Represents the year01Represents the month02Represents the date15representing hours (24-hour system)04representing minutes05representing seconds
By analogy, if you want to display "year-month-day", you can use:
"2006-01-02"as the format string.
The basic syntax structure is:{{ stampToDate(时间戳变量, "格式字符串") }}
Let's go through an example timestamp:1609470335(Indicating2021-01-01 00:25:35) to demonstrate:
Useful Case: Convert Timestamp to Multiple Date Formats
Suppose we have a timestamp variablepublishStampIts value is1609470335.
Only display the date (year-month-date): If you only need to display the specific year, month, and day, you can use
"2006-01-02"as the format string.<div>发布日期:{{ stampToDate(publishStamp, "2006-01-02") }}</div> {# 输出: 发布日期:2021-01-01 #}Display Chinese Date FormatIf you want the date to be displayed in Chinese, just add Chinese characters to the format string.
<div>发布日期:{{ stampToDate(publishStamp, "2006年01月02日") }}</div> {# 输出: 发布日期:2021年01月01日 #}Display the full date and time To display minutes and even seconds, you can add reference numbers for hours, minutes, and seconds.
<div>发布时间:{{ stampToDate(publishStamp, "2006-01-02 15:04:05") }}</div> {# 输出: 发布时间:2021-01-01 00:25:35 #}Custom delimiter: You can change the separator between year, month, and day according to your needs, for example, using a slash.
<div>日期:{{ stampToDate(publishStamp, "2006/01/02") }}</div> {# 输出: 日期:2021/01/01 #}Only show the time: If you only want to display the time part, you can combine it like this.
<div>精确到分钟:{{ stampToDate(publishStamp, "15:04") }}</div> {# 输出: 精确到分钟:00:25 #}
In the AnQiCMS template, we often obtainarchiveListorarchiveDetailthe timestamp from the article object obtained by the tag. For example, the creation time of an article can be obtained throughitem.CreatedTimeorarchive.CreatedTimeGet it.
{# 在文章列表中,显示每篇文章的创建时间 #}
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<div>
<h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
<p>发布于:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}</p>
</div>
{% endfor %}
{% endarchiveList %}
{# 在文章详情页,显示文章的更新时间 #}
{% archiveDetail archive with name="CreatedTime" %}
<p>最近更新:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04") }}</p>
{% endarchiveDetail %}
Advanced Techniques: Origin and Application of Timestamps
Many data models in AnQiCMS include timestamp fields, such as:
- Document (Archive):
CreatedTime(creation time),UpdatedTime(Updated Time) - Category: Usually does not directly contain a timestamp, but can obtain the time of the latest article below.
- Tag: Similarly, it does not directly contain a timestamp.
When you are usingarchiveList/archiveDetailWhen looping or fetching content with tags, these timestamp fields will be returned along with the data. You just need to use them asstampToDatethe first parameter.
{# 假设在一个循环中 item 代表一个文章对象 #}
<span>{{ stampToDate(item.CreatedTime, "2006/01/02") }}</span>
This ensures that regardless of where the data comes from, as long as it is a standard Unix timestamp,stampToDateit can be formatted uniformly and accurately.
Summary
stampToDateThe filter is a key tool in AnQiCMS templates for handling timestamps, converting complex numbers into user-friendly date and time expressions. By understanding the reference time template in Go language.2006-01-02 15:04:05 -0700 MSTYou can flexibly customize any required date and time format. Mastering this filter will significantly enhance the readability and professionalism of your website content.
Frequently Asked Questions (FAQ)
Ask: Why is my time formatted output0001-01-01or1970-01-01?Answer: This usually means that you have providedstampToDateThe filter timestamp is an invalid value (such as 0, null, or an empty string) or not obtained correctly. In Unix timestamp, 0 represents January 1, 1970, and in Go language,time.TimeThe zero value is January 1, 0001. Please check if the timestamp variable you passed is empty or contains the correct 1