In website operation, a clear and unified time display format is crucial for improving user experience and content professionalism.Especially the publication time of the article, a standardized "year-month-day" format makes it clear to visitors.In AnQiCMS (AnQi CMS), achieving this is actually very simple and flexible.
Understanding AnQiCMS's time handling method
When using AnQiCMS, you will find that articles (archive) and products, etc., all have two key time fields:CreatedTime(Creation time/Publish time) andUpdatedTime. In the template, you can directly call these fields, such as{{archive.CreatedTime}}or{{item.CreatedTime}}. They are usually presented in the form of Unix timestamps, such as a series of numbers1678886400. Such numbers are not intuitive for visitors and lack beauty.
AnQiCMS knows this need, therefore it provides special template tags and filters to help us easily convert these timestamps into various readable date formats.
The core of time formatting:stampToDateTags andformatParameters
AnQiCMS handles time formatting at its core with its powerfulstampToDatetags, as well as tags that can be used directly in some specific tagsformatParameter.
Go language's date formatting rules
Before we delve into how to use it, we first need to understand the Go language time formatting rules adopted by AnQiCMS. Similar to many other systems usingYYYY-MM-DDThis placeholder is different, the Go language uses a fixed reference time to define various date and time formats, which is:
2006年01月02日 15时04分05秒
You only need to construct the desired format string according to the corresponding part of this reference time. For example, to achieve the format "year-month-day", we should write'2006年01月02日'.
1. UsearchiveDetailLabel get and format the article detail page time
When you are on the article detail page (such asdetail.html), you can directly display the publish time of a single article in it.archiveDetailthe label useformatThis is the most convenient way to specify the output format.
<div class="article-meta">
<span>发布日期:</span>
{# 直接在 archiveDetail 标签中通过 format 参数指定“年-月-日”格式 #}
<span>{% archiveDetail with name="CreatedTime" format="2006年01月02日" %}</span>
</div>
Through this method, the publication time of the article will be displayed directly in the format ofUpdatedTimeField can also be formatted in the same wayformatFormatting is performed using the parameters.
2. InarchiveListWhile formatting the list of articles time in the loop
On article list page (for examplelist.htmlor home pageindex.html),you usually go througharchiveListTags cyclically display multiple articles. In this case,item.CreatedTimeThe return is an original timestamp, at which point it is necessary to usestampToDateTags as a filter to process:
{% archiveList articles with type="page" limit="10" %}
{% for item in articles %}
<article class="article-item">
<h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
<div class="article-info">
<span>分类:{% categoryDetail with name="Title" id=item.CategoryId %}</span>
{# 使用 stampToDate 标签格式化循环中的 item.CreatedTime #}
<span>发布时间:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</span>
<span>阅读量:{{item.Views}}</span>
</div>
<p>{{item.Description}}</p>
<a href="{{item.Link}}" class="read-more">阅读更多</a>
</article>
{% else %}
<p>暂时没有文章可供显示。</p>
{% endfor %}
{% endarchiveList %}
Here,stampToDateReceive two parameters: the first is the original timestamp (item.CreatedTime) and the second is the format string you want ("2006年01月02日").
More common time format examples
In addition to the "year-month-date" format, you can also combine various common date and time formats as needed:
Year/Month/Day:
"2006/01/02"{{stampToDate(item.CreatedTime, "2006/01/02")}}Display Effect:
2023/03/15Year-Month-Day Hour:Minute:Second:
"2006-01-02 15:04:05"{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}}Display Effect:
2023-03-15 10:30:00Month-Date:
"01-02"{{stampToDate(item.CreatedTime, "01-02")}}Display Effect:
03-15
By flexibly applying the reference time formatting rules of the Go language, you can easily achieve any date and time display effect you want in the AnQiCMS template.This unified and powerful formatting capability makes your website content display more professional and user-friendly.
Common Questions (FAQ)
1. Why is the time format string '2006年01月02日' instead of 'YYYY-MM-DD'?
This is because AnQiCMS is based on Go language, which uses a unique 'reference time' mode when formatting time. You need to follow2006年01月02日 15时04分05秒This fixed reference time to write your format string. For example,2006represents the year,