It is crucial to effectively display the time of content publication or updates during website operation, as it is essential for improving user experience and the readability of the content.If time information is presented in its original timestamp format, it is often difficult for ordinary users to understand.AnQiCMS provides a straightforward and flexible way to help you convert these digitized timestamps into easily readable date and time formats.
How AnQiCMS handles timestamps
AnQiCMS In storing the time information of the content, it usually adopts the standard timestamp (Unix Timestamp) format.This format is a 10-digit or 13-digit integer, representing the number of seconds or milliseconds elapsed since 00:00:00 UTC on January 1, 1970.Although machine processing is very efficient, directly displaying it to the user is obviously not user-friendly.
To solve this problem, AnQiCMS's template engine is built-in with a powerful tag that allows you to easily format these timestamps on the front-end page.
Core Function:stampToDatetags
AnQiCMS providesstampToDateLabels, used specifically to convert timestamps into readable date and time formats. The usage of this label is very concise:
{{stampToDate(时间戳变量, "格式化字符串")}}
The key here lies in the second parameter—"format string".Since AnQiCMS's template engine is based on Go language, its time formatting follows the unique reference time of Go language.YYYY-MM-DDInstead of such placeholders, it is specific dates.2006年01月02日 15时04分05秒You need to replace the time format you want to display according to the year, month, day, hour, minute, and second positions in this reference date.
For example, if you want to display:
- Year: use
2006 - Month: use
01 - Date: Use
02 - Hour (24-hour format): Use
15 - Minute: Use
04 - Second: Use
05
Examples of practical application scenarios
After understanding the formatting rules, let's see how to apply them in actual templatesstampToDateLabel.
Display article publishing and update time
In AnQiCMS, articles
CreatedTime(Creation time) andUpdatedTimeThe update time is stored in timestamp format. Suppose you are writing a detail page or a list page of an article and need to display these times.Only show the date (e.g., 2023-01-15)
{# 假设 item 是当前文章对象 #} 发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}} 更新日期:{{stampToDate(item.UpdatedTime, "2006-01-02")}}Show the full date and time (e.g., 2023-01-15 10:30:45)
发布时间:{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}} 更新时间:{{stampToDate(item.UpdatedTime, "2006-01-02 15:04:05")}}Custom Chinese format (e.g., 2023-01-15)
发布日期:{{stampToDate(item.CreatedTime, "2006年01月02日")}}
Display the comment posting time
In the comment system
CreatedTimeSame as timestamp, it can also be formatted in the same way:评论时间:{{stampToDate(comment.CreatedTime, "2006-01-02 15:04")}}Get and format the current server time
Formatting timestamps stored in the database, if you need to retrieve and display the current server time, AnQiCMS also provides
{% now "格式化字符串" %}labels. Its formatting rules are the same asstampToDateThe format string is completely consistent.当前时间:{% now "2006年01月02日 15:04:05" %}
Notes and tips
- Timestamp accuracy:
stampToDateThe label expects to receive a 10-digit Unix timestamp (second precision).If your timestamp is 13 digits (millisecond level), it usually needs to be divided by 1000 to convert it to second level, or the data processing level of AnQiCMS background has already done the adaptation. - The formatting rules of the Go language are core:This is the most confusing place. Please remember it.
2006-01-02 15:04:05This reference date, then replace the corresponding numbers according to the output format you need. For example, if you want to displayMM/DD/YYYY,and it will be written as01/02/2006. - With
dateThe difference between filters:There is also a template in AnQiCMSdatefilters (for example{{ someDate|date:"2006-01-02" }}),但它通常用于已经解析成 Go 语言time.Time结构体的日期对象,而不是原始的时间戳。在处理从数据库直接取出的时间戳时,stampToDateIt is a more convenient choice.
PassstampToDateLabel, AnQiCMS makes the time display on the website clear and flexible, greatly enhancing the efficiency of content management and the reading experience of users.
Common Questions (FAQ)
Q1: I want to display relative time such as “5 minutes ago”, AnQiCMS'sstampToDateCan tags do that?A1:stampToDateLabels are mainly used to format timestamps into specific date and time strings, such as “2023-01-15 10:30”.If you need to display "5 minutes ago" such as relative time, the template tags of AnQiCMS currently do not provide this function directly.moment.jsorday.jsCalculate and dynamically display relative time, or conduct secondary development on the backend of AnQiCMS.
Q2: If my timestamp is 13 digits (millisecond level),stampToDateCan the tag still be used?A2:stampToDateLabel is expected to receive a 10-digit Unix timestamp in seconds by default.If your timestamp is a 13-digit millisecond, using it directly may cause the date to be incorrect.In this case, you need to ensure that the 13-digit timestamp is divided by 1000 before passing it to the template to convert it into a 10-digit second-level timestamp.通常 AnQiCMS 在内部处理时会进行适配,但如果遇到问题,检查时间戳的位数是排查方向之一。
Why is the time format string in Go language2006-01-02 15:04:05, rather than likeY-m-d H:i:slike this placeholder?A3: Go 语言采用了一种非常独特且直观的时间格式化方式:它提供了一个固定的参考时间 (2006年01月02日 15时04分05秒) You only need to replace the desired output time format with the numbers and symbols in this reference date. For example, if you want to display a four-digit year, write2006; If you want to display two-digit months, write01This method avoids the complexity of memorizing a large number of different placeholders, defining all possible formats through a specific date template.