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

Understand Unix timestamp withstampToDateFilter

First, let's understand what a Unix timestamp is.It is essentially the number of seconds elapsed since 00:00:00 UTC (Coordinated Universal Time) on January 1, 1970.In databases, dates and times are often stored and calculated in this format for efficiency.CreatedTime) or update time (UpdatedTime)usually exists in the form of Unix timestamp.

In AnQiCMS templates, if you output these timestamps directly, you will see a long string of numbers, like1609470335.stampToDateThe filter acts like an interpreter, 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, which is usually a variable in a template (such asitem.CreatedTime)。Please note that it needs to be a 10-digit number representing seconds.
  2. FormatThis is a string that tells the filter the specific format you want the date and time to be displayed.

Master the date formatting magic of the Go language.

stampToDateThe filter uses the date formatting rules unique to the Go language, which is unlike PHP'sY-m-dor Python's%Y-%m-%dDifferent. The formatting of Go language is not represented by alphabetic codes for year, month, and day, but by a fixed reference time -2006年01月02日15时04分05秒, including time zone and milliseconds. You just need to remember the various 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 from the reference time will be referred to as2006with2006.{{stampToDate(publishStamp, "2006")}}-2021
  • Show year-month-date: The reference time will be referred to as2006-01-02with2006-01-02.{{stampToDate(publishStamp, "2006-01-02")}}-2021-06-30
  • Show Year/Month/Day: The reference time will be referred to as2006-01-02with2006/01/02.{{stampToDate(publishStamp, "2006/01/02")}}-2021/06/30
  • Show a date with Chinese: The reference time will be referred to as2006-01-02with2006年01月02日.{{stampToDate(publishStamp, "2006年01月02日")}}-2021年06月30日
  • Show Time:Hour:Minute:Second: The reference time will be referred to as15:04:05with15:04:05.{{stampToDate(publishStamp, "15:04:05")}}-12:00:00
  • Show Full Date and Time: The reference time will be referred to as2006-01-02 15:04:05with2006-01-02 15:04:05.{{stampToDate(publishStamp, "2006-01-02 15:04:05")}}-2021-06-30 12:00:00

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

stampToDatePractical application of:

In AnQiCMS, you will often use date and time display in places like article lists, article details, comment lists, etc. For example, when you iterate over a document list (archiveList)时,each document item(item)usually contains(CreatedTime(Creation time) andUpdatedTime(update time)field. These fields usually store the Unix timestamp.

Assuming you are designing a list page for articles, 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>

In a loop of articles, it would be like this:

{% 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,item.CreatedTimeIs a Unix timestamp,stampToDateThe filter converts it to年-月-日 时:分The format makes the date display of the article list more friendly and readable.

Precautions

  • The reference time for the Go language is crucial: Always use2006-01-02 15:04:05As a base to build your format string. Remember that this is not a custom placeholder, but a specific date value in the Go language.
  • Bits of Unix timestamp:stampToDateFilter 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 withdateFilter ConfusionAnQiCMS template may still contain a nameddatefilter.dateFilters are typically used to format the native Go languagetime.TimeType is an object, not an original Unix timestamp. If a Unix timestamp is passed