Managing website content in AnQiCMS, we often encounter situations where we need to display dates and times, such as the publication time of articles, update time, or the last login time of users, etc. These time data are usually stored in GoLang backend.time.TimeType storage. When they are passed to the front-end template, if not properly formatted, they may be displayed as a string of hard-to-understand timestamps or the default Go language format, which clearly does not meet the needs of the visitors we show.
Fortunately, AnQiCMS provides us with powerful and flexible template tags and filters, making it easy to use these GoLang'stime.TimeVariable is formatted into any date string format we want.
Core tool:stampToDateFormatting tag
AnQiCMS's template engine supports syntax similar to Django, where there is a very practical tag specifically used for formatting timestamps (or, those numbers representing time passed from the backend). This tag isstampToDate.
The basic usage is:{{ stampToDate(时间戳变量, "格式化字符串") }}.
The key is in the second parameter: "format string".AnQiCMS uses GoLang's unique 'reference time' to define the format.This reference time is a fixed date:2006-01-02 15:04:05.999999999 -0700 MST. You do not need to remember this long string, just understand what each part represents:
2006: Represents the year (YYYY)01: Represents the month (MM), for example01Represents January02: Represents the date (DD), for example02represents the second day of every month15: represents hours (24-hour clock,HH), for example15represents 3 PM04: represents minutes (mm), for example04represents 4 minutes05seconds meanss), for example05means 5 seconds
This means that if you want to format the time in the form of "year-month-date", you just need to write in the formatting string"2006-01-02"You can. If you want to display "Month/Day/Year Time:Minute", you can write it as"01/02/2006 15:04".
Let's look at some examples to understand its usage:
- Display date and time:
{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04:05") }}May be displayed as2023-10-27 10:30:45. - Only show the date:
{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}May be displayed as2023年10月27日. - US date format:
{{ stampToDate(archive.CreatedTime, "Jan 02, 2006") }}May be displayed asOct 27, 2023. - Show time (hour:minute):
{{ stampToDate(archive.CreatedTime, "15:04") }}May be displayed as10:30.
Practical application: Obtain time from documents and user data
In AnQiCMS, many places involve time data. For example, when displaying the details of an article, we can usearchive.CreatedTimeandarchive.UpdatedTimeTo get the creation and update time. These variables are usually provided in the form of timestamps and can be passed directlystampToDateFormat labels.
Suppose we are editing the template of the article detail page, we can display the publication and update time of the article like this:
<p>发布于:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}</p>
<p>最后更新:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04") }}</p>
In addition to articles, the user module also contains time information, such as the user's last login timeuser.LastLoginor the VIP expiration timeuser.ExpireTime. You can also usestampToDateto format this data:
<p>上次登录时间:{{ stampToDate(user.LastLogin, "2006-01-02") }}</p>
<p>VIP 过期日期:{{ stampToDate(user.ExpireTime, "2006年01月02日") }}</p>
If you need to display the current time or year, AnQiCMS also provides{% now %}tags, its usage is similar tostampToDateThe formatting part is similar to:
<p>版权所有 © {% now "2006" %}</p>
<p>当前时间:{% now "2006-01-02 15:04:05" %}</p>
Be careful to distinguish:stampToDatewithdateFilter
In the AnQiCMS template, you may also see a filter nameddate. This filter can also format time, but it has an important distinction withstampToDate.
stampToDate: Specifically used for processing timestamp(通常是 10 位或 13 位的数字,代表从 Unix 纪元开始的秒数或毫秒数)。This is the method most commonly used in the AnQiCMS template to format dates, as the backend will convert the date field in the database to a timestamp and pass it to the frontend.dateFilter: Used for formatting the native GoLangtime.Timeobject. If the template variable itself is a GoLangtime.TimeType (not a simple numeric timestamp), then you can use{{ 变量 | date:"格式化字符串" }}such as this.
Important reminder:Most of the time, when we get data tags from AnQiCMS (such asarchiveList/archiveDetailand so on)CreatedTimeorUpdatedTimeWhen time fields are equal, they are provided in the form timestampTherefore, you should prioritize usingstampToDatetags. If the timestamp is mistakenly passed todatethe filter, an error may occur during template parsing.
By flexible applicationstampToDateTags, combined with GoLang's precise formatting strings, you can elegantly display any date and time information on the AnQiCMS website, making your website content more readable and professional.
Frequently Asked Questions (FAQ)
1. WhystampToDateThe format string looks so strange, using a combination like '2006-01-02 15:04:05' instead of 'YYYY-MM-DD'??
This is the date and time formatting method unique to the GoLang language, which uses a fixed 'reference time'2006-01-02 15:04:05.999999999 -0700 MSTServe as a format pattern. You do not need to remember the specific date, just understand the meaning of each number in this reference time:2006represents the year,01represents the month,02represents the date,15represents the hour (24-hour format),04represents the minute,05representing seconds