Which date and time formatting parameters are supported by the `date` filter in AnQiCMS?

Calendar 👁️ 70

In website content management, the accurate display of dates and times is crucial for improving user experience and ensuring the timeliness of information.AnQiCMS provides a flexible and powerful template engine, allowing you to easily customize the display of date and time on the page.Among many practical tools,dateThe filter is an important member in handling date and time formatting.

dateThe filter: a tool for formatting date and time.

dateThe filter is used in the AnQiCMS template to convert.time.TimeThe date and time value of the type is formatted into the specific string we need. Its strength lies in the fact that it follows the unique and highly flexible date and time formatting rules of the Go language.

When you use it in the templatedatea filter, for example{{ object.timeField|date:"格式字符串" }}You need to provide a 'format string' as a parameter. This format string is not the traditionalY-m-d H:i:sThis placeholder, instead, is the uniquely crafted 'reference time' in Go language design——2006-01-02 15:04:05. This is not a simple date, but a date used internally in Go language——definitionYou want to display the date in what format. You tell the system what kind of output you want through this reference time.

For example, if you want to display the full year, month and date, you can use2006-01-02This format will output something like2023-10-27The result. If you need to display more specifically, including hours and minutes, then2006-01-02 15:04it will be output2023-10-27 14:35in this form.

Flexible date and time formatting parameters

dateThe filter supports a very rich set of formatting parameters because its essence is to let you use2006-01-02 15:04:05This reference time, rearrange it according to your desired layout.

  • Only show the date part:

    • "2006-01-02": Will output the full year, month and date, such as2023-10-27.
    • "2006/01/02": Change the separator to a slash, output such as2023/10/27.
    • "01-02": Only show the month and date, such as10-27.
    • "2006年01月02日": Combine with Chinese text, output2023年10月27日.
  • Only show the time part:

    • "15:04": Display hours and minutes, such as14:35.
    • "15:04:05": Display hours, minutes, and seconds, such as14:35:00.
    • "3:04PM": 12-hour time format with AM/PM, such as2:35PM.
  • Display date and time:

    • "2006-01-02 15:04": Common date and time format, such as2023-10-27 14:35.
    • "2006-01-02 15:04:05": A complete date and time including seconds, such as2023-10-27 14:35:00.
    • "Mon Jan _2 15:04:05 2006": This will output a very detailed format, including the day of the week, month abbreviation, date, time (including seconds) and year, for exampleFri Oct 27 14:35:00 2023. Pay attention to the underscore before the date._It is to leave space before the single-digit number (1-9).
  • Other more detailed formats:

    • "Monday, 02-Jan-06 15:04:05 MST": Will output similar toFriday, 27-Oct-23 14:35:00 CSTDetailed format.
    • "2006-01-02T15:04:05Z07:00"The ISO 8601 format is commonly used for data exchange, such as2023-10-27T14:35:00+08:00.

The key is that you just need to remember2006-01-02 15:04:05This 'magic number', then according to it to construct your own format string. If you want to display the year, use2006to represent the position; if you want to display the month, use01The position is indicated, and so on.

Generally, you will encounter some fields that are inherently in the AnQiCMS template.time.TimeType, such as document's.UpdatedTime(Updated time) or category's.CreatedTime(Creation time). At this point, directly pass these fields through the pipeline|pass todateFilter, and add the format string you defined. For example:

<p>文章发布于:{{ archive.CreatedTime|date:"2006年01月02日 15:04" }}</p>

In addition, AnQiCMS also providestimeA filter that performsdateThe filters are completely the same, you can choose to use any one of them according to your personal preference.

datewithstampToDateDifference

There is an important detail that needs to be paid attention to here:dateThe filters are specifically used for processingtime.TimeThe type of object. If you have a 10-digit or 13-digit Unix timestamp (i.e., a string of numbers, such as1609470335), you should not use it directlydateFilter. This will cause the system to report an error due to a mismatch in data types.

At this point, you should use another powerful tag provided by AnQiCMS -stampToDate. This tag is used to convert timestamps into readable date and time formats, using Go language's reference time as the format parameter. For example:

{% set publishStamp = 1609470335 %}
<p>时间戳转换:{{ stampToDate(publishStamp, "2006-01-02 15:04:05") }}</p>

Understand and apply correctlydateThe filter and its reference time formatting mechanism will give you great flexibility and fine control over the display of dates and times in the AnQiCMS, thereby better meeting the design and operation needs of the website.


Frequently Asked Questions (FAQ)

1. I want to display the publication time of the article in the template, butarchive.CreatedTime|date:"..."it doesn't work, why is that?

This is likely notdatea problem with the filter itself, but rather the one you pass to itarchive.CreatedTimeThe field type is incorrect.dateThe filter requires the input to be in Go language.time.TimeType. Ifarchive.CreatedTimeIn AnQiCMS backend configuration or code, it is stored in the form of Unix timestamp (a series of numbers), you need to usestampToDatetag to format it, for example{{ stampToDate(archive.CreatedTime, "2006-01-02") }}.

2. Besides2006-01-02 15:04:05Can I also add my own text in the format string?

Of course you can. The reference time formatting mechanism in Go language is very flexible, you can mix fixed text with reference time elements. For example, if you want to display文章发布于 2023年10月27日 星期五You can combine the format string like this:"文章发布于 2006年01月02日 Mon"It will output the article published on October 2023 according to the actual date

Related articles

How to format a `time.Time` object into a specified date string in AnQiCMS template?

In website content operation, the way dates and times are presented often directly affects user experience and the clarity of information.A professional, readable date format can not only enhance the credibility of the content, but also allow visitors to obtain key information faster.AnQiCMS as a powerful content management system naturally also provides a flexible way to handle and format date and time.When we need to display the publication time, update time, or any other date information in the AnQiCMS template, we may encounter a problem

2025-11-08

How to remove specific characters from a string in the AnQiCMS template, such as spaces or special symbols?

In website content operation, we often encounter scenarios where we need to process string data.For example, the title, description, or keywords extracted from the database may contain extra spaces, unnecessary special characters, or even inconsistent prefixes or suffixes.These characters may not only affect the aesthetic beauty of the layout, but may also cause interference when performing data analysis or search engine optimization (SEO).AnQiCMS as an efficient content management system, deeply understands the needs of users in content processing.

2025-11-08

In AnQiCMS template, can the `count` filter count the number of occurrences of elements in an array?

In AnQiCMS template development, we often need to handle various data, one common requirement is to count the number of times a specific element or keyword appears.When faced with array or string data, the `count` filter can become your powerful assistant.So, can the `count` filter in the AnQiCMS template count the number of occurrences of elements in an array?The answer is affirmative, it can not only count the word frequency in a string, but also accurately count the number of occurrences of elements in an array.

2025-11-08

How to count the frequency of a keyword in a string in AnQiCMS template?

In AnQi CMS template development, we often need to handle and analyze various data of page content.Among them, counting the number of times a specific keyword appears in a string is a very practical need.This not only helps content operators understand the density of keywords, thereby optimizing SEO, but also provides data support for certain feature displays, such as showing how many times a certain feature is mentioned.The Anqi CMS template engine provides a rich set of filter (Filters) functions, which act as mini data processing tools, allowing variables to be formatted, converted, or calculated

2025-11-08

How to set a default display value for possibly empty variables in AnQiCMS templates?

When using AnQiCMS to build a website, we often encounter such situations: some content fields may not have values every time, such as articles may not have thumbnails, products may not have detailed descriptions, or some contact information may not have been filled in.If variables that may be empty are directly called in the template, ugly blank spaces may appear on the page, or even program errors, which will greatly affect the user experience and the professionalism of the website.Luckyly, the AnQiCMS template system is based on syntax similar to Django and Blade, providing a powerful and flexible mechanism to handle these situations

2025-11-08

What is the difference between the `default` and `default_if_none` filters for handling null values in AnQiCMS?

In AnQiCMS template development, we often need to handle the case where data may be empty to ensure the stability of page display and user experience.AnQiCMS provides rich template filters to meet such needs, where `default` and `default_if_none` are very practical tools for handling null values.They are both intended to provide a fallback value for missing or empty data, but there is a subtle yet critical difference in the definition of 'empty,' understanding these differences can help us more precisely control the template rendering logic.###

2025-11-08

How to check if a number is divisible by another number in AnQiCMS template?

During the development of AnQiCMS templates, we often encounter situations where we need to adjust the display of content or apply different styles based on specific conditions.For example, when a number in the list meets a certain rule, such as being divisible by 3, we would like it to have a special performance.Lucky enough, AnQiCMS's powerful template engine provides a simple and efficient way to meet this requirement.This article will deeply explore how to flexibly judge whether a number can be evenly divided by another number in the AnQiCMS template, and provide practical code examples in real-world scenarios.### Core Function

2025-11-08

What type of value does the `divisibleby` filter in AnQiCMS return in mathematical operations?

In AnQiCMS template creation, we often need to control the display of content based on some mathematical logic, such as determining whether a number can be evenly divided by another number.At this point, the `divisibleby` filter is particularly important.It helps us easily implement this judgment in the template without writing complex logic code.The `divisibleby` filter returns a very intuitive and easy-to-understand boolean value when performing mathematical integer division.This means that the result can only be two possibilities: `True` (true) or

2025-11-08