In the template design of AnQi CMS, we often encounter situations where we need to display timestamp data in a human-readable date format.These data are usually stored in the database in the form of timestamps, whether it is the publication time of the article, the update date of the content, or the records of user activities.To ensure that website visitors can clearly and intuitively understand this information, it is particularly important to master how to convert timestamps into the specified date format in the template.
Timestamp in AnQiCMS: Where does the data come from?
In AnQiCMS, you will find that many data fields related to time are in the form of timestamps, such as article detail tags (archiveDetail) and article list tags (archiveList) within theCreatedTime(Creation time) andUpdatedTime(Updated time), as well as the comment list tag (commentList) within theCreatedTime, and the user details tag (userDetail) within theLastLogin) and (last login time) andExpireTime(VIP expiration time) etc.These timestamps are usually a 10-digit integer, representing the number of seconds elapsed since the Unix epoch (00:00:00 UTC on January 1, 1970).
Directly displaying these 10-digit numbers has no meaning to ordinary users, therefore we need to convert them into easily understandable formats such as
Core Function:stampToDateTags Take the Stage
AnQiCMS为此提供了一个专门的模板标签——EnglishstampToDateIt can easily convert timestamps to the date format you want.This label's usage is very intuitive, only two parameters are needed: the timestamp you want to convert, and a string defining the output format.
The basic syntax is:{{stampToDate(您的时间戳变量, "您的格式字符串")}}.
理解这两个参数是成功转换的关键。
Your timestamp variable:这里通常是您通过其他标签(如English)
archiveListorarchiveDetail)Obtained data field, for exampleitem.CreatedTimeorarchive.UpdatedTime. Please note that it must be a10-digitUnix timestamp (second-level).Your format stringThis is the 'magic incantation' that determines the date output format.AnQiCMS is developed based on Go language, therefore the format strings here follow the unique date formatting standards of Go language.
2006年01月02日 15时04分05秒 -0700 MST(or simply written as2006-01-02 15:04:05) as the template for formatting. You need to replace each component of this reference date with the actual value you want to display for the corresponding date.For example:
2006Represents the year (four digits)01Represents the month (two digits, padded with zero if necessary)02Represents the date (two digits, padded with zero if necessary)15Represents the hour (24-hour format, two digits, padded with zero if necessary)04Represents minutes (two-digit number, padded with zero if less))05Represents seconds (two-digit number, padded with zero if less)
By combining these 'magic numbers', you can create a variety of date formats.
How to usestampToDatetags
Let us understand how to apply AnQiCMS template through several specific examplesstampToDateTags. Suppose we have a timestamp1678886400which corresponds to2023-03-15 00:00:00.
displayed as “Year-Month-Day”:
<p>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</p> {# 输出: 发布日期:2023-03-15 #}Displayed as “Year/Month/Day Hour:Minute:Second”:
<p>更新时间:{{stampToDate(archive.UpdatedTime, "2006年01月02日 15时04分05秒")}}</p> {# 输出: 更新时间:2023年03月15日 00时00分00秒 #}Displayed as “Month/Day Hour:Minute”:
<p>登录时间:{{stampToDate(user.LastLogin, "01/02 15:04")}}</p> {# 输出: 登录时间:03/15 00:00 #}Displayed as pure year:
<p>版权年份:{{stampToDate(someTimestamp, "2006")}}</p> {# 输出: 版权年份:2023 #}
Example of common application scenarios
tostampToDateLabel integration into the actual template can greatly enhance the professionalism of information presentation.
Display the article publication date on the article list pageWhen you use
archiveListTags loop outputting multiple articles can display the publication time of each article like this:{% archiveList archives with type="page" limit="10" %} {% for item in archives %} <div> <h3><a href="{{item.Link}}">{{item.Title}}</a></h3> <p>发布于:<span>{{stampToDate(item.CreatedTime, "2006年01月02日")}}</span></p> <p>{{item.Description}}</p> </div> {% endfor %} {% endarchiveList %}The update time of the content displayed on the article detail pageIn the article detail page, you may need to display the creation time and the latest update time of the article to help readers understand the timeliness of the content:
<article> <h1>{% archiveDetail with name="Title" %}</h1> <p> <span>发布日期:{{stampToDate(archive.CreatedTime, "2006-01-02")}}</span> <span>最后更新:{{stampToDate(archive.UpdatedTime, "2006-01-02 15:04")}}</span> </p> <div class="content"> {%- archiveDetail articleContent with name="Content" %} {{articleContent|safe}} </div> </article>Here we assume
archiveis the data object of the current article detail.Display user-related time information: If your website has a user center, you may need to display the VIP expiration time of the user:
{% userDetail user with name="Id" id="当前用户ID" %} <p>您的VIP将于:<span>{{stampToDate(user.ExpireTime, "2006年01月02日")}}</span> 到期。</p> {% enduserDetail %}Certainly, when in actual use, you need to obtain according to the actual page context.
user.ExpireTimeThis timestamp variable.
A small reminder:dateFilter is related tostampToDatedifferences
In the template system of AnQiCMS, in addition tostampToDateLabel, you may also see a filter nameddate. Here, special attention needs to be paid todatefilters are natively supported by the Go language template engine, and they expect to handle Go language primitivestime.TimeType (a date-time object), not the 10-digit Unix timestamp (integer) we are discussing here.
If you try to pass a timestamp directly todateFilter, an error is likely to occur during template parsing.stampToDatetags