In the template design of AnQi CMS, flexibly displaying dates and times is a key element of content presentation. To meet this refined requirement, AnQi CMS provides a series of practical time processing tools, wheredateandtimeFilter is an important function for formatting display of type time values.time.TimeFormat type time values to display.
Understandingtime.TimeType and filter basics.
Before getting a deeper understandingdateandtimeBefore the filter, we need to clarify a premise: these two filters are specifically used to process the Go language in.time.TimeType data. Anqicms is developed based on Go language, and the time data it handles internally is usuallytime.Timeobject. If you encounter an object that has already been set in the templatetime.TimeType of time variable, thendateortimeFilter will be your first choice.
The core function of these two filters is to convert a standardtime.Timeobject, formatted into an easy-to-read text format according to the specified format string. It is worth mentioning that,timea filter is actuallydateA synonym for a filter, they have the same function and usage, and you can choose to use them according to your personal preference.
Master the time formatting rules of Go language.
Different from many programming languages that use such asY-m-d H:i:sThis placeholder is used to define the time format, and Go language uses a unique 'reference time' pattern. This reference time is fixed:“2006年01月02日 15点04分05秒”.You need to match the output format you expect with the corresponding element in this reference time.For example, if you want to display the year, write “2006”; if you want to display the month, write “01”; if you want to display the hour, write “15”.
下面是一些常用格式化规则示例:
- Year:
2006(Four-digit year),06(Two-digit year) - Month:
01(Two-digit month),Jan(缩写月份名,如Jan),January(完整月份名) - Date:
02(两位数日期) - Weekday:
Mon(缩写星期名),Monday(完整星期名) - hour (24-hour format):
15 - hour (12-hour format):
03,PM(AM/PM marker) - Minute:
04 - Second:
05 - Millisecond/Microsecond/Nanosecond:
.000,.000000,.000000000
dateandtimeHow to use the filter
No matter your choicedateOrtime, the basic syntax pattern is consistent:
{{ 您的时间变量 | date:"您的格式化字符串" }}
or
{{ 您的时间变量 | time:"您的格式化字符串" }}
Suppose you have a template variablearticle.PublishDateIt is antime.Timetype of value, you can display it like this:
Only show the date (for example: 2023-10-27):
{{ article.PublishDate | date:"2006-01-02" }}
Only show the time (for example: 14:35:00):
{{ article.PublishDate | time:"15:04:05" }}
Show the full date and time (for example: October 27, 2023, 02:35 PM):
{{ article.PublishDate | date:"2006年01月02日 下午03点04分" }}
Display English date format (e.g., Oct 27, 2023):
{{ article.PublishDate | date:"Jan 02, 2006" }}
Through flexible combination of these formatting elements, you can almost achieve any date and time display style you need.
WithstampToDateThe difference between tags
In the AnQi CMS, besides other options,dateandtimeFilters, you may also encounterstampToDateThis label. Understanding the differences between them is crucial:
date/timeFilter:Used specifically for formattingGo language'stime.TimeType.stampToDateLabel:Used specifically for formatting10-bit Unix timestamp(i.e., the number of seconds since 00:00:00 UTC on January 1, 1970). For example,CreatedTimeandUpdatedTimeThe field is usually stored in the form of Unix timestamp, at which point it is necessary to usestampToDate.
For example, when getting the document list, the creation time of the documentitem.CreatedTimeIt is a Unix timestamp, and you will see the following usage:
<span>{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
If you have one obtained from other channels and is ensured to betime.Timetype of variable, such ascustomEvent.EventStartTimethen you can usedateFilter:
<span>活动开始时间:{{ customEvent.EventStartTime | date:"2006年01月02日 15:04" }}</span>
Correctly distinguish between these two time value types and choose the corresponding processing method can ensure the normal operation of the template and the expected time display.
Summary
dateandtimeThe filter provides powerful and flexible time formatting capabilities for the Anqi CMS template. By understanding how they handletime.TimeThe premise of type data, and master the unique "reference time" formatting rules of Go language, you can easily present time information on websites in various user-friendly ways. At the same time, please pay attention to distinguishingtime.TimeObject with Unix timestamp, and use it reasonably.stampToDateTags to handle different types of time data.
Common Questions (FAQ)
Q1: Why did my time value usedateFilter after, not displayed on the page or an error message?A1: This is very likely because the variable you are trying to format is not a Go languagetime.Timetype, but a 10-digit Unix timestamp.dateFilter strictly requires input to be `time.`}]