As an experienced website operations expert, I know that the details of content presentation are crucial for user experience and information delivery.AnQiCMS (AnQiCMS) provides us with great freedom with its flexible and powerful template engine.stampToDatefunction, to document theUpdatedTimeformatted to include complete date and time with hours, minutes, and seconds.
The art of time processing in the Anqi CMS template
In AnQi CMS, the creation time of documents (CreatedTimeand update timeUpdatedTime通常以UNIX时间戳(一个代表自1970年1月1日00:00:00 UTC以来秒数的整数)的形式存储在数据库中。Although this format is very efficient for internal processing of the system, a string of cold numbers is clearly not intuitive and friendly for end-users.Imagine, when users see “Last updated: 1678886400”, they need to do mental arithmetic or use a tool to understand its meaning.This not only affects the reading experience, but also reduces the readability and professionalism of the content.
Therefore, converting these original timestamps into a date and time format that includes year, month, day, hour, minute, and second, which is familiar to our daily habits, is a key step to enhancing website user experience and making information clear at a glance. Anqi CMS provides a powerful built-in template function for this.stampToDate.
core tools:stampToDateFunction Details
stampToDateIt is a function specifically used for timestamp formatting in the AnQi CMS template engine. Its design is simple and efficient, and it can output the timestamp in the format you specify.
Its basic syntax is:
{{stampToDate(时间戳, "格式")}}
There are two key parameters here:
- Timestamp(
时间戳): This is usually an integer value representing a date and time, such as the topic of our article.UpdatedTime。In the template, it will be a variable, likearchive.UpdatedTimeoritem.UpdatedTime. - format (
"格式"):This is a string used to define the specific display style of the timestamp after it is formatted. This isstampToDateThe most unique, also the most likely to confuse beginners.The template engine of Anqi CMS is based on Go language, therefore it follows the unique date and time formatting rules of Go language.
different from commonYYYY-MM-DDor%Y-%m-%dAn placeholder, Go language formatting is based on afixed reference datedefined. This reference date is:
2006年01月02日 15时04分05秒 -0700 MST
You need to replace the year, month, day, hour, minute, and second information you want to display with the corresponding information in this reference date.NumberBuild your format string. It may sound abstract, but once you master its rules, you will find it very flexible.
UpdatedTime: Get the document update time.
UpdatedTimeThe field is used in the CMS to record the last modification time of the document. It is usually obtainable in the following two common template scenarios:
- Document details pageWhen you view the specific content of a document, you can
archive.UpdatedTime(or the custom document detail variable name you define, for examplearticleInfo.UpdatedTime)to get. - Document List Page:In the list of multiple documents displayed, such as article lists, product lists, etc., it is usually
foruse in a loop.item.UpdatedTimeto obtain the update time of each document.
Next, we will combinestampToDateFunction, in these two scenarios, practical training is carried out.
Practical Training: FormattingUpdatedTimespecific steps
Scenario 1: Display the complete update time on the document detail page
Assuming your document detail template has alreadyarchiveDetailobtained the information of the current document through the tag and assigned it toarchiveInfoVariable. Do you want to display "Last updated time: 2023-10-27 10:30:45" on the page?
{# 假设通过 archiveDetail 标签已获取文档详情,并赋值给 archiveInfo #}
{% archiveDetail archiveInfo with name="Title" %} {# 只是为了获取 archiveInfo 对象,实际场景可能已在其他地方获取 #}
{% endarchiveDetail %}
<div>
<h1>{{ archiveInfo.Title }}</h1>
<p>发布时间:{{ stampToDate(archiveInfo.CreatedTime, "2006-01-02 15:04:05") }}</p>
<p>最后更新时间:{{ stampToDate(archiveInfo.UpdatedTime, "2006-01-02 15:04:05") }}</p>
{# 如果只需要显示到分钟,可以这样: #}
<p>最后更新(精确到分钟):{{ stampToDate(archiveInfo.UpdatedTime, "2006-01-02 15:04") }}</p>
</div>
Code analysis:
- We directly pass
archiveInfo.UpdatedTimeasstampToDatethe first parameter of the function. - 第二个参数
"2006-01-02 15:04:05"就是我们用Go语言参考日期构建的格式字符串。2006representing a year,01Represents the month, :02Represents the day, :15represents the hour in 24-hour time format,04represents the minutes,05代表秒。中间的连接符(如-or:)和空格都是字面值,会原样输出。
Scene two: Show the full update time of each document on the document list page
When you usearchiveListWhen displaying multiple documents in a loop, you need to applystampToDateThe function to each document in the loopitemobject.
{# 假设通过 archiveList 标签获取文章列表 #}
<div class="article-list">
{% archiveList archives with type="list" limit="5" %}
{% for item in archives %}
<div class="article-item">
<h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
<p class="article-meta">
发布时间:<span>{{ stampToDate(item.CreatedTime, "2006-01-02 15:04:05") }}</span>
更新时间:<span>{{ stampToDate(item.UpdatedTime, "2006-01-02 15:04:05") }}</span>
</p>
<p class="article-desc">{{ item.Description }}</p>
<a href="{{ item.Link }}" class="read-more">阅读详情</a>
</div>
{% else %}
<p>暂无文章</p>
{% endfor %}
{% endarchiveList %}
</div>
Code analysis:
- In
forIn the loop, we useitem.UpdatedTimeGet the current loop item's update timestamp. - Similarly,
"2006-01-02 15:04:05"As a format string, ensure that the update time of each article is displayed in a complete date and time format.
Master the date formatting magic of Go Language
Understanding date formatting in Go is crucial. Remember,The format string you provided is not a pattern, but an exampleYou need to provide is `2006-01-`