In the security CMS, the display of dates and times often needs to be flexibly adjusted according to the actual needs of the website. As we all know,stampToDateThe filter is very convenient for handling Unix timestamps, as it can easily convert a string of numeric timestamps into the date format we need. But sometimes, the date values we process in the template may already be in a Go language format.time.TimeType object, not an original timestamp. In this case, AnQi CMS provides another powerful tool, that isdateFilter (or its aliastimeFilter),it can directly formattime.Timeobjects to provide more precise control.
Understandingtime.Timethe origin of objects in the template
When usingdateBefore the filter, we first need to clarify which data exists in the template. Usually, we would obtain this kind of date and time object from the following places:time.TimeExisting as objects.
- System built-in label returns the date and time field:For example,
archiveDetailthe document addition time obtained from the tagCreatedTimeor update timeUpdatedTimeAlthough the document mentions that they are "timestamp", in some specific scenarios or after internal processing, if they are intime.TimeThe object is passed in the form of a template variable and can be used directlydateFilter. nowThe current time obtained by the tag:{% now "2006-01-02 15:04" %}This tag actually generates a formatted string. But if we want to get an object that can be further processed,time.Timeusually the current time is handled in the backend controller.time.TimeType passed to the template, or directly as time through some auxiliary functions (if the system provides them).time.TimeProvided by the object.- Pass
setAssigned by tags.time.TimeVariable:If you assign a value to a variable of typesetby tag in the contexttime.Timeit can also be processed bydatethe filter.
It is particularly important to note,dateFilter has strict requirements for the input type, itMustreceives onetime.Timeobject. If the input is a common 10-digit or 13-digit Unix timestamp (such as1678886400)dateFilter will throw an error due to type mismatch. At this time, it should usestampToDateFilter.
dateFilter: Free formatting based on Go language layout
dateThe usage of the filter is very intuitive, and it is withstampToDateThe biggest difference is that it does not need to convert the timestamp firsttime.Timean object, because its input is alreadytime.Time. Its syntax is{{ 变量名 | date:"格式字符串" }}.
Here is the Go language's special date-time format string, which is not used like PHP or JavaScriptY-m-d H:i:sSuch placeholder, instead of using a fixed 'magic number'——2006-01-02 15:04:05.999999999 -0700 MSTAs a reference template. You need to replace the corresponding number in this reference template with the format you want.
Let us delve deeper through some examples:
Suppose we have atime.TimeVariables of typemyTimeIts value is:2023年3月15日 下午3点04分05秒.
Display year-month-date:If you want to display
2023-03-15English format string should be"2006-01-02".{{ myTime | date:"2006-01-02" }} {# 输出: 2023-03-15 #}Show date/month/year:If you want to display
15/03/2023English format string should be"02/01/2006".{{ myTime | date:"02/01/2006" }} {# 输出: 15/03/2023 #}Show time (hour:minute):If you want to display
15:04English format string should be"15:04".{{ myTime | date:"15:04" }} {# 输出: 15:04 #}Display the full date and time:If you want to display
2023年03月15日 15时04分05秒English format string should be"2006年01月02日 15时04分05秒".The date formatting of the Go language allows you to insert any text you want between numbers, such as "year{{ myTime | date:"2006年01月02日 15时04分05秒" }} {# 输出: 2023年03月15日 15时04分05秒 #}Display the day of the week and month name:The layout of the Go language also supports displaying the day of the week (Mon, Monday) and the names of the months (Jan, January).
{{ myTime | date:"Mon, January 2, 2006" }} {# 输出: Wed, March 15, 2023 #}Here
Mon对应Wednesday(星期三),January对应March(三月)。
Through these examples, you will find that the date formatting in the Go language is somewhat unique at first glance, but once you master the corresponding relationship of the "magic numbers", it can provide extremely high flexibility, allowing you to display dates and times in almost any format you can imagine.
Some tips in practical application
We may use it like this in practical content operation and template developmentdateFilter:
- Formatting the article's publication time:“`twig
{# 假设 archive.CreatedTime 已经是 time.Time 对象 #}”
}]
Published on:{{ archive.CreatedTime | date:“2006年01月02日” }}
Updated on:{{