In the template development of AnQiCMS, we often encounter the need to convert the timestamp data stored in the background into a clear and readable date and time format on the user interface. For example, the original timestamp, such as1609470335It is meaningless to ordinary users.To enhance the user experience of the website, it is particularly important to display time information appropriately.stampToDateIt can help us easily achieve this goal.

Core Function:stampToDateFilter introduction

stampToDateThe filter is a tool used specifically for timestamp formatting in the AnQiCMS template engine.Its main function is to convert a sequence of numeric timestamps into easily understandable date and time strings according to the format you specify.stampToDateAll of them can provide flexible and powerful support.

Master the usage method: easily format timestamps

stampToDateThe filter usage is very intuitive, it requires two key parameters:

  1. TimestampThis is usually a 10-digit integer representing the number of seconds from the Unix epoch (January 1, 1970, 00:00:00 UTC) to the present. In AnQiCMS templates, this timestamp usually comes from a database field, such as the article object'sCreatedTimeorUpdatedTime.

  2. Format StringThis is the key to decide the date and time display style. Since AnQiCMS is developed based on the Go language, the format string here follows the unique style of the Go language.参考时间Rule. The reference time in Go language is:2006-01-02 15:04:05 -0700 MST.

    To understand this format string, you need to consider Go's reference time as a 'template'.What do you want to display in the final output, use the corresponding part of the reference time to represent it.

    • 2006Represents the year
    • 01represents the month
    • 02represents the date
    • 15Represents hours (24-hour format)
    • 04Represents minutes
    • 05Represents seconds

    For example, if you want to display "year-month-day", you can use:"2006-01-02"as the format string.

The basic syntax structure is:{{ stampToDate(时间戳变量, "格式字符串") }}

Let's go through an example timestamp1609470335English2021-01-01 00:25:35English

English

假设我们有一个时间戳变量publishStampwith the value1609470335.

  • Show date only (Year-Month-Day)English"2006-01-02"as the format string.

    <div>发布日期:{{ stampToDate(publishStamp, "2006-01-02") }}</div>
    {# 输出: 发布日期:2021-01-01 #}
    
  • English: If you want the date to be displayed in Chinese, just add Chinese characters to the format string.

    <div>发布日期:{{ stampToDate(publishStamp, "2006年01月02日") }}</div>
    {# 输出: 发布日期:2021年01月01日 #}
    
  • Display the full date and timeTo display minutes and even seconds, you can add reference numbers for hours, minutes, and seconds.

    <div>发布时间:{{ stampToDate(publishStamp, "2006-01-02 15:04:05") }}</div>
    {# 输出: 发布时间:2021-01-01 00:25:35 #}
    
  • Custom delimiter: You can change the separator between year, month, and day according to your needs, for example, using a slash.

    <div>日期:{{ stampToDate(publishStamp, "2006/01/02") }}</div>
    {# 输出: 日期:2021/01/01 #}
    
  • Only show time: If you only want to display the time part, you can combine it like this.

    <div>精确到分钟:{{ stampToDate(publishStamp, "15:04") }}</div>
    {# 输出: 精确到分钟:00:25 #}
    

In AnQiCMS templates, we often get timestamps from the article objects obtained by tags. For example, the creation time of an article can be obtained througharchiveListorarchiveDetailitem.CreatedTimeorarchive.CreatedTimeto get.

{# 在文章列表中,显示每篇文章的创建时间 #}
{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <div>
        <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
        <p>发布于:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}</p>
    </div>
    {% endfor %}
{% endarchiveList %}

{# 在文章详情页,显示文章的更新时间 #}
{% archiveDetail archive with name="CreatedTime" %}
    <p>最近更新:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04") }}</p>
{% endarchiveDetail %}

Advanced Techniques: The Origin and Application of Timestamps

Many data models of AnQiCMS include timestamp fields, such as:

  • Document (Archive):CreatedTime(Creation Time)、UpdatedTime(Updated Time)
  • Category (Category):通常不直接包含时间戳,但可以获取其下最新文章的时间。
  • Label (Tag):同样不直接包含时间戳。

When you are usingarchiveList/archiveDetailWhen looping or fetching content with tags, these timestamp fields will be returned along with the data. You just need to pass them as thestampToDatefirst parameter.

{# 假设在一个循环中 item 代表一个文章对象 #}
<span>{{ stampToDate(item.CreatedTime, "2006/01/02") }}</span>

This method ensures that no matter where the data comes from, as long as it is a standard Unix timestamp,stampToDateit can be formatted uniformly and accurately.

Summary

stampToDateThe filter is a key tool in AnQiCMS template for handling timestamps, converting complex numbers into user-friendly date and time expressions. It provides a reference time template in Go language.2006-01-02 15:04:05 -0700 MSTYou can flexibly customize any date and time format you need. Mastering this filter will significantly enhance the readability and professionalism of your website content.


Common Questions (FAQ)

问:Why is my time formatted output?0001-01-01or1970-01-01?答:This usually means that you have providedstampToDateThe timestamp of the filter is an invalid value (such as 0, null, an empty string) or not properly obtained. In Unix timestamp, 0 represents January 1, 1970, and in Go language,time.TimeThe value is January 1, 2000. Please check if the timestamp variable you passed is empty or contains the correct 1