In website content management, the display of date and time information is ubiquitous.No matter the publication date of the article, the listing date of the product, or the submission time of the comments, a clear and readable date format is crucial for enhancing user experience.AnQiCMS (AnQiCMS) understands this need and provides powerful template tags and filters, among whichstampToDateThe filter is a tool that converts the original Unix timestamp into a localized date string that we are familiar with.
This article will delve deeper intostampToDateThe usage of the filter, which helps you easily implement various custom date and time formats in Anqin CMS templates.
Understanding Unix timestamps in Anqin CMS
In Anqi CMS, many date and time information stored in the database, such as the creation time of documentsCreatedTimeor update timeUpdatedTimeIt usually exists in the form of a Unix timestamp. A Unix timestamp is an integer representing the number of seconds elapsed since 00:00:00 UTC on January 1, 1970, also known as the Unix epoch.
Display such timestamps directly in the template, for example1609470335It is difficult for ordinary users to understand. In order to provide a better reading experience, we need to format it into a localized string such as "2021-01-01 12:25".
Core Tool:stampToDateFilter
The Anqi CMS template engine has borrowed the syntax of Django and providedstampToDateThis convenient filter is specifically used to format Unix timestamps into readable date and time strings.
Basic syntax:
{{ stampToDate(时间戳, "格式") }}
Among them:
时间戳: is typically what you get from a data object likeitem.CreatedTime)Retrieved 10-digit Unix timestamp."格式"This is a string that defines the specific format you want the date and time to be displayed in. It is especially important to note that this "format" is not the common one.Y-m-d H:i:sThis pattern, instead, adopts the unique "reference time" mechanism of the Go language.
Master the GoLang time format secret.
Go language uses a fixed reference point to define time formats, representing each time unit with this reference time point:2006年01月02日 15点04分05秒.
This means, when you want to display the year, you should use2006; When you want to display the month, you should use01(representing January); When you want to display the date, you should use02 Representing the second, and so on. The following are common correspondences:
2006-> Year (YYYY)01or1-> Month (MM/M)02or2-> Day (DD/D)15or3-> Hour (24-hour format HH/H) or (12-hour format hh/h)04-> Minute (MM)05-> Second (SS)Mon-> Day of the week (e.g., Mon, Tue, Wed)Monday-> Full day of the week (e.g., Monday, Tuesday)Jan-> Abbreviated English month names (e.g., Jan, Feb)January-> Full English months (e.g., January, February)
By combining these reference time elements, you can almost define any desired date and time format.
Common formatting examples and applications
Suppose we have a Unix timestamp1672502400It corresponds to2023年01月01日 00:00:00. Let's see how to usestampToDateThe filter formats it into various common localized strings.
“YYYY-MM-DD HH:MM”(such as: January 1, 2023 00:00)
{{ stampToDate(1672502400, "2006年01月02日 15:04") }}output:
2023年01月01日 00:00“YYYY-MM-DD”(such as: 2023-01-01)
{{ stampToDate(1672502400, "2006-01-02") }}output:
2023-01-01“MM/DD HH:MM”(like: 01/01 00:00)
{{ stampToDate(1672502400, "01/02 15:04") }}output:
01/01 00:00“HH:MM:SS”(like: 00:00:00)
{{ stampToDate(1672502400, "15:04:05") }}output:
00:00:00“YYYY/MM/DD Monday”(such as: 2023/01/01 Sunday)
{{ stampToDate(1672502400, "2006/01/02 星期Monday") }}output:
2023/01/01 星期Sunday(note the Go language's)MondayThe complete day of the week is output here, it will be Sunday because 1672502400 is Sunday
Examples of actual application scenarios
In the template design of AnQi CMS,stampToDateFilters are often usedarchiveList(Document list),archiveDetail(Document details) andcommentListin tags such as (comment list) and others.
For example, display the publication time of each article in a document list:
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<div class="article-item">
<h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
<p>发布时间:
<time datetime="{{ stampToDate(item.CreatedTime, "2006-01-02T15:04:05") }}">
{{ stampToDate(item.CreatedTime, "2006年01月02日 15:04") }}
</time>
</p>
<p>{{ item.Description }}</p>
</div>
{% empty %}
<p>暂时没有文章。</p>
{% endfor %}
{% endarchiveList %}
In this example,item.CreatedTimeThe obtained is a Unix timestamp, bystampToDateFilter, we have formatted it into a user-friendly "year-month-date hour:minute" format, and also<time>label'sdatetimeThe property outputs machine-readable ISO 8601 format, balancing user experience and SEO.