In website operation and content management, we often need to handle various data, among which date and time information is particularly common.However, these data are often stored in the form of a series of seemingly unordered numbers (i.e., timestamps), which is difficult for ordinary users to understand intuitively.AnQiCMS as an efficient enterprise-level content management system fully considers this user demand, providing a simple yet powerful way to help us convert these timestamp data into a clear date and time format and display it on the website pages.
AnQiCMS's template engine design draws inspiration from the syntax style of Django templates and Blade templates, allowing content developers to easily process and display data through specific tags and filters. To solve the problem of timestamp formatting, we will mainly rely on the built-in features of AnQiCMS.stampToDateLabel.
Understand Timestamps: What is it and why is it needed to be formatted?
In simple terms, a timestamp is a number that represents the number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC). In AnQiCMS, you usually encounter 10-digit Unix timestamps, such as1609470335.
Such numbers are efficient for computers to store and calculate, but they are meaningless for users accessing websites.Therefore, formatting these original timestamp data into readable date and time formats such as 'October 27, 2023, 10:30 AM' or '2023-10-27 10:30:00' is a crucial step in enhancing user experience.
Timestamp data source in AnQiCMS
In AnQiCMS, many core time information of the website content exist in the form of timestamps. For example, when you publish an article, update a product, or a user submits a comment, the relevant creation time (CreatedTime)Update time(UpdatedTimeThese fields, such as timestamps, are typically stored in time stamp format. These fields usually appear as attributes of content models or list items.itemThe attributes of the above fields, such as timestamps, are typically stored in time stamp format.
For example, in document details or document lists, you may encounter the following fields representing timestamps:
item.CreatedTime: The creation time of the content.item.UpdatedTime: The last update time of the content.archive.CreatedTime[en]: Single document creation time.user.LastLogin[en]: User's last login time.
[en]: Next, let's see how to do these.item.CreatedTimeWait for the timestamp variable to be converted into the date-time format we are familiar with.
Core Function:stampToDateUse of tags
AnQiCMS provides a feature namedstampToDateThe built-in template tags, specifically used to format timestamp data into a specified readable date and time format. This tag is very intuitive and flexible to use.
Its basic usage syntax is:{{stampToDate(时间戳变量, "格式字符串")}}
The "timestamp variable" here is the 10-digit timestamp number you want to format, for example,item.CreatedTimeThe "format string" is the key to defining the format in which you want the date and time to be displayed.
It is worth noting that the template engine of AnQiCMS is based on Go language, and its date formatting string follows the unique rules of Go language. Unlike what we are accustomed toYYYY-MM-DDoryyyy-MM-ddDifferent, Go language uses a fixed reference date2006-01-02 15:04:05(UTC time, that is, at 3:04:05 PM on January 2, 2006) to serve as the format template.You just need to replace the year, month, day, hour, minute, and second in this reference date with the format characters you want, and the system will automatically generate the corresponding date and time string based on the input timestamp.
Here are some common formatting examples:
Show date only (Year-Month-Day)If you only need to display the date, for example, "2023-10-27":
{{stampToDate(item.CreatedTime, "2006-01-02")}}Display the full date and time (Year-Month-Day Hour:Minute:Second)If you need to display a date and time accurate to the second, for example “2023-10-27 10:30:00”:
{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}}Custom Chinese formatIf you wish to display in a format more in line with Chinese conventions, such as “October 27, 2023”:
{{stampToDate(item.CreatedTime, "2006年01月02日")}}Other custom formatsYou can combine flexibly as needed, such as '10/27/2023' or 'Friday 10:30 AM':
{{stampToDate(item.CreatedTime, "01/02/2006")}} {# 月/日/年 #} {{stampToDate(item.CreatedTime, "Mon 15:04")}} {# 星期几 时:分 #}
Practical Exercise: Apply date format in the content list
In the actual website content display, we usually use this feature on list pages (such as article lists, product lists) or detail pages (such as article details).
Assuming we have a list of articles that includes the title and publication time. The following is an example of how to applystampToDatea tag in a template:
"`twig {# Assuming