During the operation of a website, the timing of effective content release or updates is crucial for improving user experience and the readability of content.If time information is presented in its original timestamp format, it is often difficult for ordinary users to understand.AnQiCMS provides an intuitive and flexible way to help you convert these digitized timestamps into easy-to-read date and time formats.
How AnQiCMS handles timestamps
AnQiCMS usually uses the standard Unix Timestamp format when storing content time information.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 the machine handles it efficiently, it is obviously not user-friendly to display it directly to the user.
To solve this problem, the AnQiCMS template engine is built-in with a powerful tag that allows you to easily format these timestamps on the front-end page.
Core function:stampToDateTag
AnQiCMS providedstampToDateA label specifically used to convert timestamps to readable date and time format. This label's usage is very concise:
{{stampToDate(时间戳变量, "格式化字符串")}}
The key is in the second parameter - 'formatted string'.Due to the template engine of AnQiCMS being based on the Go language, its time formatting follows the unique reference time of the Go language.This reference time is not what we are accustomed toYYYY-MM-DDInstead of such placeholders, it is a specific date2006年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 actual application scenarios
After understanding the formatting rules, let's see how to apply them in a templatestampToDate.
Display the publication and update time of the article
In AnQiCMS, the article's
CreatedTime(creation time) andUpdatedTime(Updated time) are stored in timestamp format. Suppose you are writing an article detail page or a list page, and you need to display these times.Show only 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 (for example: 2023/01/15)
发布日期:{{stampToDate(item.CreatedTime, "2006年01月02日")}}
Display the comment posting time
In the comment system
CreatedTimeIt is also a timestamp and can be formatted in the same way:评论时间:{{stampToDate(comment.CreatedTime, "2006-01-02 15:04")}}Get and format the current server time
In addition to storing formatted timestamps in the database, if you need to retrieve and display the current server time, AnQiCMS also provides
{% now "格式化字符串" %}tags. Its formatting rules are the same asstampToDateThe formatting string is completely consistent.当前时间:{% now "2006年01月02日 15:04:05" %}
Notes and tips
- The accuracy of the timestamp:
stampToDateThe tag expects to receive a 10-digit Unix timestamp (second level).If the timestamp is 13 digits (millisecond level), it usually needs to be divided by 1000 to convert it to seconds, or the data processing level of AnQiCMS background has already made adaptation. - The formatting rules of Go language are the core:This is the easiest place to confuse. 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/YYYYit is written as01/02/2006. - with
dateThe difference between filters:AnQiCMS template still has one moredatefor example, a filter (such as{{ someDate|date:"2006-01-02" }}It is usually used for parsing into Go language alreadytime.TimeThe date object of the structure, not the raw timestamp. When processing timestamps directly taken from the database,stampToDateit is a more convenient choice.
BystampToDateLabel, AnQiCMS makes the time display on the website clear and flexible, greatly improving the efficiency of your content management and the reading experience of users.
Frequently Asked Questions (FAQ)
Q1: I want to display relative time like '5 minutes ago', can AnQiCMS do thatstampToDateCan the tag do that?A1:stampToDateThe tag is mainly used to format timestamps into specific date and time strings, such as “2023-01-15 10:30”.If you need to display relative time such as "5 minutes ago", AnQiCMS template tags currently do not provide this feature.You usually need to use a frontend JavaScript library (such asmoment.jsorday.jsTo calculate and dynamically display relative time, or to implement secondary development on the AnQiCMS backend.
Q2: If my timestamp is 13 digits (millisecond level),stampToDateCan the tag still be used?A2:stampToDateThe tag is expected to receive a 10-digit second-level Unix timestamp by default.If the timestamp you have is 13 digits in milliseconds, 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 to convert it to a 10-digit second-level timestamp before passing the data to the template. Usually AnQiCMS handles it internally and adapts, but if problems arise, checking the number of digits in the timestamp is one of the troubleshooting directions.
Q3: Why is the time formatting string in Go language2006-01-02 15:04:05Instead of likeY-m-d H:i:slike this placeholder?A3: Go language uses a very unique and intuitive time formatting method: it provides a fixed reference time (2006年01月02日 15时04分05秒) You only need to replace the desired output time format according to the numbers and symbols in this reference date. For example, if you want to display a four-digit year, write 2006If you want to display two-digit months, write01This way, the complexity of memorizing a large number of different placeholders is avoided, as all possible formats are defined through a specific date template.