As an experienced website operations expert, I am well aware of the art of content publishing timing and its profound impact on user experience, search engine optimization, and operational efficiency.AnQiCMS (AnQi CMS) was born to meet these needs, and its 'Time Factor - Scheduled Release Function' is a powerful tool in the hands of content operators.But having strong publishing capabilities is not enough. How to present these carefully set publishing times elegantly and accurately to the readers on the front end is equally important.stampToDateThis is where the template tag really shines.
Scheduling release function: the cornerstone of flexible operation
In the AnQiCMS backend, when you edit a document, you will find a field named "Publish Time".It will automatically be filled with the current date and time by default.However, if you want this document to go live at a specific moment in the future, simply set this field to a future date and time.AnQiCMS will intelligently publish content automatically after reaching the moment, greatly enhancing the flexibility and automation level of content operation.2006-01-02 15:04:05This is not only the input specification of the background, but also implies the storage method within the system.
Front-end precise display:stampToDateThe magic of
However, these time information set in the background are usually stored in the database in the form of timestamps (timestamp), which is efficient and convenient for machines, but a string of 10 or 13-digit numeric timestamps is obviously not intuitive for human readers. In order for users to clearly and accurately read the specific time of content publication or update, AnQiCMS providesstampToDateThis concise and powerful template tag.
stampToDateThe core function of the label is to convert this machine-friendly timestamp into the familiar, readable date and time format that we are accustomed to. Its usage is very intuitive, usually including two core parameters:{{stampToDate(时间戳, "格式")}}.
- TimestampThis is typically the original time data you want to format. In the AnQiCMS content model, the document's
CreatedTime(Creation time) andUpdatedTime(Updated time) and other fields are provided in the form of timestamps and can be used directly asstampToDatethe first parameter. - Format:This is
stampToDatethe essence lies in, it follows the unique time formatting rules of the Go language. Unlike many other programming languages usingYYYY-MM-DDSuch placeholders are different, Go language adopts a specific reference point time -2006年01月02日 15时04分05秒and uses the corresponding number as a template. For example, if you want to display the year, write2006If you want to display the month, write01. This is a very flexible and powerful formatting method.
Understanding this formatting rule in Go is crucial:
- Year:
2006 - Month:
01(or)Janstands for the English abbreviation,JanuaryIndicates the English full name) - Day:
02(or)_2Indicates the day of a single number, preceded by a space) - Hour:
15(24-hour format) or03[12-hour format] - Minute:
04 - seconds:
05 - millisecond:
.000 - AM/PM:
PM(combined with 12-hour clock) - time zone:
MST(or)Z07:00denoting UTC offset)
By combining these reference values, you can customize almost any date and time display format you want.
Practice Exercise: Applying in AnQiCMS Template
Now, let's see through specific template code examples,stampToDateHow to accurately display the publish time in the AnQiCMS front-end template.
Scenario 1: Display the publish time on the article detail page.
Assuming we want to display the publication time of the article (usually corresponding toarchiveDetailtags) on the article detail page. The document'sCreatedTimefield will provide the timestamp we need.
{# 获取当前文章的发布时间戳,并格式化为 "年-月-日 时:分" #}
<div>
文章发布时间:
{% archiveDetail articleCreatedTime with name="CreatedTime" %}
{{stampToDate(articleCreatedTime, "2006-01-02 15:04")}}
</div>
{# 如果您希望显示为中文格式的日期 #}
<div>
发布日期:
{% archiveDetail articleCreatedTime with name="CreatedTime" %}
{{stampToDate(articleCreatedTime, "2006年01月02日")}}
</div>
{# 或者需要更详细的中文时间,精确到秒 #}
<div>
详细发布时间:
{% archiveDetail articleCreatedTime with name="CreatedTime" %}
{{stampToDate(articleCreatedTime, "2006年01月02日 15时04分05秒")}}
</div>
{# 甚至只显示月份和日期 #}
<div>
月日:
{% archiveDetail articleCreatedTime with name="CreatedTime" %}
{{stampToDate(articleCreatedTime, "01-02")}}
</div>
In this example, we first usearchiveDetailtag retrieves the current document'sCreatedTime(Timestamp), then pass it as the first parameter tostampToDateand provided different Go language time formatting strings as needed.
Scene two: Display the publication time of each article on the article list page.
When you need to display the publication time of multiple articles on an article list page (usually corresponding)archiveListtags),stampToDateit also applies. InforWhen iterating over the article list, each article'sCreatedTimewill beitem.CreatedTimeretrieved.
【en】 twig {% archiveList archives with type=“page” limit=“10” %}
{% for item in archives %}
<div class="article-