In the AnQi CMS, format timestamps into readable dates: a practical guide

When managing website content, we often encounter the need to display article publish time, update time, user registration time, and other information.This data is usually stored in the database as a series of numbers, that is, in the form of 'timestamp'.Although computers can easily understand these numbers, they are not very user-friendly for the visitors of the website.For example, seeing the number

It is fortunate that AnQi CMS provides a very convenient and powerful template tag, which helps us easily convert these timestamps into various readable date and time formats.

Understanding Core Tools:stampToDatetags

The template engine of Anqi CMS adopts a syntax style similar to Django, one very practical built-in tag beingstampToDate. It is specifically used for formatting and displaying timestamps.

This label's basic usage is very intuitive:{{stampToDate(时间戳, "格式")}}

Let's take a detailed look at these two parameters:

  1. Timestamp (the first parameter)This is the original timestamp you want to convert. In AnQi CMS templates, we often get data objects (such as document lists),itemor document details.archive)'s specific field to obtain the timestamp, for exampleitem.CreatedTime(Creation time) orarchive.UpdatedTime(Update time). These fields usually store a 10-digit Unix timestamp (in seconds).

  2. format (second parameter)This is the specific format you want the date and time string to display. There is an important point to note: the template engine of Anqi CMS follows the Go language's date formatting rules, rather than the use of many other systems.Y-m-dorYYYY-MM-DDSuch placeholder.

    Go language uses a fixed "reference time" to define the format.You only need to write this reference time in the display format you want, and the system will automatically convert it.2006年01月02日 15时04分05秒English2006-01-02 15:04:05)

    English2006;To display the month, write01English02. Continue in this manner.

English

Format to be displayed Go language format string Example output (assuming timestamp is1678886400means2023-03-15 10:00:00 UTC+08:00)
Year-Month-Day 2006-01-02 2023-03-15
Year, Month, Day (Chinese) 2006年01月02日 2023年03月15日
Hour:Minute:Second 15:04:05 10:00:00
Year-Month-Day Time: minute 2006-01-02 15:04 2023-03-15 10:00
Year/Month/Day Hour:Minute:Second 2006/01/02 15:04:05 2023/03/15 10:00:00
Month/Day/Year (US style) 01/02/2006 03/15/2023
Week, Day-Month-Year Hour:Minute Monday, 02-Jan-06 15:04 Wednesday, 15-Mar-23 10:00

Actual application example

Now, let's takestampToDateLabel applied to specific template code.

  1. Display the publish date in the document list.Assuming you are iterating over a document list (archives) and want to display the creation time of each article.

    {% archiveList archives with type="list" limit="10" %}
        {% for item in archives %}
        <li>
            <a href="{{item.Link}}">{{item.Title}}</a>
            {# 显示格式为“2023-03-15” #}
            <span>发布于:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
            {# 显示格式为“2023年03月15日 10:00” #}
            <span>更新于:{{stampToDate(item.UpdatedTime, "2006年01月02日 15:04")}}</span>
            <span>阅读量:{{item.Views}}</span>
        </li>
        {% endfor %}
    {% endarchiveList %}
    
  2. Display detailed time on the document detail pageYou can directly use it on the detail page of a single documentarchive.CreatedTimeorarchive.UpdatedTime.

    <article>
        <h1>{{archive.Title}}</h1>
        <div>
            <span>发布日期:{{stampToDate(archive.CreatedTime, "2006年01月02日 15时04分05秒")}}</span>
            <span>最后更新:{{stampToDate(archive.UpdatedTime, "2006/01/02 15:04")}}</span>
            <span>浏览次数:{{archive.Views}}</span>
        </div>
        <div class="content">
            {{archive.Content|safe}}
        </div>
    </article>
    

Through these examples, you can seestampToDateThe flexibility and ease of use of the label. Just adjust the format string, and the timestamp can be displayed in the way you want.

AboutdateDescription of the filter

When using the AnQi CMS template, you may also notice