In website content management, accurate display of dates and times is crucial for enhancing 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.dateThe filter is an important member in handling date and time formatting.

dateFilter: A Tool for Formatting Date and Time

dateThe filter is used in AnQiCMS templates to converttime.TimeThe date-time value of type is formatted into the specific string we need. Its strength lies in its adherence to the unique and highly flexible date-time formatting rules of the Go language.

When you use a templatedatefilter, for example{{ object.timeField|date:"格式字符串" }}, you need to provide a 'format string' as a parameter. This format string is not a traditionalY-m-d H:i:sThis placeholder, instead, is the uniquely crafted 'reference time' in Go language design2006-01-02 15:04:05. This is not a simple date, but a reference used internally by the Go languagedefinedThis is a fixed template for how you want the date to be displayed. You 'tell' the system what kind of output you want by referring to this 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 output2023-10-27 14:35in this form.

Flexible and diverse date and time formatting parameters

dateThe filter supports a very rich set of formatting parameters, because its essence is to allow you to use2006-01-02 15:04:05This reference time, re-spell it according to the layout you want. Here are some examples of commonly used formatting parameters and their corresponding output patterns:

  • Only show the date part:

    • "2006-01-02"【en】The year, month, and date will be output completely, such as2023-10-27.
    • "2006/01/02"【en】The delimiter will be changed to a slash, output as2023/10/27.
    • "01-02"【en】Only the month and date will be displayed, such as10-27.
    • "2006年01月02日"【en】Combined with Chinese text, output2023年10月27日.
  • 【en】Only the time part will be displayed:

    • "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 format with AM/PM, such as2:35PM.
  • Display date and time:

    • "2006-01-02 15:04"Common date and time formats, such as2023-10-27 14:35.
    • "2006-01-02 15:04:05"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 (with seconds) and year, for exampleFri Oct 27 14:35:00 2023Note the underscore before the date._It is to leave space in front of single-digit numbers (1-9).
  • Other more detailed formats:

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

The key is, you just need to remember2006-01-02 15:04:05This 'magic number', then use 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, use01[The position is indicated here, and so on.]

[You may encounter some fields that are already]time.Time[types, such as document's]UpdatedTime[update time] or [category's]CreatedTime(Creation time). At this point, you can directly pass these fields through the pipe character.|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 providestimeFilter, its function is withdateBoth filters are identical, you can choose to use either one according to your personal preference.

dateWithstampToDatedifferences

There is a very important detail to pay attention to here:dateFilter specifically designed for processingtime.Timeobjects of a certain type. If you have a 10-digit or 13-digit Unix timestamp (i.e., a sequence of numbers, such as1609470335If so, it should not be used directlydateThis will cause the system to report an error because the data types do not match.

This is when you should use another powerful tag provided by AnQiCMSstampToDateThis tag is used to convert timestamps to readable date and time formats, using the reference time in Go language 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 date and time for website content in AnQiCMS, thereby better satisfying the design and operational needs of the website.


Common Questions (FAQ)

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

this is likely notdatea problem with the filter itself, but rather with the one you pass to itarchive.CreatedTimeThe field type is incorrect.dateThe input required by the filter must be in Go language.time.Timetype. Ifarchive.CreatedTimeThe value is stored in the AnQiCMS backend configuration or code in the form of Unix timestamp (a series of numbers), you need to usestampToDatetags 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. The Go language's reference time formatting mechanism is very flexible, allowing you to mix fixed text with reference time elements. For example, if you want to display文章发布于 2023年10月27日 星期五You can combine the format string in this way:"文章发布于 2006年01月02日 Mon"It will output `The article was published on October 2023` according to the actual date: