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