In AnQiCMS, managing website content often involves displaying date and time, such as the publication time, update time of articles, or the last login time of users, etc. These time data are usually handled in the GoLang backend.time.TimeType storage.When they are passed to the front-end template without proper formatting, they may be displayed as a string of difficult-to-understand timestamps or the default Go language format, which clearly does not meet our needs for displaying to visitors.

幸运的是,AnQiCMS 为我们提供了强大而灵活的模板标签和过滤器,可以轻松地将这些 GoLang 的time.TimeThe variable is formatted to any date string format we want.

core tools:stampToDateFormat labels

AnQiCMS's template engine supports syntax similar to Django, with a very useful tag specifically for formatting timestamps (or, those numeric representations of time passed from the backend). This tag isstampToDate.

Its basic usage is:{{ stampToDate(时间戳变量, "格式化字符串") }}.

The key here lies in the second parameter: "format string".AnQiCMS uses the unique "reference time" defined by GoLang in English.2006-01-02 15:04:05.999999999 -0700 MST。You do not need to remember this long string, just understand what each part represents:.

  • 2006:Represents the year ()YYYY)
  • 01:Represents the month ()MMfor example01Represents January
  • 02:Represents the date (DDfor example02represents the second day of each month
  • 15: represents hours (24-hour clock,)HHfor example15represents 3 PM
  • 04: represents minutes (mmfor example04represents 4 minutes
  • 05:represents seconds(ssfor example05represents 5 seconds

This means that if you want to format the time in the form of “year-month-day”, you just need to write in the formatting string"2006-01-02"English. If you want to display "month/day/year Time:minute", you can write it as"01/02/2006 15:04".

Let's look at some real examples to understand its usage:

  • Display date and time: {{ stampToDate(archive.CreatedTime, "2006-01-02 15:04:05") }}It may be displayed as2023-10-27 10:30:45.
  • Only show the date: {{ stampToDate(archive.CreatedTime, "2006年01月02日") }}It may be displayed as2023年10月27日.
  • American date format: {{ stampToDate(archive.CreatedTime, "Jan 02, 2006") }}It may be displayed asOct 27, 2023.
  • Only show time (hour:minute): {{ stampToDate(archive.CreatedTime, "15:04") }}It may be displayed as10:30.

Practical Application: Obtain time from documents and user data

In AnQiCMS, many places involve time data. For example, when displaying the details of an article,archive.CreatedTimeandarchive.UpdatedTimeGet the creation and update time. These variables are usually provided in the form of timestamps and can be passed directlystampToDateLabel formatting.

Assuming we are editing the template of the article detail page, we can display the publication and update time of the article like this:

<p>发布于:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}</p>
<p>最后更新:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04") }}</p>

In addition to the article, the user module also contains time information, such as the user's last login timeuser.LastLoginor VIP expiration timeuser.ExpireTime. You can also usestampToDateto format these data:

<p>上次登录时间:{{ stampToDate(user.LastLogin, "2006-01-02") }}</p>
<p>VIP 过期日期:{{ stampToDate(user.ExpireTime, "2006年01月02日") }}</p>

If you need to display the current time or year, AnQiCMS also provides{% now %}tags, whose usage is similar tostampToDatethe formatted part:

<p>版权所有 © {% now "2006" %}</p>
<p>当前时间:{% now "2006-01-02 15:04:05" %}</p>

Be careful to distinguish:stampToDateWithdateFilter

In AnQiCMS templates, you may also see a filter nameddate. This filter can also format time, but it has an important distinction withstampToDate.

  • stampToDate:专门用于处理Timestamp(Typically 10 digits or 13 digits, representing seconds or milliseconds since the Unix epoch).This is the method most often used to format dates in the AnQiCMS template, because the backend will convert the date field in the database to a timestamp and pass it to the frontend.
  • dateFilter:用于格式化 GoLang 原生的time.Time Object。如果模板变量本身就是一个 GoLangtime.TimeType (instead of a simple numeric timestamp), then you can use{{ 变量 | date:"格式化字符串" }}this syntax.

Important reminder:In most cases, when we get data tags from AnQiCMS (such asarchiveList/archiveDetailetc).CreatedTimeorUpdatedTimeWhen the time field is equal, they will be provided in the form ofTimestampTherefore, you should prioritize usingstampToDatetags. If the timestamp is mistakenly passed todatefilter, errors may occur during template parsing.

By using flexibilitystampToDateLabels, combined with GoLang's precise formatting strings, you can elegantly display any date and time information on the AnQiCMS website, making your website content more readable and professional.


Common Questions and Answers (FAQ)

1. WhystampToDateThe format string looks so strange, it uses '2006-01-02 15:04:05' such a number combination instead of 'YYYY-MM-DD'??

This is the GoLang language-specific date and time formatting method, which uses a fixed 'reference time'2006-01-02 15:04:05.999999999 -0700 MSTAs a format pattern. You do not need to remember the specific date, just understand the meaning of each number in this reference time.2006Represents the year,01Represents the month,02represents the date,15represents the hour (24-hour clock),04represents the minutes,05Represents seconds