In AnQi CMS template development, we often need to display 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 to various date formats.But in addition to this '万能药', Anqi CMS also provides other several highly efficient and practical time formatting methods, which have their own advantages in different scenarios and can help us handle the display of time data more finely and conveniently.


stampToDateTimestamp conversion is the foundation and versatile choice

First, let's confirm againstampToDatefunction, as it is the core tool for handling the common 10-digit Unix timestamp in databases. No matter where you arearchiveList/archiveDetailorcommentListObtained from the tagCreatedTimeorUpdatedTimeThis timestamp fieldstampToDateCan be converted into a readable date-time format.

Its usage is very intuitive, accepting two parameters: the timestamp value to be formatted, and a string defining 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 database return timestamps, with the flexibility of customizing any output format.


Define the format directly within the tag: a quick and convenient option

For time fields like article publish time (CreatedTimeAnd update time(UpdatedTime) which are often displayed inarchiveDetailorarchiveListtags, Anqi CMS provides a more convenient formatting method: directly within the tag throughformatparameter.

This method eliminates the extra callstampToDatesteps, making the template code more concise and easy to read.

{# 在文档详情页,直接获取并格式化文章创建时间 #}
<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 can be seen, when you accessarchiveListorarchiveDetaildirectlyitem.CreatedTimeoritem.UpdatedTimeif used in conjunction withformatA filter that automatically formats, with the same effect asstampToDateThis can improve development efficiency in many scenarios.


{% now %}Label: 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 time to the second in the footer. At this time,{% now %}the tag comes into play.

It does not depend on any external variables, it is responsible for obtaining 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)|time) Filter: for specific data types

In addition to the aforementioned methods, Anqi CMS also provides|date(or its alias|time)Filter. This filter is related tostampToDateslightly different, it requires the filtered object itself to be a Go languagetime.Timetype object, rather than a pure Unix timestamp.

If you are operating on a template that has already been parsed into Go languagetime.TimeA type variable (for example, some custom Go structures or time objects converted in other ways), then|datea filter is 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 databases,stampToDatemore common, but understanding|dateand|timeThe existence can help you choose more precise tools when dealing with pre-processed Go time objects.


The mystery of string formatting: Time layout in Go language

Whether it isstampToDateFilter, tag withinformatParameter, or{% now %}Tags and|dateFilters, they all follow the same time formatting rule: Go language reference time format.

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 as2006-01-02 15:04:05) as the formatting template.

This means, 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 zero,)Jan(abbreviation,)January(full,)
  • Date:02(with)}