As an experienced website operations expert, I am well aware that the details of content presentation are crucial for user experience and information communication.AnQiCMS (AnQiCMS) with its flexible and powerful template engine provides us with great freedom.Today, let's delve deeply into a common and practical need in content operation: how to utilizestampToDateFunction, to document theUpdatedTimeFormat into a complete date and time including hours, minutes, and seconds.

Time processing art in Anqi CMS template

In AnQi CMS, the creation time of documents (CreatedTimeAnd update time(UpdatedTime) It is usually stored in the database in the form of a UNIX timestamp (an integer representing the number of seconds since January 1, 1970, 00:00:00 UTC.Although this format is very efficient for internal system processing, a string of cold numbers is obviously not intuitive and friendly for end-users.Imagine, when users see “Last updated: 1678886400”, they need to do mental arithmetic or use a tool to understand its meaning.This not only affects the reading experience, but also reduces the readability and professionalism of the content.

Therefore, converting these original timestamps into a date and time format that includes year, month, day, hour, minute, and second according to our daily habits is a crucial step in enhancing the user experience of a website and making information clear at a glance. Anqi CMS provides a powerful built-in template function for this:stampToDate.

Core tool:stampToDateFunction Details

stampToDateIt is a function specifically used for timestamp formatting in the Anqi CMS template engine. Its design is simple and efficient, able to output the timestamp in the specified format.

The basic syntax is:

{{stampToDate(时间戳, "格式")}}

Here are two key parameters:

  1. Timestamp (时间戳)This usually represents a date-time integer value, such as the article topic we mentioned inUpdatedTime. In the template, it will be a variable, likearchive.UpdatedTimeoritem.UpdatedTime.
  2. format("格式")This is a string used to define the specific display style you want for the timestamp format. This isstampToDateThe most unique and also the easiest place to confuse beginners.The template engine of Anqi CMS is based on Go language, therefore it follows the unique date and time formatting rules of Go language.

different from the commonYYYY-MM-DDor%Y-%m-%detc. Placeholder, Go language formatting is based on aFixed reference dateTo define. This reference date is:

2006年01月02日 15时04分05秒 -0700 MST

You need to replace the year, month, day, hour, minute, second information you want to display with the corresponding information in this reference date.NumberBuild your format string. It may sound a bit abstract, but once you master its rules, you will find it very flexible.

UpdatedTime: Document update time acquisition

UpdatedTimeThe field is used in Anqi CMS to record the last modification time of a document. It is usually obtainable in the following two common template scenarios:

  • Document detail pageWhen you view the specific content of a document, you can usearchive.UpdatedTime(or you can define your own document detail variable name, such asarticleInfo.UpdatedTime) to obtain.
  • Document list pageIn displaying a list of multiple documents, such as article lists, product lists, etc., it is usuallyforUsing a loopitem.UpdatedTimeto get the update time of each document.

Next, we will combinestampToDateFunction, practice in these two scenarios.

Practice: FormattingUpdatedTimespecific steps

Scenario one: Display the full update time on the document detail page.

Assuming your document detail template has alreadyarchiveDetailobtained the information of the current document and assigned it toarchiveInfoVariable. You want to display 'Last updated time: 2023-10-27 10:30:45' on the page.

{# 假设通过 archiveDetail 标签已获取文档详情,并赋值给 archiveInfo #}
{% archiveDetail archiveInfo with name="Title" %} {# 只是为了获取 archiveInfo 对象,实际场景可能已在其他地方获取 #}
{% endarchiveDetail %}

<div>
    <h1>{{ archiveInfo.Title }}</h1>
    <p>发布时间:{{ stampToDate(archiveInfo.CreatedTime, "2006-01-02 15:04:05") }}</p>
    <p>最后更新时间:{{ stampToDate(archiveInfo.UpdatedTime, "2006-01-02 15:04:05") }}</p>
    {# 如果只需要显示到分钟,可以这样: #}
    <p>最后更新(精确到分钟):{{ stampToDate(archiveInfo.UpdatedTime, "2006-01-02 15:04") }}</p>
</div>

Code analysis:

  • We directly takearchiveInfo.UpdatedTimeasstampToDatefirst parameter of the function.
  • the second parameter"2006-01-02 15:04:05"This is the format string built by referring to the date in Go language.2006Represents the year,01Represents the month,02represents the day,15Represents hours in a 24-hour clock system,04represents the minute,05Represents the second. The connecting characters (such as-or:) and spaces are literal values and will be output as is.

Scenario two: Display the full update time of each document on the document list page

When you usearchiveListWhen displaying multiple documents in a loop with tags, you need to applystampToDateThe function to each item in the loopitemObject.

{# 假设通过 archiveList 标签获取文章列表 #}
<div class="article-list">
    {% archiveList archives with type="list" limit="5" %}
    {% for item in archives %}
    <div class="article-item">
        <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
        <p class="article-meta">
            发布时间:<span>{{ stampToDate(item.CreatedTime, "2006-01-02 15:04:05") }}</span>
            更新时间:<span>{{ stampToDate(item.UpdatedTime, "2006-01-02 15:04:05") }}</span>
        </p>
        <p class="article-desc">{{ item.Description }}</p>
        <a href="{{ item.Link }}" class="read-more">阅读详情</a>
    </div>
    {% else %}
    <p>暂无文章</p>
    {% endfor %}
    {% endarchiveList %}
</div>

Code analysis:

  • InforIn the loop, we useitem.UpdatedTimeGet the update timestamp of the current loop item.
  • Similarly,"2006-01-02 15:04:05"As a format string, ensure that the update time of each article is displayed in a complete date and time format.

Mastering the Go Language Date Formatting Magic

Understanding Go's date formatting is crucial. Remember,The format string you provided is not a pattern, but an exampleThe information you need to provide is `January 2006'