In website operation, the content publication date is not just a time mark, but also an important basis for visitors to obtain information timeliness.An easily readable and clear date format that significantly enhances user experience, making your website content appear more professional and thoughtful.
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 often a string of numbers that is difficult to understand directly. For example, you might see1609470335This number, rather than the one we are accustomed to,2021年1月1日. At this point, we need to format these timestamps into a human-friendly date and time format.
幸运的是,安企CMS为此提供了一个极其便捷且灵活的内置模板标签,让这个过程变得轻而易举。
告别数字海洋:认识stampToDatetags
The template system of Anqi CMS adopts syntax similar to Django template engine, one of the core highlights beingstampToDateThis tag is specifically designed to convert timestamps to the date and time format we are familiar with.
Its basic usage is very intuitive:
{{ stampToDate(时间戳, "格式") }}
There are two key parts:
Timestamp:This is the original timestamp you obtain from the content data, usually a 10-digit or 13-digit number. In the template of Anqi CMS, the publication time of articles or products is usually through
item.CreatedTimeGet, the update time isitem.UpdatedTime. These fields will directly provide timestamp values.Format:This is a string that tells the system the specific format you want the date and time to be displayed in.The underlying of AnQi CMS is developed in Go language, therefore the format string here follows 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 for date and time formatting: it does not use placeholders like this, but instead uses a fixed reference date and time -Y-m-dsuch as this, but instead uses a fixed reference date and time —"2006-01-02 15:04:05"——To construct the format. You only need to replace the year, month, day, hour, minute, and second elements in this reference date with the display style you want, and the system can automatically identify and apply it.
Let us understand the usage of this 'magic number' through some examples:
Display the year, month, and date (for example: 2023-10-26)If you wish to display
年-月-日format, it can be written as:"2006-01-02".{{ stampToDate(item.CreatedTime, "2006-01-02") }}it may output2023-10-26
English friendly format (e.g., October 26, 2023)If you want a format that is more in line with Chinese reading habits, simply replace the hyphen in the reference date with Chinese:
"2006年01月02日".{{ stampToDate(item.CreatedTime, "2006年01月02日") }}it may output2023年10月26日
Contains time (for example: 2023-10-26 14:30)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
Only show time (e.g., 14:30:55)If you only need to display 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 the hour in 24-hour time format,04represents the minutes,05Represents seconds. Just remember this date and time, and use it to 'construct' any output format you want.
Hands-on practice: Apply it in your safe CMS template.
Now, let's see how to apply it in an actual security CMS templatestampToDateLabel.
Assume you are editing a template for an article detail page (for example)article/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 traverse 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 %}