In AnQiCMS template design, the way data is displayed is crucial to user experience.Especially information such as dates and times, if presented in the original Unix timestamp format, is difficult for ordinary visitors to understand.Fortunately, AnQiCMS provides a very practical filter -stampToDateIt can help us easily convert these machine-readable number sequences into clear and friendly date and time formats.

Understanding Unix timestamp withstampToDateFilter

First, get a basic understanding of what a Unix timestamp is.It is essentially the number of seconds elapsed since 00:00:00 UTC on January 1, 1970.In the database, for storage and calculation efficiency, dates and times are often saved in this format.For example, the release time of the document (CreatedTime) or update time(UpdatedTimeIt is usually in the form of a Unix timestamp.

In the AnQiCMS template, if you output these timestamps directly, you will see a long string of numbers, such as1609470335HoweverstampToDateThe filter acts like a translator, its task is to 'translate' this string of numbers into the date and time format we are accustomed to reading.

UsestampToDateThe basic syntax of the filter is very intuitive:{{stampToDate(时间戳, "格式")}}

There are two key parts:

  1. timestamp: This is the Unix timestamp you want to format, usually a variable in the template (such asitem.CreatedTime). Please note that it needs to be a 10-digit number representing seconds.
  2. Format: This is a string used to tell the filter the specific format you want the date and time to be displayed in.

Master the date formatting magic of the Go language.

stampToDateThe filter uses the date formatting rules specific to the Go language, which is different from PHP'sY-m-dor Python's%Y-%m-%dDifferent. The formatting of Go language is not represented by letter codes for year, month, and day, but by a fixed reference time2006年01月02日15时04分05秒With time zone and milliseconds. Just remember the parts of this 'magic number' and then replace them with the format you want.

Let us understand this format through some common examples:

  • Only show the year: The year in the reference time is2006Replace2006. For example:{{stampToDate(publishStamp, "2006")}}-u003e2021
  • Show year-month-date: The reference time is2006-01-02Replace2006-01-02. For example:{{stampToDate(publishStamp, "2006-01-02")}}-u003e2021-06-30
  • Show year/month/day: The reference time is2006-01-02Replace2006/01/02. For example:{{stampToDate(publishStamp, "2006/01/02")}}-u003e2021/06/30
  • Show a date with Chinese: The reference time is2006-01-02Replace2006年01月02日. For example:{{stampToDate(publishStamp, "2006年01月02日")}}-u003e2021年06月30日
  • Show hour:minute:second: The reference time is15:04:05Replace15:04:05. For example:{{stampToDate(publishStamp, "15:04:05")}}-u003e12:00:00
  • Show the full date and time: The reference time is2006-01-02 15:04:05Replace2006-01-02 15:04:05. For example:{{stampToDate(publishStamp, "2006-01-02 15:04:05")}}-u003e2021-06-30 12:00:00

The key is that you need to remember2006-01-02 15:04:05This skeleton, then according to the format you need to “assemble” a new format string. For example, if you want月/日/年the format, you write01/02/2006.

stampToDateactual application

In AnQiCMS, you will often use date and time display in places like article lists, article details, comment lists, etc. For example, when you traverse a document list (archiveListWhen each document item (itemusually includesCreatedTime(creation time) andUpdatedTimeThe (update time) field. These fields usually store Unix timestamps.

Suppose you are designing an article list page, hoping to display the publication date of each article:

{# 假设 publishStamp 是一个Unix时间戳变量,例如 1609470335 #}
{% set publishStamp = 1609470335 %}

<p>格式化为 2021年06月30日: {{stampToDate(publishStamp, "2006年01月02日")}}</p>
<p>格式化为 2021-06-30: {{stampToDate(publishStamp, "2006-01-02")}}</p>
<p>格式化为 2021/06/30 12:00:00: {{stampToDate(publishStamp, "2006/01/02 15:04:05")}}</p>
<p>格式化为 30/06/2021: {{stampToDate(publishStamp, "02/01/2006")}}</p>
<p>格式化为 12:00:00: {{stampToDate(publishStamp, "15:04:05")}}</p>

It will be like this in a loop of article lists:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <article>
        <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
        <p>
            发布时间:{{stampToDate(item.CreatedTime, "2006年01月02日 15:04")}}
            <span class="views">阅读量:{{item.Views}}</span>
        </p>
        <p>{{item.Description}}</p>
        <a href="{{item.Link}}" class="read-more">阅读更多</a>
    </article>
    {% empty %}
    <p>暂时没有文章。</p>
    {% endfor %}
{% endarchiveList %}

In this code block,item.CreatedTimeIt is a Unix timestamp,stampToDateThe filter converts it into年-月-日 时:分which makes the date display of the article list more friendly and readable.

Points to note

  • The reference time in Go language is crucial: Always in2006-01-02 15:04:05Use it as a baseline to construct your format string. Remember that this is not a custom placeholder, but a specific date value in the Go language.
  • The number of bits in a Unix timestamp.:stampToDateThe filter expects to receive a 10-digit Unix timestamp (second level). If your timestamp is 13 digits (millisecond level), you need to divide it by 1000 first, for example{{stampToDate(item.CreatedTime / 1000, "格式")}}.
  • Avoid usingdateFilter confusion: AnQiCMS template may still contain a nameddate.dateFilters are typically used to format the native Go language'stime.TimeThe type is an object, not the raw Unix timestamp. If you pass a Unix timestamp