As an experienced website operations expert, I deeply understand the art of content release timing and its profound impact on user experience, search engine optimization, and even operational efficiency.AnQiCMS (AnQi CMS) was born to meet these needs, and its 'Time Factor-Scheduled Publishing 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 is equally important. This is exactlystampToDateThis is the place where the template tag really shines.

Scheduling publishing feature: the cornerstone of flexible operation

AnQiCMS's 'Time Factor - Scheduled Publishing Feature' is undoubtedly one of its core highlights, giving content operations great flexibility.Imagine that you can be inspired to create content at night, but set the publication time for the next morning when users are active at peak hours, or schedule content release plans in advance for holidays without the need for manual monitoring.

In the AnQiCMS backend, when you edit a document, you will find a field named "Publish Time".By default, it will automatically fill in the current date and time.However, if you wish for this document to go live at a specific future date and time, simply set this field to the future date and time.AnQiCMS will intelligently publish content after reaching the moment, greatly enhancing the flexibility and automation level of content operations.The document clearly states, the format of this publication time is2006-01-02 15:04:05This is not only the input specification of the background, but also hints at its storage method within the system.

Front-end precise display:stampToDateMagic

However, the time information set by the background is usually stored in the database in the form of timestamps, which is efficient and convenient for machines, but a string of 10 or 13-digit digital timestamps is obviously not intuitive for human readers. To enable 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 a familiar, readable date and time format. Its usage is very intuitive, usually containing two core parameters: {{stampToDate(时间戳, "格式")}}.

  1. timestampThis is usually the original time data you want to format. In the AnQiCMS content model, the document'sCreatedTime(creation time) andUpdatedTime(Update time) and other fields are provided in the form of timestamps and can be used directly asstampToDatethe first parameter.
  2. FormatThis isstampToDateThe essence of it, it follows the unique time formatting rules of the Go language. It is used by many other programming languagesYYYY-MM-DDThis placeholder is different, Go language uses a specific reference time point-2006年01月02日 15时04分05秒And its corresponding number is used as a template. For example, if you want to display the year, you write2006If you want to display the month, write01. It is a very flexible and powerful formatting method.

Understanding this formatting rule in Go language is crucial:

  • year:2006
  • month:01(or)Janstands for English abbreviation,Januaryindicating the English full name)
  • Day:02(or)_2indicating the day of a single number, preceded by a space)
  • hour:15(24-hour format) or03(12-hour format)
  • minute:04
  • seconds:05
  • milliseconds:.000
  • AM/PM:PM(combined with 12-hour format)
  • Time Zone:MST(or)Z07:00Represents UTC offset)

By combining these reference values, you can customize almost any desired date and time display format.

Practical exercise: Applying in AnQiCMS template

Now, let's look at specific template code examples,stampToDateHow to achieve accurate display of the publication time in the AnQiCMS front-end template.

Scenario one: Display the publication time on the article detail page

Assuming we want to display the publication time on the article detail page (usually correspondingarchiveDetailtags) of the article. The document'sCreatedTimeThe field 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 usearchiveDetailThe tag obtained the current document'sCreatedTime(Timestamp), then pass it as the first argument tostampToDateand provides different Go language time formatting strings as needed.

Scenario 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 correspondingarchiveListtag) to display the publication time of multiple articles,stampToDateit also applies. InforWhen iterating over the list of articles, each article'sCreatedTimewill pass throughitem.CreatedTimeRetrieve.

`twig {% archiveList archives with type=“page” limit=“10” %}

{% for item in archives %}
    <div class="article-