How does the AnQiCMS template format timestamps and display them in a specified date and time format?
Easily format timestamps in AnQiCMS templates: Custom date and time display guide
In the operation of the website, the date information such as content release time, update time, and comment time is an important part of the information we convey to visitors.Most of the time, the time data obtained from the database is usually a timestamp (a string of numbers), which is not intuitive for users.How to convert these original timestamps into a readable date and time format, and flexibly adjust the display style according to the page design, is a common requirement in template development.
AnQiCMS provides very practical and flexible tools for templates to solve this problem, the most core of which isstampToDate.
Get to knowstampToDateTag: The 'magic wand' of timestamps
stampToDateThe tag is an AnQiCMS template engine built-in feature, specially designed to convert numeric timestamps into common date and time strings. Its usage is simple and straightforward:
{{stampToDate(时间戳, "格式")}}
The key is in two parts:
Timestamp:This is typically a 10-digit number representing the number of seconds elapsed since the Unix epoch (00:00:00 UTC on January 1, 1970). For example, in AnQiCMS documentation, comments, user data, etc.
item.CreatedTime(Creation time),item.UpdatedTime(Update time) etc. fields, all return this timestamp.Format:This is the key to how date and time are displayed. The template engine of AnQiCMS is based on Go language, and its time formatting method is very unique.It does not use the common ones such as
Y-m-d H:i:sInstead of such placeholders, use oneFixed reference time2006-01-02 15:04:05As a formatting template.Understanding this reference time is crucial:
- Year:using
2006Indicates - Month:using
01(Represents January) Indicates - Date:using
02(Representing two) indicates - Hour:using
15(24-hour system, representing 3 PM) indicates - Minute:using
04(Representing the fourth) indicates - Seconds:using
05(Representing five seconds) indicates - AM/PM:using
PMorAMIndicates (for example)3:04PM) - Weekday:using
Mon(Monday abbreviation) orMonday(Monday full name) indicates
When you want a certain date-time format, just spell out the desired format using the corresponding numbers or letters in the reference time.
- Year:using
Use case: Timestamp in AnQiCMS data
In the AnQiCMS template, you will often encounter situations where you need to format timestamps. Below, we will illustrate through several common examples:
1. The creation/update time in the document list or detail page
Whether it is an article, product, or other custom content model, their creation and update time are stored in timestamp form.
Example code:
{# 假设这是在一个文档列表的循环中,或者文档详情页直接获取 archive 对象 #}
<div>
<span>发布日期:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</span>
<span>更新时间:{{stampToDate(item.UpdatedTime, "2006-01-02 15:04")}}</span>
</div>
{# 如果是在文档详情页直接调用当前文档的时间 #}
<div>
<p>文章发布于:{{stampToDate(archive.CreatedTime, "2006/01/02")}}</p>
<p>最后更新于:{{stampToDate(archive.UpdatedTime, "01-02 15:04:05")}}</p>
</div>
2. Comment time in the comment list
The time when the user comments needs to be clearly displayed.
Example code:
{# 假设这是在一个评论列表的循环中 #}
{% for comment in comments %}
<div>
<p>{{comment.UserName}} 于 {{stampToDate(comment.CreatedTime, "2006年1月2日 15:04")}} 评论:</p>
<p>{{comment.Content}}</p>
</div>
{% endfor %}
3. Login and expiration time in user information
When displaying user personal information, such as the last login time or VIP membership expiration time, formatting is also important.
Example code:
{# 假设这是在一个用户详情页 #}
{% userDetail userInfo with name="LastLogin" id="1" %}
{% userDetail vipExpire with name="ExpireTime" id="1" %}
<div>
<p>上次登录:{{stampToDate(userInfo, "2006-01-02 15:04")}}</p>
<p>VIP 到期:{{stampToDate(vipExpire, "2006年01月02日")}}</p>
</div>
Flexible and diverse formatting examples.
Mastered the reference time formatting rules of the Go language, you can create various date and time display effects as you wish.
| The format you want to display | stampToDateformat string |
Sample output (assuming the timestamp is1609470335that is, January 1, 2021, 11:05:35 AM |
|---|---|---|
| Year-Month-Date | "2006-01-02" |
2021-01-01 |
| Year-Month-Day (in Chinese) | "2006年01月02日" |
2021年01月01日 |
| Month/Day/Year | "01/02/2006" |
01/01/2021 |
| Time:Minutes (24-hour format) | "15:04" |
11:05 |
| Time:Hour:Minute:Second (24-hour format) | "15:04:05" |
11:05:35 |
| Time:Hour:Minute AM/PM (12-hour format) | "3:04PM" |
11:05AM |
| Year-Month-Date Hour:Minute | "2006-01-02 15:04" |
2021-01-01 11:05 |
| Week, Day Month Year | "Mon, 02 Jan 2006" |
Fri, 01 Jan 2021 |
| Complete Date and Time (with timezone) | "2006-01-02T15:04:05Z07:00" |
2021-01-01T11:05:35Z+08:00 |
Tip:
- Go language reference time memorization method:There is a simple mnemonic to help remember the reference time of the Go language: 'On January 2, at 3:04:05, 2006'. It corresponds to
01(month),02(day),03(PM hour, also known as)15of a 24-hour format),04(minutes),05(seconds),2006(years). stampToDatewithdateThe difference between filters:There is also one in the AnQiCMS template.dateFilter ({{obj|date:"格式"}}Please note that.dateThe filter is only applicable to Go language primitives.time.TimeAn object of type, not a direct numeric timestamp. For the 10-digit timestamps commonly found in the AnQiCMS database, it is imperative to usestampToDateFormat labels.
Summary
stampToDateThe tag is the core tool for handling date and time display in the AnQiCMS template.By understanding its formatting mechanism based on the Go language reference time, you can easily convert the original timestamp into various easy-to-read and design-satisfying date and time formats, thereby enhancing the professionalism of the website's user experience and content presentation.Use this tag flexibly to make your website content more vivid and humanized.
Frequently Asked Questions (FAQ)
1. Why do I use it directly in the template{{item.CreatedTime}}but it shows a string of numbers instead of the date format I want?
This is becauseitem.CreatedTimeIn AnQiCMS, a standard 10-digit Unix timestamp is stored, which is a pure number. To display it in the common date and time format we are familiar with, you need to usestampToDateTag