How to use `stampToDate` to format the `UpdatedTime` of the document into a complete date and time that includes hours, minutes, and seconds?

Calendar 👁️ 66

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'

Related articles

In Anqi CMS template, the 'format' parameter of `stampToDate` should follow which time formatting standard of the Go language?

## The mystery of time formatting in `stampToDate` of AnQi CMS template: Unveiling the time standard in Go language As an experienced website operations expert, I know that the flexible display of timestamps in content management systems is crucial for the freshness of website information and user experience.AnQiCMS (AnQiCMS) relies on the powerful genes of the Go language, and inherits the unique elegance and efficiency of the Go language in template processing.Today, let's delve deeply into a commonly used and powerful time processing function in the Anqi CMS template —— `stampToDate`

2025-11-07

How to format `CreatedTime` obtained from the `archiveDetail` tag using `stampToDate` to the "YYYY-MM-DD" format?

## Unlock the AnQi CMS Time Magic: How to beautifully format `CreatedTime` to "YYYY-MM-DD" As an experienced website operations expert, I am well aware of the importance of content timeliness and display methods for user experience.In AnQiCMS (AnQiCMS) such a powerful content management system based on Go language, we can not only efficiently manage content, but also convert technical information into user-friendly display through flexible template tags.Today, let's talk about a common demand in operation

2025-11-07

How to use the `stampToDate` tag to display the publication date of each document on the article list page?

## Enabling content timeliness: The efficient way to display publication dates on the article list page of AnQi CMS In today's fast-paced digital world, the timeliness of content is crucial for the attractiveness of a website and the user experience.Whether it is news reports, technical articles, or product updates, clearly displaying the publication date can help readers quickly judge the value of the information and enhance the professionalism of the website.

2025-11-07

In AnQi CMS, how does the `stampToDate` tag convert a 10-digit timestamp to a common date format?

## Time Magician: How to easily handle 10-digit timestamps in AnQi CMS `stampToDate` tag In the world of content operation, the way time is presented often directly affects users' understanding and perception of the content.A clear and readable date format that allows users to quickly obtain key information while browsing articles, products, or comments, enhancing the overall browsing experience.However, at the bottom level of databases and systems, time is often represented by a series of 'mysterious' numbers - a 10-digit timestamp (Unix timestamp).

2025-11-07

Can the `stampToDate` tag customize the display of the English abbreviation of the month, such as "Jan", "Feb"?

## The `stampToDate` tag of AnQi CMS: Flexible time format customization, easily display month abbreviations As an experienced website operations expert, I know that precision and beauty are equally important in content display.AnQiCMS (AnQiCMS) provides strong support for content operation with its efficient and customizable features.In everyday operations, we often need to convert database timestamps to user-friendly date formats.Aqit CMS provides a very practical template tag - `stampToDate`.

2025-11-07

How to extract only the year from the timestamp using `stampToDate` in the AnQi CMS template?

As an experienced website operations expert, I know that it is crucial to efficiently and flexibly display information in daily content management.AnQiCMS (AnQiCMS) leverages the efficient features of the Go language and the template syntax of Django style, providing us with powerful content display capabilities.Today, we will focus on a very practical little trick in the Anq CMS template: how to extract only the year from the timestamp using the `stampToDate` tag.

2025-11-07

Does the `stampToDate` tag support formatting timestamps into the international date format "MM/DD/YYYY"?

In the daily operation of AnQiCMS, the formatting of the Unix timestamp is a common requirement, especially when our website content needs to be targeted at international users, the flexibility of the date display format is particularly important.Today, let's delve deep into the `stampToDate` tag in AnQiCMS to see if it can meet our needs to format timestamps into the international date format of "MM/DD/YYYY".

2025-11-07

In the `archiveList` loop, how does `stampToDate` ensure that each `item.CreatedTime` is formatted correctly?

## Mastering Time: The Art of Date Formatting in AnQiCMS' `archiveList` Loop In the world of content management, information such as the publication time and update time of website content is not only a key element for providing context to readers, but also an important indicator of a website's professionalism and user experience. For AnQiCMS, an enterprise-level content management system based on Go language and focusing on efficiency and flexibility, how to elegantly present the original timestamps stored in the database to users is a significant issue.

2025-11-07