In website content operation, the publication time of articles is often one of the focuses of users, which not only affects the interest of users in reading, but also has a potential impact on the timeliness and authority of the content.However, the time information stored in the database is typically in a machine-readable timestamp format, such as a string of numbers, which is difficult for ordinary visitors to understand.AnQiCMS fully considers this point, providing a flexible way for you to convert these timestamps into clear and easy-to-read date and time formats, greatly enhancing the user experience.
AnQiCMS when managing articles in the background, whether you manually publish, schedule publication (using the 'Time Factor - Scheduled Publication' feature), or through content collection or import, the creation time of the article (CreatedTimeand update timeUpdatedTime
Core Tools:stampToDatetags
In order to present these timestamps on the front-end page in a familiar and easy-to-read format, AnQiCMS provides a very practical template tag: stampToDateThis tag can convert timestamps to custom date and time strings.
Its basic usage is very concise:{{stampToDate(时间戳变量, "格式字符串")}}
The key point here is格式字符串.AnQiCMS template uses the Go language's unique date and time formatting rules, rather than what you might commonly see in other systemsYYYY-MM-DD[such symbols.] The Go language uses a fixed reference date and time as a template, and you need to construct the format you want by using the various 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 wish to output:
- Year:
2006 - Month:
01(representing the month number, pad with zero if less than two digits) orJan(representing the month abbreviation in English) - Date:
02(representing the date number, pad with zero if less than two digits) - Hour:
15(24-hour format) or03[12-hour format] - minutes:
04(pad with zero if less than two digits) - seconds:
05(pad with zero if less than two digits)
You can combine these reference numbers with delimiters (such as-///:,) and Chinese characters (such as年/月/日/时/分/秒) to generate various date and time formats needed.
Here are some examples of common formats:
| Format string | Example output (for example, 2023-10-26 14:30:00) | Meaning |
|---|---|---|
"2006-01-02" |
2023-10-26 |
Year-Month-Day |
"2006/01/02" |
2023/10/26 |
Year/Month/Day |
"2006年01月02日" |
2023年10月26日 |
Chinese Year-Month-Date |
"01-02" |
10-26 |
Month-Date |
"15:04" |
14:30 |
24-hour format Hour:Minute |
"15:04:05" |
14:30:00 |
24-hour format Hour: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: Application in article template
in the AnQiCMS article or product list (viaarchiveListtags), as well as the article detail page (viaarchiveDetailtags), each article will haveCreatedTime(Article creation/publishing time) andUpdatedTime(Article last updated time) these two fields, they are provided in timestamp format.
Assuming you are looping through a list of articles on a page and you 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 creation time of the article will be formatted into a Chinese date-time format of "October 26, 2023 14:30".{{ stampToDate(item.UpdatedTime, "2006-01-02") }}The article's update time will be formatted into the standard date format like “2023-10-26”.
Similarly, on the article detail page, you can usearchiveCall the current article's timestamp field directly using a variable:
<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>
Cautionary Notes and Tips
- Uniqueness of Go language formattingPlease: