In website operation, the timeliness of content is often the key to attracting readers' attention.Whether it is the publication date of the article or the update time of the product, these information will greatly enhance the user experience if presented on the page in a clear and understandable way.However, you may find that the time information recorded by AnQiCMS background, when called directly in the template, often displays as a long series of numbers, which is what we commonly call a timestamp.These original timestamps are convenient for internal system processing, but they lack readability for ordinary users.

It is fortunate that AnQiCMS provides a very practical and powerful tag, which can easily convert these timestamps into the familiar, easy-to-read date and time format that we are accustomed to.

Understand timestamp and time data in AnQiCMS

In AnQiCMS, many content objects contain time-related fields. For example, when you usearchiveDetailorarchiveListTags can be used to retrieve articles (archive) or product details.CreatedTime(Creation time) andUpdatedTime(Update time) fields.These fields return the exact Unix timestamp, which is the number of seconds elapsed since 00:00:00 UTC on January 1, 1970.They accurately record the time points of events, but further formatting is needed to present them in a friendly manner to visitors.

ApplicationstampToDateTags are used for formatting.

To solve the readability problem of timestamps, AnQiCMS is built-in.stampToDateThis template tag. Its usage is very intuitive. You just need to provide the timestamp to be formatted and specify the date and time format you want it to display. Its basic syntax structure is as follows:

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

Here is the “timestamp” you receivedCreatedTime/UpdatedTimeThese are the numeric values obtained from the fields such as “format” is the key to defining the output style.

Master date and time formatting: Go's magic numbers

AnQiCMS is developed based on Go language, therefore its date and time formatting rules also follow the unique way of Go language. You may need to remember a special reference time: 2006-01-02 15:04:05This sequence of numbers is not random, but a standard template used by the Go language to represent the parts of date and time:

  • 2006: represents the year
  • 01: represents the month (with a leading zero)
  • 02: represents the date (with a leading zero)
  • 15:represents the hour in 24-hour format.03It represents the 12-hour format.
  • 04:represents minutes
  • 05:represents seconds

By cleverly combining and arranging these 'magic numbers', you can flexibly customize various date and time display effects.

Some common formatting examples:

  • Only show year, month, and day (separated by hyphens): "2006-01-02"For example, it will be displayed as:2023-10-27.
  • Only show year, month, and day (separated by slashes): "2006/01/02"For example, it will be displayed as:2023/10/27.
  • Show full date and time (to the minute): "2006-01-02 15:04"For example, it will be displayed as:2023-10-27 10:30.
  • Display the full date and time (to seconds): "2006-01-02 15:04:05"For example, it will be displayed as:2023-10-27 10:30:45.
  • Chinese year-month-day format: "2006年01月02日"For example, it will be displayed as:2023年10月27日.

Practical application: tostampToDateintegrate into the template

Now, let's see how to apply it in the actual AnQiCMS templatestampToDateTags:

Example 1: Display the publication time on the article detail page

<div class="post-meta">
    <span class="publish-date">发布时间:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}</span>
</div>

Example 2: Display the update time of each article on the article list page

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <div class="article-item">
        <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
        <p class="update-info">最近更新于:{{ stampToDate(item.UpdatedTime, "2006/01/02") }}</p>
    </div>
    {% endfor %}
{% endarchiveList %}

You can see these simple code snippets by these.stampToDateHow to combine tags witharchiveDetailorarchiveListThe timestamp field obtained is combined.Whether you want to display time to the second or just need a simple date, this tag can easily help you achieve it, making your website content come alive.

When designing templates, it is recommended that you choose a unified date and time display format for the entire website to ensure consistent page style, thereby providing visitors with a smoother, more professional browsing experience. MasterstampToDateLabel, it is an indispensable practical skill in AnQiCMS content operation.


Common Questions (FAQ)

  1. Q: Why does the time I format deviate from the actual current time?A: AnQiCMS server will use the time zone of the server it is located in by default when processing timestamps.If the time zone of your server does not match the time zone you expect to display (for example, your local time zone), a discrepancy may occur.You can check the time zone configuration of the server, or adjust it if AnQiCMS provides a corresponding time zone configuration option.In some special cases, it may be necessary to convert the timestamp to the appropriate time zone by the backend logic before passing it to the template.
  2. Q:stampToDateCan the label handle all types of timestamps? For example, 13-digit millisecond timestamps?A:stampToDateThe label expects to process a 10-digit Unix timestamp, that is, a second-level timestamp. If your timestamp is a 13-digit millisecond timestamp, you may need to convert it before passing it instampToDateBefore, first convert it to a 10-digit second timestamp by other means (for example, by dividing it by 1000 in the Go language timestamp acquisition logic).
  3. Q: Can I add non-numeric characters to the format string of the label, such as 'AM/PM'?stampToDateThe format string is based on the reference time of the Go language.A:stampToDateThe format string is based on the reference time of the Go language.2006-01-02 15:04:05. At this reference time,03represents the hour in a 12-hour clock format (with a leading zero), combined withPMcan represent AM/PM. For example,"2006-01-02 03:04 PM"will be displayed2023-10-27 10:30 AMor2023-10-27 10:30 PM。You can combine these elements and customize the text to achieve more complex display effects.