AnQi CMS is an efficient content management system that fully considers the flexibility of templates and the display needs of content. Many of the internal data related to time in the system, such as the creation time of articles (CreatedTimeAnd update time(UpdatedTime), the default storage is all timestamps.These timestamps are precise, but they are undoubtedly not intuitive to display directly to users.Fortunately, AnQiCMS provides a very convenient template tag that can easily format these timestamps into any date string we need.
The core secret:stampToDateThe use of tags
To format the timestamp in AnQiCMS, we need to use a name calledstampToDateThe template tag. This tag is specifically used to handle timestamps and output dates in the specified format. Its usage is very intuitive:
{{ stampToDate(时间戳变量, "格式化字符串") }}
The "timestamp variable" is usually the timestamp field we get in loops or detail pages, for exampleitem.CreatedTimeorarchive.UpdatedTime. The "formatted string" is the key to defining the date output style.
The template engine of AnQi CMS adopts the formatting rules of Go language, which is very important.Different from the common formats such as 'Y-m-d' or 'yyyy-MM-dd', Go language uses a specific reference time '2006-01-02 15:04:05' to represent year, month, day, hour, minute, and second."2006-01-02".
For example, if a variablepublishStampstores a timestamp1609470335(corresponding to January 1, 2021), we can format it as “2021-01-01”:
<div>{{ stampToDate(publishStamp, "2006-01-02") }}</div>
Or, if you want to display it in a format with Chinese characters like "2021-01-01", you can write it like this:
<div>{{ stampToDate(publishStamp, "2006年01月02日") }}</div>
Actual operation: apply the timestamp to the content display
In the presentation of actual website content, timestamp formatting is mainly applied to the creation or update time of articles, products, and other content.
In the article or product detail page:
When we are displaying a single article or product details, we usually go througharchiveDetailtags to get specific content. This tag will return a list that includesCreatedTimeandUpdatedTimeObjects of the fields. We can format these fields directly.
Suppose we are usingarchiveDetailTag to get the details of the current document and assign it to a variable namedarchiveThe variable, then you can display the publish time in the format of "Year-Month-Date" on the page like this:
<div>发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</div>
In the article or product list page:
On the list page, we usually go througharchiveListTag loop to display multiple articles or products. In the loop body, eachitemrepresents an item, and also includes a timestamp field. We can format the timestamp of eachitemthe timestamp.
For example, in a loop through an article list, we can display the publication date of each article like this:
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<p>文章标题:<a href="{{item.Link}}">{{item.Title}}</a></p>
<p>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
{% endfor %}
{% endarchiveList %}
Except for articles and products, other fields containing timestamps, such as the recent login time in the user profileLastLogin(ThroughuserDetailLabel acquisition), you can also use the same method to format it to ensure that all time information is presented in a user-friendly manner.
Understand the formatting of strings: Time formatting rules in GoLang
At first glance, the date formatting rules in GoLang are somewhat special, as they are not formatted using symbols such asYRepresents the year) instead, it is mapped through a fixed reference date “2006-01-02 15:04:05”.This means that when you want to represent a part of a date, you use the corresponding number from the reference date.
The following are some common Go language date formatting references and their corresponding outputs:
"2006": represents a four-digit year, such as 2023"01": represents a two-digit month, such as 07"1"Indicates a single digit month, such as 7"02"Indicates a two-digit date, such as 09"2"Indicates a single digit date, such as 9"15"Indicates a two-digit hour (24-hour format), such as 14"03"Indicates two digits for hours (12-hour clock), such as 02"04"Indicates two digits for minutes, such as 30"05"Indicates two digits for seconds, such as 00"Jan"Indicates the abbreviation for months, such as Jul"Monday": denotes the full name of the week, such as Tuesday
Therefore, when you need the "year-month-date" format, select the year, month, and day from the reference date and combine them with a separator, that is"2006-01-02"This rule may require some adaptation at first, but once mastered, it can be flexibly combined to produce various date display formats required.
BystampToDateTags and flexible date formatting rules in Go language, AnQiCMS users can easily convert background timestamp data into clear, professional, and style-compliant date display, greatly enhancing the reading experience of website content.
Frequently Asked Questions (FAQ)
Q1: Why did I usestampToDateThe label, but the date is displayed incorrectly or blank?
A1: This usually has several reasons. First, please check the inputstampToDate
Q2: Can I format the timestamp into other more detailed forms besides "year-month-date"?
A2: Completely okay.The date formatting rules of Go language are very flexible.You can use any combination of the reference date "2006-01-02 15:04:05" to define the format.
- To display "year/month/day HH:mm:ss", you can use
"2006/01/02 15:04:05". - You can display "Month-Day Weekday" using
"01-02 Monday". - You can display "July 9, 2023 2:30 PM" using
"2006年1月2日 15点04分".\nIt\'s all about what you want to display, use the corresponding number or text in the reference date to represent it.
Q3: Does AnQiCMS have a global setting to unify the date and time format for the entire website?
A3: Currently, the AnQiCMS template system mainly usesstampToDateLabels to control the display format of dates.This means you need to explicitly specify the format in each template code where the date needs to be displayed.There is no one-click global background setting to unify the display format of all dates, but this template-level control provides great flexibility. You can set different date formats according to the needs of different pages or content types (such as detailed time may be needed on article detail pages, while list pages may only need year-month-day).