As a professional familiar with AnQi CMS and proficient in content operation, I am well aware that the accuracy and beauty of time are crucial in the display of website content.A clear and readable timestamp, which not only improves user experience, but also reflects the standardization of data display.Below, I will give you a detailed introduction on how to format timestamps in the AnQiCMS template.
Detailed guide on timestamp formatting output in AnQiCMS template
AnQiCMS as an efficient and customizable content management system, its template engine supports syntax similar to Django, providing great flexibility for content creators and developers.When building dynamic pages, it is often necessary to display information such as the time of article publication, update time, and user login time.These times are usually stored in databases in the form of timestamps (Unix epoch seconds).To display this time information in a user-friendly format, AnQiCMS provides special template tags for formatting processing.
Core formatting tags:stampToDate
The core tag for formatting timestamp output in the AnQiCMS template system isstampToDate. This tag allows you to convert a 10-digit timestamp (usually called Unix timestamp or Unix epoch seconds) to a string in the date and time format you specify.
stampToDateThe basic usage of the tag is as follows:
{{ stampToDate(时间戳, "格式") }}
The "timestamp" here is the 10-digit number you need to format, and the "format" is the string that defines the output date and time style.
Understand the time formatting rules of Go language
The backend of AnQiCMS is developed based on Go language, thereforestampToDateThe "format" parameter in the tag strictly follows the Go language'stime.FormatThe special time formatting rule used by the function. Unlike many programming languages that useYYYY-MM-DDsuch placeholder, Go's formatting is based on a fixed reference time point:2006年1月2日15点04分05秒.
You need to replace each component of this reference time point with the corresponding format you want. The following are some commonly used reference time components and their meanings:
- year:
2006(full year, such as 2023) or06(two-digit year, such as 23) - month:
01(month with leading zero, such as 08) or1(month without leading zero, such as 8) orJan(Month abbreviation, such as Aug) orJanuary(Full month name, such as August) - Day:
02(With leading zero, such as 09) or2(Without leading zero, such as 9) - hour:
15(24-hour format, with leading zero, e.g. 14) or3(12-hour format, without leading zero, e.g. 2) or03(12-hour format, with leading zero, e.g. 02) - minute:
04(with leading zero for minutes, such as 07) - seconds:
05(with leading zero for seconds, such as 01) - AM/PM:
PMorpm(used for 12-hour format) - Week:
Mon(Abbreviation of the week, such as Fri) orMonday(Full name of the week, such as Friday) - Time Zone:
MST(Abbreviation of the time zone, such as CST) or-0700(Digital time zone offset, such as +0800)
Actual formatting example
Combine the formatting rules of the Go language, we can construct various common date and time output formats. Suppose we have a timestamp variablepublishStampIts value is1609470335(Corresponding to 2021-01-01 00:25:35 UTC+8), here are some formatting examples:
- Displayed as "2021-01-01":
<div>发布日期:{{ stampToDate(publishStamp, "2006年01月02日") }}</div> - Show as “2021-01-01”:“
<div>发布日期:{{ stampToDate(publishStamp, "2006-01-02") }}</div> - Show as “01/01/2021”:“
<div>发布日期:{{ stampToDate(publishStamp, "01/02/2006") }}</div> - Show as “2021-01-01 00:25”:“
<div>发布时间:{{ stampToDate(publishStamp, "2006-01-02 15:04") }}</div> - Shown as “2021-01-01 00:25:35”:
<div>发布时间:{{ stampToDate(publishStamp, "2006-01-02 15:04:05") }}</div> - Shown as “Friday, January 1, 2021”:
<div>发布时间:{{ stampToDate(publishStamp, "Monday, 2006年1月2日") }}</div> - Show only the month and date "01-01":
<div>发布月日:{{ stampToDate(publishStamp, "01-02") }}</div> - Show only the hour and minute "00:25":
<div>发布时分:{{ stampToDate(publishStamp, "15:04") }}</div>
Apply formatting in AnQiCMS tags of various types
In the AnQiCMS template, timestamps are usually used as properties of document, comment, user and other data objects. You can directly use these properties asstampToDatethe first parameter of the tag.
In document lists or details
When displaying article or product lists, we often need to show their creation or update time.archiveListandarchiveDetailThe object returned by the tag containsCreatedTimeandUpdatedTimeProperties, which are timestamps.
- In
archiveListIn:{% archiveList archives with type="list" limit="10" %} {% for item in archives %} <div> 文章标题:{{ item.Title }} 发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }} 更新时间:{{ stampToDate(item.UpdatedTime, "2006-01-02 15:04") }} </div> {% endfor %} {% endarchiveList %} - In
archiveDetailIn:
(Please note,{% archiveDetail currentArchive with name="Title" %} {# 获取当前文档标题,作为示例 #} <div> 当前文档标题:{{ currentArchive }} 创建时间:{{ stampToDate(archive.CreatedTime, "2006年1月2日 15:04:05") }} 修改时间:{{ stampToDate(archive.UpdatedTime, "最近更新于 2006-01-02") }} </div>archive.CreatedTimeand so on,archiveDetailThe context can be used directly, or as shown above, through a named variable
In the comment list
commentListThe comment object returned by the tag containsCreatedTimeProperty.
{% commentList comments with archiveId=archive.Id type="list" limit="5" %}
{% for item in comments %}
<div>
评论者:{{ item.UserName }}
评论内容:{{ item.Content }}
评论时间:{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}
</div>
{% endfor %}
{% endcommentList %}
In the user details
userDetailThe user object returned by the tag containsLastLoginandExpireTimeWaiting for timestamp attribute.
{% userDetail userInfo with name="UserName" id="1" %} {# 示例:获取ID为1的用户信息 #}
<div>
用户名:{{ userInfo }}
上次登录:{{ stampToDate(user.LastLogin, "2006-01-02 15:04:05") }}
VIP过期:{{ stampToDate(user.ExpireTime, "2006年1月2日") }}
</div>
(Please note,user.LastLoginand so on,userDetailThe context can be used directly, or as shown above, through a named variable
Summary
BystampToDateLabel and understand the unique time formatting rules of the Go language, you can flexibly convert timestamps to any date and time format you need in the AnQiCMS template.This not only helps to enhance the professionalism and user experience of the website, but also ensures the accuracy and consistency of time information display.In practice, it is recommended that you try different format combinations to find the one that best suits your website design style and user reading habits.
Frequently Asked Questions (FAQ)
Question: Why does the time formatting parameter of AnQiCMS look strange and not common?Y-m-d H:i:s?
Answer: This is because the AnQiCMS backend is developed using Go language, and Go language adopts its unique "reference time" mode in time formatting. It does not use placeholders, but uses a fixed reference time2006年1月2日15点04分05秒Use as a template, you need to map the format you want to the corresponding position at this reference time point. For example, if you want to display a four-digit year, you write2006; If you want to display a two-digit month, you write01and so on.
Question: If my timestamp is not 10 digits but 13 digits (millisecond level),stampToDateCan the tag still be used?
Answer:stampToDateThe label defaults to receiving a 10-digit Unix epoch second timestamp. If your timestamp is a 13-digit millisecond timestamp, you will need to pass it tostampToDateBefore dividing it by 1000, convert it to a second-level timestamp. For example:{{ stampToDate(item.MillisecondTime / 1000, "2006-01-02") }}Make sure the division result is an integer.
Question: Can I directly get the current time and format it in the template?
Answer: Yes, AnQiCMS providesnowtags to get the current time and format it. Its usage is similar tostampToDatebut does not require passing a timestamp parameter. For example:{% now "2006-01-02 15:04:05" %}It will output the current date and time. If you only need the year, you can use{% now "2006" %}.