As an experienced CMS website operation personnel for a security company, I know that website content should not only be rich but also presented in a user-friendly manner.Time information is an important part of the content, but the timestamps (Unix timestamps) stored in the database are often just a series of numbers, which can be quite confusing when displayed directly.Therefore, formatting these timestamps into easily readable date and time strings is a crucial step in enhancing user experience and increasing content readability.
In AnQi CMS, we have a very convenient template tag to solve this problem, that isstampToDate.
Understanding timestamps in the database
The content release and update time information is stored as a 10-digit Unix timestamp in AnQi CMS. For example, you may obtain it inarchiveListorarchiveDetailthe tagitem.CreatedTimeoritem.UpdatedTimeSuch fields, their values may be similar to1609470335This sequence of numbers represents the number of seconds from January 1, 1970 00:00:00 UTC to a certain moment. While precise, it is of no significance to the average reader.
Security CMS solution:stampToDatetags
To make these numbers meaningful, Anqi CMS providesstampToDateTemplate tag. This tag can take a 10-digit timestamp and convert it into a readable date and time string according to the format you specify.
The usage of this tag is very intuitive:
{{stampToDate(时间戳, "格式")}}
Among them,时间戳is the number you get from the database field (such asitem.CreatedTime). It is the style you want the date and time to be displayed, which follows the unique time formatting rules of the Go language.格式Then it is the style you want the date and time to be displayed, which follows the unique time formatting rules of the Go language.
Deep understanding of Go language's time formatting rules
Go language's time formatting rules may differ from those in other programming languages (such as PHP'sY-m-d H:i:sor Python's%Y-%m-%d %H:%M:%SThe different habits in the middle of the sentence.In Go language, string formatting is not represented by placeholders for year, month, day, etc., but defined by a fixed reference date and time to specify the output format.
2006-01-02 15:04:05
You can understand this reference date as a format "template". When you want to output the year, use2006; when you want to output the month, use01; when you want to output the date, use02;and so on.
Here are some common formatting examples and their corresponding output effects:
- Display only the date (Year-Month-Day):
{{stampToDate(item.CreatedTime, "2006-01-02")}} {# 示例输出: 2021-06-30 #} - Display the date (Year/Month/Day):
{{stampToDate(item.CreatedTime, "2006/01/02")}} {# 示例输出: 2021/06/30 #} - 显示日期和时间(精确到分):
{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}} {# 示例输出: 2021-06-30 12:30 #} - 显示完整日期和时间(精确到秒):
{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}} {# 示例输出: 2021-06-30 12:30:00 #} - 仅显示月份和日期:
{{stampToDate(item.CreatedTime, "01-02")}} {# 示例输出: 06-30 #} - 显示中文日期:
{{stampToDate(item.CreatedTime, "2006年01月02日")}} {# 示例输出: 2021年06月30日 #} - 显示带有星期几的日期:
{{stampToDate(item.CreatedTime, "Mon Jan _2 2006")}} {# 示例输出: Wed Jun 30 2021 #}
You can flexibly combine according to your actual needs2006/01/02/15/04/05and create unique date-time strings by using other time format elements supported by the Go language.
Actual application: Formatting document time in templates
In the template of Anqi CMS, when we usearchiveListorarchiveDetailwill usually include when retrieving document data with tags such asCreatedTime(Creation time) andUpdatedTimeThe field (update time). The values of these fields are 10-digit timestamps. We can directly use them asstampToDatethe first parameter for formatting.
For example, on a document detail page, you may want to display the publishing time of the document:
<article>
<h1>{{archive.Title}}</h1>
<div class="article-meta">
<span>发布于:{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04")}}</span>
<span>浏览量:{{archive.Views}}</span>
</div>
<div class="article-content">
{{archive.Content|safe}}
</div>
</article>
In this example,archive.CreatedTimewill be formatted as2021年06月30日 12:30such a string, which greatly enhances the user's understanding of time information.
Summary
PassstampToDateLabel, Auto CMS provides a powerful and flexible way to handle and display timestamp data from the database.The key to mastering the Go language's date formatting rules is to correctly use this tag, which allows you to convert cold, numeric timestamps into warm, understandable date and time strings based on different designs and user needs, thereby optimizing the presentation of your website content and user experience.
Common Questions and Answers (FAQ)
1. The timestamp I retrieve from the database is not a 10-digit number, but a longer number (such as 13 digits), how should I handle it?
10-bit timestamp usually refers to Unix timestamp (seconds), while 13-bit timestamp usually represents Unix millisecond timestamp. Anqiy CMS'sstampToDateLabel expects to receive a 10-digit Unix timestamp (seconds). If your timestamp is a 13-digit millisecond timestamp, you need to convert it before passing it tostampToDateBefore, perform a simple mathematical operation to convert it to seconds. For example, you can use the division by 1000 method:){{stampToDate(item.MilliTimestamp / 1000, "2006-01-02 15:04:05")}}Make sure to check your data source and confirm the actual unit of the timestamp.
2. Go language's date formatting rules (2006-01-02 15:04:05) looks strange, is there a more intuitive way to memorize it?
Go language's reference date2006-01-02 15:04:05The number can be decomposed into the following digits, each representing a specific time unit:
2006Represents the year01Represents the month (January)02Represents the date (2nd day)15Represents hour (3 PM, 24-hour format)04Represents minutes05Represents seconds
You can remember it as "January 2, 3:04:05 PM in 2006”。When you want to include a time unit in the format, use its corresponding number. For example, to display the year, write2006;to display the month, write01This is a very unique and highly flexible formatting method once mastered.
3. BesidesstampToDateBesides, does Anqi CMS have any other time-related template tags?
Yes, in addition to formatting the timestamps in the database, Anqi CMS also provides{% now "格式" %}tags to retrieve and format the current system time. Its formatting rules are the same asstampToDateExactly the same, both follow the reference date format of the Go language. For example, to display the current year, you can use{% now "2006" %}This tag is often used to display the current year's copyright information in the footer, or in other places where real-time time needs to be displayed.