In website operation, the content release date is not just a time mark, but also an important basis for visitors to obtain information timeliness.A clear and readable date format that can significantly improve user experience and make your website content appear more professional and considerate.
However, in such a powerful content management system as AnQiCMS, the publish date is usually stored in the database in the form of a 'timestamp', which is usually a string of numbers that is difficult to understand directly. For example, you might see1609470335Such numbers, rather than the ones we are accustomed to2021年1月1日At this point, we need to format these timestamps into a human-friendly date and time format.
Luckyly, Anqi CMS provides an extremely convenient and flexible built-in template tag for this, making the process effortless.
Say goodbye to the ocean of numbers: to understandstampToDateTag
The Anqi CMS template system adopts a syntax similar to the Django template engine, one of the core highlights beingstampToDateThe label is specifically designed to convert timestamps into the familiar date and time format.
Its basic usage is very intuitive:
{{ stampToDate(时间戳, "格式") }}
There are two key parts:
Timestamp:This is the original timestamp you obtain from the content data, which is usually a 10-digit or 13-digit number. In the Anqi CMS template, the publication time of articles or products is usually through
item.CreatedTimeObtain, the update time isitem.UpdatedTime. These fields will directly provide timestamp values.Format:This is a string used to inform the system of the specific format you want the date and time to be displayed in.The AnQi CMS is developed based on the Go language, therefore, the format strings here follow the unique date and time formatting conventions of the Go language.
Understand the "magic numbers" of date formatting in Go language
The Go language has a very clever convention in date and time formatting: it does not use placeholders similar toY-m-dbut instead uses a fixed reference date and time"2006-01-02 15:04:05"—To construct the format. You just need to replace the year, month, day, hour, minute, second, and other elements in this reference date with the display method you want, and the system will automatically recognize and apply it.
Let's understand the usage of this 'magic number' with some examples:
Display the year, month and date (e.g., 2023-10-26)If you wish to display
年-月-日The format, it can be written like this:"2006-01-02".{{ stampToDate(item.CreatedTime, "2006-01-02") }}It may output2023-10-26
Chinese-friendly format (e.g., October 26, 2023)If you want a format that is more in line with Chinese reading habits, just replace the hyphen in the reference date with Chinese:
"2006年01月02日".{{ stampToDate(item.CreatedTime, "2006年01月02日") }}It may output2023年10月26日
Contain time (for example: 2023-10-26 14:30)Do you want to be precise to hours and minutes? Add the 'magic number' to the time part of the format string:
"2006-01-02 15:04".{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}It may output2023-10-26 14:30
Show only the time (e.g., 14:30:55)If you only need to display the time, the format can be simplified to:
"15:04:05".{{ stampToDate(item.CreatedTime, "15:04:05") }}It may output14:30:55
Custom delimiter (e.g., 2023/10/26)You can freely change the separator between date elements according to your needs:
"2006/01/02".{{ stampToDate(item.CreatedTime, "2006/01/02") }}It may output2023/10/26
The mystery of this 'magic number' lies in,2006represents the year,01represents the month,02represents the date,15Represents hours in a 24-hour clock system,04represents the minute,05Represents seconds. Just remember this date and time and use it to "build" any output format you want.
Hands-on practice: Apply it to your Anqi CMS template.
Now, let's see how to apply in actual Anqi CMS templatesstampToDate.
Suppose you are editing a template for an article detail page (for examplearticle/detail.htmlorarchive/detail.html), and hope to display the publication date and update date of the article:
{# 假设当前页面的文章数据可以通过 archive 变量直接访问 #}
<article>
<h1>{{ archive.Title }}</h1>
<div class="post-meta">
<span>发布于:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}</span>
{# 您可能希望更新时间只显示日期,或者只在有更新时才显示 #}
{% if archive.UpdatedTime and archive.UpdatedTime != archive.CreatedTime %}
<span>最近更新:{{ stampToDate(archive.UpdatedTime, "2006-01-02") }}</span>
{% endif %}
</div>
<div class="post-content">
{{ archive.Content|safe }}
</div>
</article>
If you are on the article list page (for examplearticle/list.htmlorarchive/list.html),need to iterate through multiple articles and display their publication dates, the situation is also simple:
`twig {% archiveList archives with type=“page” limit=“10” %}
{% for item in archives %}
<div class="archive-item">
<h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
<p class="summary">{{ item.Description }}</p>
<div class="info">
<span>分类:{% categoryDetail with name="Title" id=item.CategoryId %}