In website operation, the comment module is not only an important battlefield for user communication, but also an embodiment of the vitality of the content ecosystem.A clear and readable comment posting time, which can greatly enhance user experience, allowing visitors to grasp the freshness of the information at a glance.However, many content management systems (CMS) often output the comment time in an unformatted timestamp by default in their templates, which is hard and not intuitive for ordinary users to read.
AnQiCMS (AnQiCMS) is an efficient and highly customizable content management system that takes this into full consideration. EvencommentListTags when retrieving comment data, itsCreatedTimeThe field returns the original timestamp, AnQiCMS also provides a very flexible and easy-to-use way to format it into various date and time formats that we are familiar with and like. This is due to its powerful template engine and built-instampToDate.
Get to knowcommentListofCreatedTime
When we go throughcommentListThe tag displays comments in the template, each comment data includes aCreatedTimefield, 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 is unreadable to users. To convert it to a format like 'March 15, 2023 10:30' or '10:30 yesterday', it is necessary to use the 'time magic' in the AnQiCMS template engine...stampToDate.
stampToDateThe charm of tags
stampToDateIs AnQiCMS a powerful tool specifically used for timestamp formatting.Its usage is very intuitive, just pass in two parameters: the timestamp to be formatted and the date-time format string you want to present.
Its basic syntax is:{{stampToDate(时间戳, "格式")}}.
It should be emphasized that AnQi CMS is developed based on Go language, and therefore its time formatting follows the unique specification 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 MSTas a formatting template. You can use the components of this reference time to build any format you want. Simply put:
2006Represents the year01Represents the month02Represents the date15represents 24-hour time format (can also be03represents 12-hour time format)04representing minutes05representing seconds.999999999represents nanoseconds-0700represents timezone offsetMSTrepresents timezone name
Mastered this rule, you can define the time display format at will.
Practice: Customize the comment time format
Next, let's look at several common examples to see how tocommentListinCreatedTimeto customize the format.
Example one: only display the year, month and day
If we only need to display the publication date of the comment, in the format of
{% 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 to the second, the format is "Year-Month-Day Hour:Minute:Second", you can define 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 15:04:05")}}</span> {# 输出:2023-03-15 10:30:00 #}
{# ... 其他评论内容 ... #}
</div>
{% endfor %}
{% endcommentList %}
Example three: Display Chinese date format
To fit the Chinese reading habits, 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 might only need to pay attention to the time and minutes a comment was 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 %}
By these examples, you can seestampToDateThe strength of the tag, it can meet the vast majority of date and time display needs, greatly enhancing the usability and aesthetics of the comment module.In practice, you can choose the most suitable date and time format according to the overall design style of the website and user preferences.
Summary
The custom comment publishing time format is a small yet beautiful detail that enhances the user experience of the website. Anqi CMS relies on its flexible template engine and simple and intuitivestampToDateTags make this requirement easy. As a website operator, we should make good use of these features, transform boring technical data into user-friendly, pleasing content, and create a more smooth and pleasant browsing environment for visitors.
Frequently Asked 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 abandoned the traditional "Y-m-d" placeholder at the beginning of its design and adopted a fixed reference time point - "January 2, 2006 at 3:04:05 PM MST (-0700)" which is also2006-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 numbers in this reference time with the format characters you want (such as replacing the01Replace月), Go language will parse and output the corresponding format time according to your template string.Although it looks a bit peculiar at first glance, once you understand its principle, you will find it very intuitive and powerful.
Q2: BesidescommentListofCreatedTime, I amarchiveListorarchiveDetailinCreatedTimeorUpdatedTimealso can be usedstampToDateCan I format it?
A2:Of course you can.stampToDateThe tag is a general time formatting tool provided by the AnQiCMS template engine, it is not limited to specific tags. Whether it isarchiveListThe article was published at (item.CreatedTime),archiveDetailThe article was updated at (archive.UpdatedTime) or any other with Unix time