In website content operation, the correct display of date and time information is crucial, as it not only affects the user's reading experience but also directly relates to the timeliness and accuracy of the information.AnQiCMS (AnQiCMS) knows this and provides powerful and flexible timestamp formatting tags, allowing developers and operators to easily convert the original timestamps stored on the backend into a date and time format that is intuitive and easy to read for front-end users.

Why do we need timestamp formatting?

We know that the data stored on the website backend, especially the publish time, update time, comment time, etc., usually exists in the form of a timestamp (Unix timestamp). For example, you may see a string of numbers1678886400.Such numbers are efficient for computers, but they are meaningless for users accessing websites.Therefore, we need a mechanism to format these original timestamp data into various readable forms such as

Security CMS solution:stampToDatetags

English CMS provides a special timestamp format tag for this.stampToDateThis tag can receive a timestamp and a format string as parameters, and output the corresponding date and time according to the format you define.

Its basic usage method is very concise and clear:{{stampToDate(时间戳, "格式")}}

The key is in two parts:

  1. Timestamp(时间戳)This is usually a 10-digit or 13-digit number, representing the number of seconds or milliseconds elapsed since the Unix epoch (January 1, 1970, 00:00:00 UTC). For example, in Anqi CMS, such as articles,CreatedTimeorUpdatedTimeComments, reviews are provided in this form.CreatedTimeAnd, are provided in this form.
  2. format ("格式")This is the string defining the date and time display format. The format string used by Anqi CMS follows the Go language's date formatting rules, and it is not the traditionalYYYY-MM-DDSuch a placeholder, but instead of using a special 'reference time' to define the layout.

Understanding the time formatting reference time in the Go language.

The time formatting in Go language is quite unique, it uses a fixed reference time:2006年01月02日 15时04分05秒(corresponding)2006-01-02 15:04:05.000000000 -0700 MST)

You can replace the part you want to display with the corresponding number in this reference time. For example:

  • If you want to displayYear,English it2006.
  • If you want to displayMonth,English it01(Leading zero included)or1(Leading zero excluded).
  • If you want to displayDate,English it02(Leading zero included)or2(Leading zero excluded).
  • If you want to displayHour,English it15(24小时制)或03(12-hour clock,requires companionPM/AM).
  • If you want to displayminutes,English it04.
  • If you want to displayseconds,English it05.

By combining these numbers, you can construct various complex date and time formats.

Common formatting examples:

  • "2006-01-02"Is displayed as2023-03-15
  • "2006年01月02日"Is displayed as2023年03月15日
  • "15:04:05"Is displayed as14:30:00
  • "2006/01/02 15:04"Is displayed as2023/03/15 14:30
  • "Mon, 02 Jan 2006"Is displayed asWed, 15 Mar 2023

Actual application scenarios: Display the backend timestamp on the frontend

In the template development of AnQi CMS, we often encounter scenarios where we need to display timestamps.For example, the publishing and updating time of the article detail page, the entry time in the article list, the time of each comment in the comment list, and even the time of user registration or the last login time.

For example, taking an article list, assuming we want to display the article title and its publication time, update time:

{% archiveList archives with type="list" limit="5" %}
    {% for item in archives %}
    <div class="article-item">
        <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
        <p>发布时间:{{ stampToDate(item.CreatedTime, "2006年01月02日 15:04") }}</p>
        <p>更新时间:{{ stampToDate(item.UpdatedTime, "2006/01/02") }}</p>
        <p class="description">{{ item.Description }}</p>
    </div>
    {% endfor %}
{% endarchiveList %}

In this code block:

  • item.CreatedTimeanditem.UpdatedTimeThe timestamp is obtained from the backend.
  • stampToDateThe label receives these timestamps.
  • firststampToDateUse"2006年01月02日 15:04"Format, converts the timestamp to a Chinese display of “year, month, day, hour, minute”.
  • secondstampToDateUse"2006/01/02"Format, convert the timestamp to a concise display of "year/month/day

This way, even if the backend returns a string of numbers, the frontend can present it to the user in a clear and beautiful way according to the preset format.

Advanced hint: Distinguish between time types and tags

The template engine of Anqi CMS also providesdateandtimeFilters, but please note that these filters are mainly used for processing Go language intime.TimeType (an object representing dates and times), not direct handling of raw numeric timestamps. If you try to pass a numeric timestamp todateortimea filter, you are likely to encounter an error.

Therefore, when you are sure that the data source is a "timestamp", you must definitely usestampToDatetags, which are created for this purpose.

If you need to displaythe current time,instead of the specific event time stored in the database, the security CMS also provides{% now "格式" %}tags. For example, to display the current year in the footer:{% now "2006" %}

Summary

PassstampToDateLabel, you can easily convert the timestamp stored on the backend to a user-friendly date and time format, greatly enhancing the readability and user experience of the website content.Proficient in Go language reference time format will be the key to efficient template development in Anqi CMS.stampToDateCan meet all your needs, ensure that the date and time are displayed correctly on the front end.


Common Questions (FAQ)

1. Why do I get a string of numbers instead of a date when I use it directly in the template?{{ item.CreatedTime }}Why do I get a string of numbers instead of a date?This is becauseitem.CreatedTimeIt is usually stored in the back-end database in the form of Unix timestamp (Timestamp), which represents the number of seconds from January 1, 1970 to the present.Browser cannot directly recognize it as a readable date format.stampToDateThe label needs to be formatted correctly in order to display as a date and time on the front end.

2. I tried using{{ item.CreatedTime|date:'YYYY-MM-DD' }}a filter, but the page showed an error. Why is that?In the template engine of Anqi CMS,dateThe filter is used to process Go languagetime.Timeobjects, not to process the raw digital timestamp directly. If you want to format the timestamp, you must usestampToDateLabel, and define the date format according to the reference time format of the Go language (such as"2006-01-02") to define the date format, instead of usingYYYY-MM-DDother placeholders.

3. If I only need to display the current time, rather than the content publishing time, which tag should I use?If you want to display the current date or time in the template instead of the timestamp of the specific content retrieved from the database, you can use{% now "格式" %}Tags. For example,{% now "2006年" %}It will display the current year. The format string also follows the reference time rules of the Go language.