In website content operations, the publication time of articles is often one of the focuses of users, as it not only affects the interest of users in reading, but also has potential effects on the practicality and authority of the content.However, the time information stored in the database is usually in a machine-readable timestamp format, such as a string of numbers, which is difficult for ordinary visitors to understand.AnQiCMS fully considered this point, providing a flexible way for you to convert these timestamps into clear and readable date and time formats, greatly enhancing the user experience.
AnQiCMS when managing articles in the background, whether you manually publish, schedule publishing (with the help of the "Time Factor-Scheduled Publishing" feature), or through content collection or import, the creation time of the article (CreatedTimeAnd update time(UpdatedTimeThe information will be stored in the standard timestamp format. These timestamps are the number of seconds elapsed since the Unix epoch (January 1, 1970 00:00:00 UTC), although precise, they are not intuitive to users.
Core Tool:stampToDateTag
To display these timestamps on the front-end page in a familiar and easy-to-read format, AnQiCMS provides a very practical template tag:stampToDate. This tag can convert timestamps to custom date and time strings.
Its basic usage method is very concise:{{stampToDate(时间戳变量, "格式字符串")}}
The key point is here格式字符串. The AnQiCMS template uses the unique Go language date and time formatting rules instead of the ones you might find in other systemsYYYY-MM-DDSuch symbols. Go language uses a fixed reference date and time as a template, and you need to construct the desired format through the components of this reference date.
This reference date is:2006年01月02日15时04分05秒.
You can use the numbers in this reference date to represent the date and time units you want to output:
- Year:
2006 - Month:
01(representing the month number, pad with zeros if less than two digits) orJan(representing the English abbreviation of the month) - Date:
02(representing the date number, with leading zeros if less than two digits) - hours:
15(24-hour format) or03(12-hour format) - minute:
04(with leading zeros if less than two digits) - seconds:
05(with leading zeros if less than two digits)
You can combine these reference numbers with a separator such as-///:Combining with Chinese characters (such as年/月/日/时/分/秒) to generate various required date and time formats.
The following are some common format examples:
| Format string | Example output (for example, at 14:30:00 on October 26, 2023) | Meaning |
|---|---|---|
"2006-01-02" |
2023-10-26 |
Year-Month-Date |
"2006/01/02" |
2023/10/26 |
Year/Month/Date |
"2006年01月02日" |
2023年10月26日 |
Chinese year-month-day |
"01-02" |
10-26 |
Month-Day |
"15:04" |
14:30 |
24-hour time:minute |
"15:04:05" |
14:30:00 |
24-hour format, time:minute:second |
"2006-01-02 15:04:05" |
2023-10-26 14:30:00 |
Year-Month-Day Hour:Minute:Second |
"Mon Jan 2 15:04:05 MST 2006" |
Thu Oct 26 14:30:00 CST 2023 |
Go language standard format, including weekday, month abbreviation and timezone |
Practical case: applying in article template
In the AnQiCMS article or product list (viaarchiveListtags), as well as on the article detail page (viaarchiveDetailtags), each article will haveCreatedTime(Article creation/publishing time) andUpdatedTimeThese two fields, (article last updated time), are provided in timestamp format.
Assuming you are looping through articles on a list page and have already assigned the article data toarchivesa variable, then in the loop bodyitemyou can format the time like this:
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<article>
<h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
<p>{{ item.Description }}</p>
<div class="meta">
<span>发布于:{{ stampToDate(item.CreatedTime, "2006年01月02日 15:04") }}</span>
<span>更新于:{{ stampToDate(item.UpdatedTime, "2006-01-02") }}</span>
<span>阅读量:{{ item.Views }}</span>
</div>
</article>
{% empty %}
<p>暂时没有文章。</p>
{% endfor %}
{% endarchiveList %}
In the above example:
{{ stampToDate(item.CreatedTime, "2006年01月02日 15:04") }}The article creation time will be formatted as{{ stampToDate(item.UpdatedTime, "2006-01-02") }}The article's update time will be formatted into a standard date format like “2023-10-26”.
Similarly, you can use it on the article detail page.archiveVariable to directly call the timestamp field of the current article:
<section>
<h1>{{ archive.Title }}</h1>
<div class="article-info">
<span>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04") }}</span>
<span>更新日期:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05") }}</span>
<span>浏览次数:{{ archive.Views }}</span>
</div>
<div class="article-content">
{{ archive.Content|safe }}
</div>
</section>
Notes and tips
- Uniqueness of Go language formatPlease: