As an experienced website operation expert, I know that accurate display of time information is crucial for user experience and SEO optimization.In the powerful template system of AnQiCMS, we often encounter scenarios where we need to format and output time data.stampToDateanddateThese two seemingly similar tools will come to mind.However, there is a core difference between them, understanding this difference is crucial for efficient and error-free template development.

stampToDate:Time stamp's exclusive translator

First, let's talk aboutstampToDate. In the template tag system of Anqi CMS,stampToDateIt is designed as a dedicated label for formatting timestamps. This means that when you get aUnix timestampwhenstampToDateit is your preferred tool.

Many core data of Anqi CMS, such as the creation time of articles (CreatedTimeand update timeUpdatedTimestampToDateCan translate it into various date and time formats we are accustomed to read.

Its usage is very intuitive:{{stampToDate(时间戳, "格式")}}。Where “timestamp” is that number, and “format” is the reference time format string unique to the Go language, such as2006-01-02 15:04:05This format string looks a bit special, but it is a clever design used by the Go language to define various date and time output patterns in English.

For example, if you want to display the publication date of the article on the article detail page, and the backenditem.CreatedTimereturns a timestamp, then you would write it like this:<span>发布日期:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</span>This is,1675881600such a timestamp will be elegantly converted to2023年02月09日.

dateFilter:time.TimeThe standard companion of the object

The opposite of itdateA filter, which exists as a standard filter built into the security CMS template engine (Pongo2, similar to Django template engine).dateThe function of the filter is also to format time, but it has a key prerequisite: it requires the input value to be anative Go languagetime.Timetype of dataIt is rather than the Unix timestamp we are accustomed to.

This is like having a piece of paper in your hand with the number '1675881600' written on it,dateThe filter requires a date object that is already precisely printed with “2023-02-09 00:00:00 UTC”. If you try to pass a pure Unix timestamp todateFilter, the template engine will report an error due to type mismatch, resulting in the failure of page rendering.

dateThe way to use the filter is{{ value|date:"格式" }}Here,valuemust betime.Timeobject. For example, if your Go backend code has already converted the timestamptime.Timeobject and passed it to the template, you can usedatea filter to format it:<span>活动开始:{{ event.StartTime|date:"2006/01/02 15:04" }}</span>

core distinction and practice selection

In short,stampToDateanddateFiltersThe core difference lies in the input data types they accept.

  • stampToDateSpecifically handlesNumeric Unix timestamps.
  • dateFilters specifically handlenative Go languagetime.TimeObject.

In the daily template development of Anqi CMS, due to the content model ofCreatedTime/UpdatedTimefields usually provided in the form of timestamps, thereforestampToDateThe usage frequency of tags will be higher, and it will also be more in line with the way we directly operate backend dataIt avoids additional data type conversions in the template layer, keeping the template code concise and efficient.

whiledateFilters are more suitable for the following scenarios:

  1. You are processing the data that is directly passed through the Go backend controller and has been formatted astime.Timevariable of the object.
  2. Your data source is not the content model built-in to the Anqi CMS, but obtained from elsewhere, and is itselftime.Timetime data of a certain type.

Therefore, when we think about “how to format time”, the first thing to clarify is what the “original form” of the data in your hand is. Is it the original numeric timestamp, or is it already parsed?time.TimeIs it an object? Make sure of this, and you can easily choose the most suitable template tool to ensure the accurate display of time information.


Common Questions (FAQ)

1. Why does APT CMS not directly provideCreatedTimeobjects, but timestamps instead, is that not possible to use directly?time.TimedateFilter?

This is mainly based on several considerations.Firstly, Unix timestamp is more efficient and general in database storage, facilitating cross-system and cross-language data exchange.stampToDateThis provides a simple template tag for formatting. In the Go backend, convert the timestamp totime.TimeThe object is simple, but directly providing a timestamp is more flexible in some scenarios and avoids unnecessary memory overhead and type conversion.stampToDateThe label is designed to facilitate direct processing of this common data type by template developers.

2. I can convert a timestamp in the Anqi CMS template.time.TimeType, then usedateFilter?

Not allowed.The template engine (Pongo2) of AnQi CMS is mainly used for rendering and displaying data, rather than performing complex programming logic or data type conversion.time.TimeObject, this is an operation at the Go language level, which needs to be completed in the backend controller or business logic, and then the convertedtime.TimeObject is passed to the template. If you have a timestamp directly in the template, the correct approach is to usestampToDateLabel formatting.

3. If I forget the reference time format in Go language (such as2006-01-02 15:04:05What are some convenient memory methods or reference channels?

The reference time format of Go language is indeed unique. The memory method is: "Month Day Hour Minute Second Year", that is01/02 03:04:05 2006can be understood as "January 2, Wednesday, 3:04:05 PM, 2006"}]This format is actually a specific date when the Go language was born (January 2, 2006, at 3:04:05 PM), using these numbers to represent their corresponding date and time components.If you really can't remember, you can always check the development documentation of AnQi CMS or search for "Go time format" in the Go language community, and you can find a detailed comparison table soon.In practical development, maintaining a memo of common formats is also a good habit.