In AnQiCMS template, processing timestamps and displaying them in a specific date format is a common requirement in content operation.No matter whether you need to display the publication time of an article or the update date of a product, understanding how to flexibly format time will greatly enhance the presentation of website content.AnQiCMS provides simple and powerful template tags, making this process easy and convenient.

Time processing mechanism in AnQiCMS

The AnQiCMS template engine is designed to be similar to Django template syntax, it uses standard Unix timestamps as the internal storage format for time processing, and provides special tags for formatting in the template layer. This means that the time usually obtained from the database or system internally is typically a 10-digit integer (for example1678886400It represents the number of seconds from 1970-01-01 00:00:00 UTC to the present.

To convert this original timestamp into a date and time that is easy to understand in our daily lives, AnQiCMS provides a namedstampToDateThe tag is at the core of its support for Go language time formatting rules, understanding this is the key to mastering time formatting.

stampToDateUsage method of the tag

stampToDateThe basic syntax of tags is very intuitive:

{{stampToDate(时间戳, "格式")}}

There are two important components:

  1. Timestamp (Timestamp)This is a 10-digit integer representing the time point you wish to format. In the AnQiCMS template, you will usually start fromarchive/itemOr obtain these timestamp fields from other data objects, for examplearchive.CreatedTimeoritem.UpdatedTime.
  2. Format (Format String)This is a string that defines the specific style of timestamp formatting. AnQiCMS follows the special reference time of the Go language.2006-01-02 15:04:05This group of seemingly random numbers and time actually has special meanings in Go language, they correspond to specific time units:
    • 2006Represents the year
    • 01Represents the month
    • 02Represents the date
    • 15representing hours (24-hour system)
    • 04representing minutes
    • 05representing seconds
    • (such as otherPMrepresenting AM/PM,MSTrepresenting time zones, etc.)

You just need to use the date and time format you expect, using2006-01-02 15:04:05this reference time to 'simulate' it. For example, if you want to display年-月-日then the format string is"2006-01-02".

Get the timestamp and format it in the template

In the AnQiCMS template, you can usearchiveDetail/archiveListTags get the creation or update time of articles, products, and other content. These time fields are usually inCreatedTime(Creation Time) orUpdatedTime(Update time) Naming, their values are 10-digit timestamps.

Let's see how to apply it with an example of an article detail page:

Suppose you are on the article detail page (archiveDetail) and you want to display the publication date of the article:

{# 假设当前页面是文章详情页,直接通过 archive.CreatedTime 获取时间戳 #}
<p>发布日期:{{stampToDate(archive.CreatedTime, "2006年01月02日")}}</p>

If you are on an article list page (archiveListIn the loop, the timestamp of each article item can be obtained byitem.CreatedTimeObtain:

{% 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 15:04")}}</p>
        <p>{{item.Description}}</p>
    </div>
    {% endfor %}
{% endarchiveList %}

Practical cases and common scenarios

According to different display requirements, you can flexibly adjust the format string:

  1. Show year, month and date only:

    <p>发布日期:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</p>
    {# 或者更简洁的横杠分隔 #}
    <p>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</p>
    
  2. Show full date and time (to the second):

    <p>更新时间:{{stampToDate(item.UpdatedTime, "2006-01-02 15:04:05")}}</p>
    
  3. Show month and date only:

    <p>日期:{{stampToDate(item.CreatedTime, "01月02日")}}</p>
    
  4. Show hours and minutes only:

    <p>时间:{{stampToDate(item.CreatedTime, "15:04")}}</p>
    
  5. Custom separator or text: You can add any text or symbol to the format string.

    <p>发布于:{{stampToDate(item.CreatedTime, "发布于 2006-01-02")}}</p>
    

What are the precautions

  • Precision of timestamps:stampToDateThe label expects to receive a 10-digit Unix timestamp (second level).If your timestamp is in milliseconds (13 digits), you may need to process it first, such as dividing by 1000 to convert it to a second-level timestamp.
  • Characteristics of time format in Go languagePlease remember the reference time of the Go language2006-01-02 15:04:05All format strings should be built around this date and time, not usingYYYY-MM-DDSuch a general placeholder. For example, to represent a year, you must use2006instead ofYYYY.
  • withdateThe difference between filters: AnQiCMS template also supports onedatefor example, a filter (such as{{ value|date:"2006-01-02" }}), but it is designed to handle Go language intime.TimeType of data, not the original 10-digit timestamp. For fromarchive.CreatedTimePlease make sure to use the original timestamp obtained fromstampToDate.

By mastering the use ofstampToDateThe label and Go language formatting rules allow you to easily present beautiful, clear, and demand-compliant time information on the AnQiCMS website, thereby enhancing user experience and the readability of content.


Frequently Asked Questions (FAQ)

Q1: Why instampToDatemust be used2006-01-02 15:04:05as a format reference instead of how other systems doYYYY-MM-DD?

A1: This is the unique design of the Go language. The Go language does not use abstract placeholders likeYYYY-MM-DDbut uses a specific reference date and time2006-01-02 15:04:05It uses each number and symbol in this date and time to represent the corresponding time unit, such as2006represents the year,01Represents the month).Just use this reference date to "build" the display format you want.

Q2: If my timestamp is not 10 digits (for example, it is a 13-digit millisecond timestamp),stampToDatecan the tags still work?

A2:stampToDateThe tag expects to receive a 10-digit Unix timestamp (second level).If the timestamp you have is 13 digits (millisecond level), using it directly may cause formatting errors or incorrect display.{{ stampToDate(item.CreatedTime / 1000, "2006-01-02") }}This form, depending on whether AnQiCMS supports simple mathematical operations within templates, is converted to a 10-digit second timestamp.

Q3: Can I display the current date and time in the template in addition to the content publishing time?

A3: Yes, AnQiCMS provides{% now %}Labels to display the current date and time. Its usage is withstampToDateThe formatting rules are similar and follow the Go language's reference time format. For example, to display the current year, you can use{% now "2006" %}.