How do the `date` and `time` filters display `time.Time` type time values in a specified format?

Calendar 👁️ 73

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.

Related articles

How to remove specified characters from any position in the string using the `cut` filter in the Anqi CMS template?

In website content operation, we often need to process strings, such as cleaning up extra characters, or standardizing the display content.AnQiCMS template engine provides a series of powerful filters to help us complete these tasks, among which the `cut` filter is a very practical tool, specifically used to remove specific characters from any position in a string.### Understanding the working principle of the `cut` filter The `cut` filter plays a very direct role in the AnQi CMS template

2025-11-08

How to use the `count` filter to calculate the number of times a keyword appears in a string or array?

In website content operation, we often need to understand the frequency of certain specific keywords appearing in articles, product descriptions, and other text.This is very valuable for SEO optimization, content quality analysis, and even just data statistics.AnQi CMS provides a powerful and flexible template system, which includes a very practical `count` filter, which can help us easily achieve this goal.### Understanding the `count` filter In simple terms, `count`

2025-11-08

How to determine if a string or array contains a keyword using the `contain` filter in the AnQi CMS template?

In Anqi CMS template development, we often need to dynamically adjust the display method of the page according to the different characteristics of the data.Among them, determining whether a string or array contains a certain keyword or element is a very basic but extremely common requirement.At this moment, the `contain` filter has become a powerful tool in our hands, helping us easily achieve this goal.The `contain` filter is mainly used to check if a variable (whether it is a string, array, or key-value pair and structure) contains a specific content.Its result will be a boolean value

2025-11-08

How do the `center`, `ljust`, and `rjust` filters align strings to the center, left, or right at a specified length?

In the presentation of website content, the format and alignment of text often directly affect the reader's reading experience.Especially when creating AnQiCMS templates, we often encounter the need to center, left-align, or right-align titles, phrases, or data lists according to a specific length.Although most layouts rely on CSS styles, in some cases, using built-in string filters in templates can be more flexible and efficient to achieve these delicate text layouts.

2025-11-08

How to set default values for `default` and `default_if_none` filters to avoid displaying blank when the variable is empty or `nil`?

In daily website content management, we often encounter such situations: certain variables may be empty due to unfilled content, missing data, or other reasons (or in programming terms, `nil`).If not handled, these variables may be displayed as blank areas on the front-end page, which not only affects the beauty of the page but may also confuse visitors and reduce the professionalism of the website.AnQiCMS (AnQiCMS) is well aware of this pain point

2025-11-08

How does the `divisibleby` filter determine if a number is divisible by another number, and what conditions are commonly used?

The template engine of AnQiCMS provides a powerful and flexible tool for the dynamic display of website content.Amongst them, the `divisibleby` filter is a small but effective feature that enhances the logic of templates, mainly used to determine if a number can be evenly divided by another number.Understand and make good use of this filter, which can help us achieve more intelligent effects in content presentation and page layout.### `divisibleby` filter's core function

2025-11-08

How can the `dump` filter be used in the template development process for debugging and viewing the structure and value of variables?

During the development of Anqi CMS templates, we often need to handle various dynamic data.This data may come from a database, system configuration, or user input, and it is passed to the template as variables for display.However, sometimes the results displayed by the page are not what we expect-a field may be empty, the data format is incorrect, or the elements contained in a set may not be as expected.In this case, efficiently "viewing" the internal structure, type, and specific values of variables becomes the key to troubleshooting and speeding up development progress

2025-11-08

What security escaping features do the `escape`, `e`, and `escapejs` filters provide when outputting HTML or JavaScript code?

It is crucial for the safety of output content in website operations.Especially when displaying user-submitted data or information obtained from external sources, if not properly processed, websites are easily vulnerable to cross-site scripting attacks (XSS) and other threats.AnQiCMS as a content management system that focuses on security, provides a powerful security escaping mechanism at the template rendering level, among which the `escape`, `e`, and `escapejs` filters are the key tools to ensure output security.AnQiCMS's template engine draws inspiration from Django

2025-11-08