In website operation, the comment module is not only an important battlefield for user communication, but also a reflection of the vitality of the content ecosystem.A clear, easy-to-read comment posting time, which can greatly enhance user experience and allow visitors to grasp the freshness of the information at a glance.However, many content management systems (CMS) often output unformatted timestamps by default in templates, which are both rigid and not intuitive for ordinary users to read.
autocommentListautoCreatedTimeThe field returns the original timestamp, AnQiCMS also provides a highly flexible and easy-to-use way to format it into various date and time formats we are familiar with and love. This is thanks to its powerful template engine and built-instampToDateLabel.
KnowcommentListofCreatedTime
When we go throughcommentListThe label is displayed in the template loop, each comment data contains oneCreatedTimefield, for example:
{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
{% for item in comments %}
<div>
{# 如果直接输出,可能会看到这样的原始时间戳:1678886400 #}
<span>发布时间:{{item.CreatedTime}}</span>
{# ... 其他评论内容 ... #}
</div>
{% endfor %}
{% endcommentList %}
Such an original timestamp is accurate, but it has no readability for users. To convert it to a format like 'March 15, 2023 10:30' or '10:30 yesterday', you need to use the 'time magic' in the AnQiCMS template engine.stampToDateLabel.
stampToDateThe charm of tags
stampToDateIt is a powerful tool specially used by AnQiCMS to handle timestamp formatting.The usage is very intuitive, only two parameters are needed: the timestamp to be formatted and the date-time format string you want to present.
The basic syntax is:{{stampToDate(时间戳, "格式")}}.
It is especially important to emphasize that, as an enterprise CMS developed in Go language, its time formatting follows the unique specifications of Go language. Unlike many other languages that use placeholders such as 'Y-m-d H:i:s', Go language adopts a fixed reference time.2006-01-02 15:04:05.999999999 -0700 MSTEnglish as a format template. You can use each component of this reference time to build any format you want. Simply put:
2006Represents the year01represents the month02represents the date15represents hours in 24-hour format (can also be03represents hours in 12-hour format)04Represents minutes05Represents seconds.999999999represents nanoseconds-0700represents time zone offsetMSTrepresents time zone name
Mastered this rule, you can define the time display format at will.
Practice: Customize the comment time format
接下来,我们通过几个常见的示例,看看如何在 EnglishcommentList中 EnglishCreatedTime进行自定义格式化 English
示例一:仅显示年月日 English
If we only need to display the comment's posting date in the format 'Year-Month-Day', we can do it like this:
{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
{% for item in comments %}
<div>
<span>发布时间:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span> {# 输出:2023-03-15 #}
{# ... 其他评论内容 ... #}
</div>
{% endfor %}
{% endcommentList %}
Example two: Display the full date and time
If you need to display the complete date and time down to seconds, the format is “Year-Month-Day Hour:Minute:Second”, it can be defined like this:
{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
{% for item in comments %}
<div>
<span>发布时间:{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}}</span> {# 输出:2023-03-15 10:30:00 #}
{# ... 其他评论内容 ... #}
</div>
{% endfor %}
{% endcommentList %}
Example three: Display the Chinese date format
To fit the Chinese reading habit, we can format the date as "Year Month Day":
{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
{% for item in comments %}
<div>
<span>发布时间:{{stampToDate(item.CreatedTime, "2006年01月02日 15:04")}}</span> {# 输出:2023年03月15日 10:30 #}
{# ... 其他评论内容 ... #}
</div>
{% endfor %}
{% endcommentList %}
Example four: Only show time (hour:minute)
Sometimes, we may only need to pay attention to the time a comment is posted:
{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
{% for item in comments %}
<div>
<span>发布时间:{{stampToDate(item.CreatedTime, "15:04")}}</span> {# 输出:10:30 #}
{# ... 其他评论内容 ... #}
</div>
{% endfor %}
{% endcommentList %}
Through these examples, you can seestampToDateThe powerful features of the label, which can meet the vast majority of date and time display needs, greatly enhances the usability and aesthetics of the comment module.In practical application, you can choose the most suitable date and time format according to the overall design style of the website and user preferences.
Summary
Custom comment posting time format is a small but beautiful detail to enhance the user experience of the website. Anqi CMS relies on its flexible template engine and simple intuitivestampToDateTags, make this requirement easy.As website operators, we should make good use of these features, converting boring technical data into user-friendly and pleasing content, creating a more smooth and pleasant browsing environment for visitors.
Common Questions (FAQ)
Q1: Why does the time formatting string of AnQiCMS look so special, using '2006-01-02 15:04:05' instead of 'YYYY-MM-DD'?
A1:This is because AnQiCMS is developed in Go language. Go language, from the beginning of its design, abandoned the traditional 'Y-m-d' placeholder and adopted a fixed reference time point - '2006 January 2 3:04:05 PM MST (-0700)', that is,2006-01-02 15:04:05 -0700 MST. When you need to format time, you just need to replace the corresponding year, month, day, hour, minute, second, and other numbers in this reference time with the format characters you want (for example, replacing01with月),Go Language will parse and output the corresponding format time based on your template string.Although it may look a bit unusual at first glance, once you understand its principle, you will find it very intuitive and powerful.
Q2: BesidescommentListofCreatedTimeI amarchiveListorarchiveDetailobtained inCreatedTimeorUpdatedTimealso can be usedstampToDateto format?
A2:Of course you can.stampToDateThe tag is the universal time formatting tool provided by AnQiCMS template engine, it is not restricted to a specific tag. WhetherarchiveListThe article publication time (item.CreatedTime),archiveDetailThe article update time (archive.UpdatedTime), or any other Unix time