In Anqi CMS template design, flexibly displaying dates and times is a key link in content presentation. To meet this refined need, Anqi CMS provides a series of practical time processing tools, includingdateandtimeThe filter is a feature that targetstime.TimeFormatting the type time value for display is an important feature.

Understandingtime.TimeThe type and the filter base

Further understanddateandtimeBefore the filter, we need to clarify a premise: these two filters are specifically used to process the Go language intime.TimeType data. Anqi CMS is developed based on Go language, and the time data it processes internally is usuallytime.Timean object. If you encounter an already defined object in the template,time.TimeA type of time variable, thendateortimeThe filter will be your preference.

The core function of these two filters is to standardize a standardtime.TimeAn object, formatted according to the specified string format, can be converted into an easily readable text form. It is worth noting that,timeA filter is actually,dateA filter alias, they have the same function and usage, you can choose to use it 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:sPlaceholders are used to define time formats, Go language uses a unique "reference time" mode. This reference time is fixed:“2006 January 02 15:04:05”. You need to match the expected output format with the corresponding element in this reference time.For example, if you want to display the year, write '2006';To display the month, write '01'; to display the hour, write '15'.

Below are some common formatting rules examples:

  • Year: 2006(Four-digit year),06(Two-digit year)
  • Month: 01(Two-digit month),Jan(Abbreviated month name, such as Jan),January(Full month name)
  • Date: 02(two-digit date)
  • Weekday: Mon(abbreviated weekday name),Monday(full weekday name)
  • Hour (24-hour format): 15
  • Hour (12-hour clock): 03,PM(AM/PM indicator)
  • Minute: 04
  • Second: 05
  • Millisecond/Microsecond/Nanosecond: .000,.000000,.000000000

dateandtimeHow to use the filter

No matter which you choosedateOrtimeIts basic syntax pattern is consistent:

{{ 您的时间变量 | date:"您的格式化字符串" }}

Or

{{ 您的时间变量 | time:"您的格式化字符串" }}

Assuming you have a template variablearticle.PublishDateIt is atime.Timetype of value, you can display it like this:

Show only the date (e.g., 2023-10-27):

{{ article.PublishDate | date:"2006-01-02" }}

Show only the time (e.g., 14:35:00):

{{ article.PublishDate | time:"15:04:05" }}

Display 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 (for example: Oct 27, 2023):

{{ article.PublishDate | date:"Jan 02, 2006" }}

By flexibly combining these formatting elements, you can almost achieve any date and time display style you need.

withstampToDateThe difference between tags

In Anqi CMS, in addition todateandtimeFilters, you may also encounter.stampToDateThis tag. It is crucial to understand the difference between them:

  • date/timeFilter:Used specifically for formattingGo languagetime.TimeType.
  • stampToDateLabel:Used specifically for formatting10-digit Unix timestamp(i.e., the number of seconds since 00:00:00 UTC on January 1, 1970). For example, the article'sCreatedTimeandUpdatedTimeFields are typically stored in the form of Unix timestamps, at which point you need to usestampToDate.

For example, when getting a list of documents, the creation time of the documentitem.CreatedTimeIt is a Unix timestamp, you will see the following usage:

<span>{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>

And if you have one from another source and are sure it istime.Timetype variable, such ascustomEvent.EventStartTime, then 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, which can ensure the normal operation and expected time display of the template.

Summary

dateandtimeThe filter provides powerful and flexible time formatting capabilities for Anqi CMS templates. 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 distinguishtime.TimeAn object with Unix timestamp and use it reasonablystampToDateTags to handle different types of time data.


Frequently Asked Questions (FAQ)

Q1: Why did my time value usedateAfter the filter, the page does not display or report an error?A1: This is very likely because the variable you are trying to format is not of Go language type, but a 10-digit Unix timestamp.time.Timetype, but a 10-digit Unix timestamp.dateThe filter strictly requires the input to be `time.