In AnQi CMS, the display of dates and times often needs to be adjusted flexibly according to the actual needs of the website. 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 handle in the template may already be in a Go language'stime.TimeType object, not an original timestamp. In this case, Anqi CMS provides another powerful tool, namelydateFilter (or its aliastimeFilter, it can directly act ontime.Timeobjects to customize the format, providing more fine-grained control.
Understandingtime.Timethe source of objects in the template
While usingdateBefore the filter, we first need to clarify which data is in the template in the form oftime.TimeObjects. Usually, we get this kind of date and time objects from the following places:
- The system built-in tag returns the date and time field:For example, in
archiveDetailthe document addition time obtained from the tagCreatedTimeor update timeUpdatedTimeAlthough the document mentions that they are 'timestamps', in some specific scenarios or after internal processing, if they are in the form oftime.TimePass the object in the form to the template variable, and it 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 a processabletime.Timeobject, it is usually in the backend controller to represent the current time intime.TimeType is passed to the template or the time is directly provided as an object through some auxiliary functions (if the system provides them)time.TimeProvided.- By
setTag assignment.time.TimeVariable:If you pass a variable through the context (context) in the templatesetby assigning a tagtime.Timeof a type, then it can also be processed directlydateby the filter.
It should be noted that,dateThe filter has strict requirements for the input type, itit mustreceives atime.Timeobject. If the input is a common 10-digit or 13-digit Unix timestamp (such as1678886400)dateThe filter will fail due to type mismatch. In this case, it should usestampToDatefilter.
dateFilter: Free formatting based on Go language layout
dateThe usage of the filter is very intuitive, it is withstampToDateThe biggest difference is that it does not need to convert the timestamp totime.Timean object because its input is alreadytime.Time. Its syntax is{{ 变量名 | date:"格式字符串" }}.
The format string here is a Go language-specific date and time format, which is not like PHP or JavaScriptY-m-d H:i:sThis 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 into some examples to understand better:
Assuming we have atime.TimeVariable typesmyTimeIt is worth2023年3月15日 下午3点04分05秒.
Display year-month-date:If you want to display
2023-03-15The format string should be:"2006-01-02".{{ myTime | date:"2006-01-02" }} {# 输出: 2023-03-15 #}Display date/month/year:If you want to display
15/03/2023The 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:04The 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秒The format string should be:"2006年01月02日 15时04分05秒". Go language's date formatting allows you to insert any text you want between numbers, such as 'year', 'month', 'day', 'hour', 'minute', 'second'.{{ myTime | date:"2006年01月02日 15时04分05秒" }} {# 输出: 2023年03月15日 15时04分05秒 #}Show the day of the week and the month name:The layout of the Go language also supports displaying the day of the week (Mon, Monday) and the name of the month (Jan, January).
{{ myTime | date:"Mon, January 2, 2006" }} {# 输出: Wed, March 15, 2023 #}Here
MonCorresponding to Wednesday,JanuaryCorresponding to March.
Through these examples, you will find that the date formatting in the Go language may seem unique at first glance, but once you master the corresponding relationship of the 'magic numbers', it can provide great flexibility, allowing you to display dates and times in almost any format you can imagine.
Some tips for practical application
We may use it like this in content operation and template developmentdateFilter:
- Formatting the publication time of articles:“`twig
{# Assuming archive.CreatedTime is already a time.Time object #}
Published on: {{ archive.CreatedTime | date: “January 02, 2006” }}
Updated on: {{