In AnQi CMS template development, flexibly displaying dates and times is an indispensable part of content presentation. You may often need to format date information in the system or content, such as年-月-日月/日 时:分Presented in various ways to users. At this point,dateThe filter becomes your helpful assistant.
AnQi CMS is an enterprise-level content management system built with Go language, and naturally follows some conventions of GoLang in handling date and time in the template level.dateThe filter is specifically designed to process GoLang nativetime.Timetype data.
Get to knowdateThe filter and its GoLang formatting rules
dateThe main function of the filter is to transform atime.TimeThe value, formatted as a string as defined by you, should be converted to an easily readable date-time string. But there is an important detail to note:dateThe filter only acceptstime.TimeType data as input.
The formatting of strings is unique to GoLang,“2006-01-02 15:04:05Base time layout.This is not a random sequence of numbers, but a reference time point used internally by the Go language to define date and time formats.
2006: denotes the year01: denotes the month (with leading zero)02: denotes the date (with leading zero)15Hours (24-hour format, with leading zero)04Minutes (with leading zero)05Seconds (with leading zero)- You can freely combine these numbers and symbols to create any date and time format you want. For example:
2006-01-02It will output.年-月-日01/02It will output.月/日15:04It will output.时:分Mon, 02 Jan 2006 15:04:05 MSTIt will output.星期一, 02 1月 2006 15:04:05 CSTThis is a complete format
How to usedateFilter
When your template variable directly holds GoLang'stime.Timeobject, usedatethe filter is very intuitive. Suppose you have defined a filter in your GoLang backend code.currentTimeSet the variable and make it current.time.TimePass the object to the template:
{# 假设 currentTime 是一个 GoLang 的 time.Time 对象 #}
{# 仅显示年份 #}
<div>当前年份:{{ currentTime|date:"2006" }}</div>
{# 显示年-月-日 #}
<div>完整日期:{{ currentTime|date:"2006-01-02" }}</div>
{# 显示月/日 时:分 #}
<div>简要时间:{{ currentTime|date:"01/02 15:04" }}</div>
{# 显示中文格式的日期时间 #}
<div>中文格式:{{ currentTime|date:"2006年01月02日 星期一 15时04分" }}</div>
Key points:time.TimeDistinguish from timestamps.
In AnQi CMS template development, you will often encounter the need to display the content publication time (CreatedTime) or update time (UpdatedTime) These fields are usually in thearchiveDetail/archiveListorcategoryDetail tags, usually in the timestampThe form provided, not directtime.TimeType.
If you pass these timestamps directly todateA filter, the template engine will report an error due to type mismatch. In this case, Anqi CMS provides another convenient tag specifically for formatting timestamps -stampToDate.
stampToDateThe usage of tags is as follows:
{# 假设 item.CreatedTime 是一个时间戳(例如:1675862400) #}
{# 格式化时间戳为“年-月-日” #}
<div>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</div>
{# 格式化时间戳为“年年年年/月月/日日 时:分:秒” #}
<div>更新时间:{{ stampToDate(item.UpdatedTime, "2006/01/02 15:04:05") }}</div>
{# 格式化为更友好的中文形式 #}
<div>内容创建于:{{ stampToDate(item.CreatedTime, "2006年01月02日 15点04分") }}</div>
You can clearly see through the above examplesdateFilters andstampToDateEach label applies to a specific scenario. Correctly distinguishing between these data types and their corresponding processing methods can help you develop templates more efficiently and avoid unnecessary errors.
Summary
dateThe filter is used to process GoLang natively in the Anqi CMS templatetime.TimeThe standard way of formatting data types.It is based on the unique GoLang time format layout, providing high flexibility. timestampRemember to use when handling datastampToDateTo complete the formatting, make sure the template runs correctly and displays the data. Mastering these two tools will allow you to easily handle various date and time display needs.
Frequently Asked Questions (FAQ)
1. Why myCreatedTimeUse the field directly.dateThe filter will report an error?
This is usually becauseCreatedTimeThe field is in the content tag of AnQi CMS (such asarchiveList/archiveDetailetc.), and is returned in the form of timestamp rather than GoLang'stime.TimeObject.dateThe filter requires the input to betime.Timeof type. For timestamps, you should usestampToDatetags for formatting, for example:{{ stampToDate(item.CreatedTime, "2006-01-02") }}.
2. Why is the time formatting string of GoLang '2006-01-02 15:04:05'? Are there any other memory methods?
The GoLang date time formatting string is designed by its creator to avoid ambiguity, each number and symbol represents a specific date and time element. Remember it is1月2日周一下午3点04分05秒MST 2006年(Monday, January 2 15:04:05 MST 2006),You can use each element of this string as a replacement template. For example, if you want the year, write “2006Enter the month you want01Enter the hour you want15This helps you quickly build the required format.
3. Can I create in custom GoLang backend codetime.TimeObject and pass it to the template, then usedatefilter?
Of course you can. If you are doing a second development and have created it in your GoLang backend logic,time.TimeA type variable, you can directly pass it to the Anqi CMS template. In the template, this variable can be used directlydateFormatted by the filter. This isdateOne of the most typical use cases of the filter, it perfectly connects the GoLang backend logic and the frontend template's date and time display.