In AnQiCMS template development, the display of time data is an indispensable part, whether it is the publication time of articles, the update date of products, or the time records of user behavior, they all need to be presented to the user in a clear and readable manner. To meet different time processing needs, AnQiCMS provides two core filters:dateandstampToDateAlthough they can all help us format time, there is a fundamental difference in the data types they handle at the bottom level. Understanding these differences is crucial for avoiding template errors and efficient development.
AnQiCMS time data overview
In the AnQiCMS content management system, time data usually exists in two main forms: one is stored directly as a standard date and time objecttime.TimeType, this is the data structure used to represent 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.TimeThe elegant presentation of objects
dateThe filter is used to process those that have been parsed as Go language primitivestime.TimeType of time data. Its main function is to output the structured date and time object in a specified format for easy understanding.
While usingdateWhen filtering, you just need totime.Timepass the variable of the type through the pipe|Connected todateApply the filter and provide a time formatting string that conforms to the Go language standard.
For example, suppose you have a variable namedcreateTimevariable, which 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 a timestamp is passed as an integer, it will not be recognized and may cause template rendering errors. In AnQiCMS,datethere is also an aliastime, and the function is withdatebeing exactly the same.
stampToDateFilter: Professional Timestamp Converter
withdateThe filter is different,stampToDateThe filter is specifically designed to handle Unix timestamps. Many built-in time fields in AnQiCMS, such as the document'sCreatedTime(creation time) andUpdatedTime(Update 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 a bit 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, if you want to list the articlesitem.CreatedTimeTo format the 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.TimeAn object is then formatted, so that it can perfectly handle the display of most built-in time fields in AnQiCMS.
Guide to Core Differences and Selection
Now, let's clarify.dateandstampToDateThe core difference, so you can make the correct choice during development:
Input data type:
dateFilter: Expected to receiveGo languagetime.Timeobject.stampToDateFilter: Expected to receiveUnix timestamp in integer form.
Applicable scenarios:
- Use
dateFilterWhen your template variable is explicitly atime.Timeobject (for example, you have already processed the time data in your Go backend code intotime.Timeand passed it to the template). - Use
stampToDateFilterWhen your template variable is an integer timestamp, this is very common in AnQiCMS, such asitem.CreatedTime/archive.UpdatedTimeetc.
- Use
In simple terms, if you are unsure but the variable name suggests that this is a "timestamp" (for example, with "Stamp", "Time", etc.), thenstampToDateIt is often a safer and more correct choice.
The mystery of Golang time formatting strings.
whether it isdateOrstampToDateThey follow the Go language's unique time formatting rules. 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.
For example:
"2006-01-02"Indicates年-月-日"15:04:05"Indicates时:分:秒"2006年01月02日 15时04分"Indicates2006年01月02日 15时04分
Mastery of this rule allows you to control the time display format at will.
**Practice and Precautions
- **Specify the data source clearly.