In AnQiCMS template development, displaying time data is an indispensable part, whether it is the publishing time of articles, the update date of products, or the time records of user behavior, all of which need to be presented to users in a clear and readable manner. To meet different time processing needs, AnQiCMS provides two core filters:dateandstampToDate.Although they can all help us format time, there are fundamental differences in the data types they handle at the bottom level. Understanding these differences is crucial for avoiding template errors and efficient development.

AnQiCMS 中的时间数据概览

In AnQiCMS content management system, time data usually exists in two main forms: one is stored directly as a standard datetime object (time.TimeType, this is the data structure representing time in Go language), another is stored in the form of Unix timestamp (Timestamp), which is usually an integer representing the number of seconds since 00:00:00 UTC on January 1, 1970.Understand the type of your data is the first step in choosing the correct filter.

dateFilter:time.Timeelegant presentation of objects

dateThe filter is used to process those that have been parsed into Go language primitivestime.TimeType of time data. Its main function is to output the structured date and time object in a specified format as a string that is easy to understand.

When usingdateThe filter, you only need totime.Timevariable of type|connected todateand provide a time formatting string that conforms to the Go language standard.

For example, suppose you have a variable namedcreateTimeThe variable, it is atime.Timeobject, if you want to format it as "year-month-day", you can use it like this:

{{ createTime|date:"2006-01-02" }}

The key point is:dateFilterstrict requirementsThe input data must betime.TimeType. If an integer timestamp is passed, it will not be recognized and may cause template rendering errors. In AnQiCMS,datethere is also an aliastime, the function is the same asdateentirely the same.

stampToDateFilter: A professional timestamp converter.

WithdateThe filter is different,stampToDateThe filter is specifically designed for handling Unix timestamps. Many built-in time fields in AnQiCMS, such as the documentsCreatedTime(Creation time) andUpdatedTime(Updated time),the user'sLastLogin(Last login time)etc., are stored in the form of 10-digit (or more) integer timestamps.

stampToDateThe usage of the filter is somewhat different, it is more like a function call, you need to pass the timestamp variable as the first parameter, and the formatted string as the second parameter.

For example, to convert the article list toitem.CreatedTimeTo format this timestamp as “Year-Month-Day”, you need to do this:

{{ stampToDate(item.CreatedTime, "2006-01-02") }}

This filter will first convert the integer timestamp totime.TimeThe object is then formatted, so it can perfectly meet the display needs of the vast majority of built-in time fields in AnQiCMS.

Core Differences and Selection Guide

Now, let's make it cleardateandstampToDateThe core distinction between, so you can make the correct choice during development:

  1. Input data type:

    • dateFilter: Expected to receiveGo language'stime.TimeObject.
    • stampToDateFilter: Expected to receiveInteger form of Unix timestamp.
  2. Applicable scenarios:

    • UsedateFilterWhen your template variable is explicitly atime.Timeobject (for example, you have already processed the time data in your Go backend code andtime.Timepassed it to the template).
    • UsestampToDateFilterWhen your template variable is an integer timestamp, this is very common in AnQiCMS,item.CreatedTime/archive.UpdatedTimeetc.

In simple terms, if you are unsure, but the variable name suggests it is a 'timestamp' (for example, with words like 'Stamp', 'Time', etc.), thenstampToDateIt is often a safer and more correct choice.

The Secrets of Golang Time Formatting Strings

WhetherdateOrstampToDateThey follow the Go language's unique time formatting rules when formatted. Go language does not useY-m-d H:i:sthis common placeholder, but uses a fixed reference time2006-01-02 15:04:05.999999999 -0700 MSTDefine the format. You need to remember this magical 'reference time' and use it to build the format you want.

举例来说:

  • "2006-01-02"representing年-月-日
  • "15:04:05"representing时:分:秒
  • "2006年01月02日 15时04分"representing2006年01月02日 15时04分

Familiarize yourself with this rule, and you can freely control the display format of time.

**Practical Tips and Considerations

  • **Define the data source clearly