In the daily operation of AnQi CMS, the precise presentation of content is a key factor in improving user experience and the professionalism of the website.Formatting of dates and times is an indispensable part.As an experienced website operator, I am well aware of the readers' requirements for clear and consistent information.stampToDateFilter how to format timestamp to specified date and time in AnqiCMS template.
Flexible timestamp formatting:stampToDateFilter introduction
In the AnqiCMS template system, we often encounter scenarios where we need to display content publish time, update time, etc.This time data is usually stored in the form of a timestamp (Unix timestamp), which is difficult and unfriendly for users to understand directly.stampToDateFilter becomes a powerful tool for us to perform time formatting.It can convert the standard 10-digit timestamp into the date or time string we preset, which is easy to read, greatly enhancing the readability and user experience.
stampToDateBasic usage of the filter
stampToDateThe syntax of the filter usage is very intuitive:
{{stampToDate(时间戳, "格式")}}
Here are the two main parameters:
- TimestampThis refers to a 10-digit Unix timestamp that needs to be formatted. In AnqiCMS templates, this is usually sourced from a database field, such as the article object's field.
item.CreatedTimeoritem.UpdatedTimeFor example,1609470335represented a specific moment. - Format: This is a string parameter used to define the date and time display mode after the timestamp is converted. The 'format' here is not what we usually understand as
YYYY-MM-DDEnglish placeholder, instead of following the Go (Golang) specific reference time format.
Mastering the Go (Golang) reference time format is the correct way to use it.stampToDateEnglish version: The key. Golang uses a fixed reference time:2006年01月02日 15时04分05秒 -0700 MSTTemplate comes as a definition of format.This means, when you want to output a certain date-time format, you need to write the representation of the corresponding part in the "format" string.
For example, if you want to display年-月-日, you would write"2006-01-02"; if you want to display时:分, you would write"15:04". Let us understand this better through some practical examples.
Deep understanding of Golang's reference time format
Golang's reference time is a very specific value:2006-01-02 15:04:05.999999999 -0700 MSTEach number and letter represents a specific part of the date and time.
- 2006Year, such as
2023 - 01Month (number), such as
03Represent March - JanuaryorJan: Month (full name or abbreviation)
- 02: Date, such as
15 - MondayorMon: Day of the week (full name or abbreviation)
- 15: Hour (24-hour format), such as
09Represent 9 AM - 03: Hour (12-hour format), such as
09Represent 9 AM - PMorpm: AM/PM indicator
- 04:minutes, such as
30 - 05:seconds, such as
45 - .999999999:nanoseconds
- -0700:time zone offset
- MST:time zone abbreviation
By combining the elements of these reference times, we can define various required date and time formats.
Practice examples in AnqiCMS template
假设我们有一个时间戳变量publishStampwith the value1609470335(代表2021年1月1日 12:25:35 UTC)。以下是几种常见的格式化需求及其stampToDateUsage:
仅显示日期:
- Year-Month-Day (e.g., 2021-01-01)
{% set publishStamp = 1609470335 %} <p>发布日期:{{stampToDate(publishStamp, "2006-01-02")}}</p> - Year/Month/Day (e.g., 2021/01/01)
{% set publishStamp = 1609470335 %} <p>发布日期:{{stampToDate(publishStamp, "2006/01/02")}}</p> - Year, month, day Chinese format (for example: 2021年01月01日)
{% set publishStamp = 1609470335 %} <p>发布日期:{{stampToDate(publishStamp, "2006年01月02日")}}</p>
Only display time:
- Hour:Minute:Second (24-hour format, for example: 12:25:35)
{% set publishStamp = 1609470335 %} <p>发布时间:{{stampToDate(publishStamp, "15:04:05")}}</p> - Time: minute (e.g., 12:25)
{% set publishStamp = 1609470335 %} <p>发布时间:{{stampToDate(publishStamp, "15:04")}}</p>
Display date and time:
- Year-Month-Day Time: minute: second (e.g., 2021-01-01 12:25:35)
{% set publishStamp = 1609470335 %} <p>完整时间:{{stampToDate(publishStamp, "2006-01-02 15:04:05")}}</p> - Month day, year, time: minute AM/PM (e.g., Jan 01, 2021, 12:25 PM)
{% set publishStamp = 1609470335 %} <p>英文格式:{{stampToDate(publishStamp, "Jan 02, 2006, 03:04 PM")}}</p>
In practical applications, we usually willstampToDateFilter applied to the content object.CreatedTimeorUpdatedTimeFor example, show the publication time of the article on the article detail page:.
{% archiveDetail article with name="CreatedTime" %}
<p>文章发布于:{{stampToDate(article, "2006年01月02日 15:04")}}</p>
Or show the update time of each article in the article list:.
{% archiveList archives with type="list" limit="10" %}
{% for item in archives %}
<p>{{item.Title}} - 最后更新:{{stampToDate(item.UpdatedTime, "2006-01-02 15:04")}}</p>
{% endfor %}
{% endarchiveList %}
stampToDateFilter's value to website operation
For website operators,stampToDateThe filter is not just a technical detail, but also a part of content management and user experience optimization. By flexibly using this filter, we can:
- Enhance user experienceConvert rigid timestamps to human-readable formats, making it easy for users to obtain content timeliness information at a glance.
- Maintain consistency of content:No matter the data source, the date and time format can be displayed uniformly on the front end, maintaining the overall professional image of the website.
- Convenient for content maintenance and updateThrough clear time information, operation personnel can more easily track the publishing and updating cycle of content, and timely adjust operation strategies.
- Supports multilingual and localizationThrough adjusting the format string, it can adapt to the needs of different language environments for displaying date and time.
In short,stampToDateThe filter is a small but powerful tool in AnqiCMS template.Mastery of its use will help you better manage and present website content, providing users with a superior browsing experience.
Common Questions (FAQ)
The timestamp parameter must be 10 digits? If my timestamp is 13 digits (millisecond level) what should I do?
stampToDateThe filter is currently designed to accept standard 10-digit Unix timestamps (second level). If your timestamp is 13 digits in milliseconds,stampToDateBefore, first convert it to a second-level timestamp by dividing it by 1000.AnqiCMS template itself may not have the direct function for mathematical operations, which usually needs to be completed on the backend data processing or through front-end JavaScript assistance, ensuring that the parameters passed to the filter are 10-digit integers.
Why doesn't the date and time show up correctly when I set the format string to 'YYYY-MM-DD HH:MM'?
This is a common misconception.stampToDateThe filter uses the reference time format of Golang, not the commonYYYYorMMThis placeholder. You must use Golang's reference time2006年01月02日 15时04分05秒English equivalent of 'auto'年-月-日 时:分English equivalent of 'auto'"2006-01-02 15:04",instead of"YYYY-MM-DD HH:mm"English equivalent of 'auto'
English equivalent of 'auto'CreatedTimeandUpdatedTimeWhat other scenarios would be usedstampToDateFilter?
stampToDateFilter can be used for any date-time data stored in the form of a 10-digit Unix timestamp.For example, user registration time, comment posting time, order generation time, etc.stampToDateThe filter can be used. You can use it on any template variable that needs to format a timestamp.