In the template development of AnQi CMS, we often need to display the time values stored in the database, such as the publication time and update time of articles, in the date and time format we expect.stampToDateThe filter is undoubtedly one of the most commonly used and powerful tools, which can flexibly convert Unix timestamps into various date formats.But in addition to this 'panacea', Anqi CMS also provides several other highly efficient and practical time formatting methods, each with its own strengths in different scenarios, which can help us handle the display of time data more finely and conveniently.


stampToDate【en】Timestamp conversion: the fundamental and versatile choice.

Firstly, let us confirm again.stampToDate【en】Functionality, as it is the core tool for handling the common 10-digit Unix timestamp in databases. Regardless of where you arearchiveList/archiveDetailorcommentListObtained from the tag in 【en】CreatedTimeorUpdatedTimeThis timestamp field,stampToDateCan all be converted to an easily readable date and time format.

The usage is very intuitive, accepting two parameters: the timestamp value to be formatted, and a string that defines the output format.

{# 假设 publishStamp 是一个10位的时间戳,例如 1609470335 #}
{% set publishStamp = 1609470335 %}

{# 格式化为 2021年06月30日 #}
<div>发布日期:{{stampToDate(publishStamp, "2006年01月02日")}}</div>
{# 格式化为 2021-06-30 15:04:05 #}
<div>发布时间:{{stampToDate(publishStamp, "2006-01-02 15:04:05")}}</div>

This filter is the preferred choice for processing timestamp returns from databases, with its flexibility lying in the ability to customize any output format.


Define the format directly within the label: a quick and convenient option.

For time fields like article publish time (CreatedTimeand update timeUpdatedTime) which are commonly displayed in tags such as this onearchiveDetailorarchiveList, the Anqi CMS provides a more convenient formatting method: directly within the tag.formatParameters can be specified.

This method avoids an additional call.stampToDateThis step, making the template code more concise and readable.

{# 在文档详情页,直接获取并格式化文章创建时间 #}
<div>
    文章发布于:{% archiveDetail with name="CreatedTime" format="2006年01月02日 15:04" %}
</div>

{# 在文档列表中,同样可以为每个文章项格式化时间 #}
{% archiveList archives with type="list" limit="5" %}
    {% for item in archives %}
        <div>
            {{item.Title}} - 发布于:{{item.CreatedTime|format("2006-01-02")}}
        </div>
    {% endfor %}
{% endarchiveList %}

As you can see, when you access inarchiveListorarchiveDetaildirectlyitem.CreatedTimeoritem.UpdatedTimewhen combinedformatfilter, it will automatically format, with the effect ofstampToDateThe same. This can improve development efficiency in many scenarios.


{% now %}Tag: a powerful tool for getting the current time

Sometimes, what we need is not a specific time from the database, but the current server time when the web page is rendered, such as displaying the current year or the exact time down to seconds in the footer. At this time, {% now %}Tags come into play.

It does not depend on any external variables and is responsible for retrieving and formatting the server's real-time time.

{# 在页脚显示当前年份 #}
<div>&copy; {% now "2006" %} All Rights Reserved.</div>

{# 显示完整的当前日期和时间 #}
<div>当前服务器时间:{% now "2006-01-02 15:04:05" %}</div>

This tag is very useful for scenarios that require dynamic display of the current time.


|date(or)|timeFilter: For a specific data type

In addition to the above methods, the CMS also provides|date(or its alias)|time) Filter. This filter is related tostampToDateslightly different, it requires the object to be filtered to be a Go language objecttime.Timetype object, not a pure Unix timestamp.

If you are operating on a template that has already been parsed into Go languagetime.TimeA variable of a type (such as, certain custom Go structs or time objects converted in other ways), then|datefilters are very applicable.

{# 假设 myGoTimeVar 是一个已知的Go time.Time类型变量 #}
<div>自定义时间:{{ myGoTimeVar|date:"2006年01月02日 星期一" }}</div>

{# myGoTimeVar|time 和 myGoTimeVar|date 效果一致 #}
<div>自定义时间:{{ myGoTimeVar|time:"2006/01/02 15:04" }}</div>

Although in most scenarios where timestamps are directly retrieved from a database,stampToDateit is more common, but understanding|dateand|timeThe presence can help you choose more precise tools when dealing with pre-processed Go time objects.


The secrets of string formatting: time layout in Go language

Whether it isstampToDateFilter, within tagsformatParameter, or{% now %}Tags and|dateFilters, all of which follow the same time formatting rule: the reference time format of the Go language.

Go language does not use format specifiers like 'Y-m-d' as PHP does or 'yyyy-MM-dd' as Java does, but uses a specific reference date2006-01-02 15:04:05.999999999 -0700 MST[usually abbreviated as]2006-01-02 15:04:05[as a formatting template]

This means that you need to write the corresponding part of the reference date/time in the format string according to the date/time part you want to display:

  • Year:2006
  • Month:01(with leading zeros),1(without leading zeros),Jan(abbreviation),January(full)
  • Date:02(with)