In Anqi CMS, how to format timestamps into readable dates: a practical guide
When managing website content, we often encounter the need to display article publishing time, update time, user registration time, and other information.These data are usually stored in the database in the form of a series of numbers - that is, 'timestamps'.Although computers can easily understand these numbers, they may not be as friendly for users visiting websites.For example, seeing a number like '1678886400' is not as intuitive as '2023 March 15 10:00:00'.
Fortunately, AnQi CMS provides a very convenient and powerful template tag that helps us easily convert these timestamps into various readable date and time formats.
Get to know the core tools:stampToDateTag
The AnQi CMS template engine adopts a syntax style similar to Django, one very practical built-in tag beingstampToDate. It is used specifically for formatting timestamp displays.
This tag's basic usage is very intuitive:{{stampToDate(时间戳, "格式")}}
Let's take a detailed look at these two parameters:
Timestamp (the first parameter)This is the original timestamp you need to convert. In AnQi CMS templates, we often get data objects (such as document lists in)
itemor document details inarchiveTimestamp can be obtained from the specific field, for exampleitem.CreatedTime(Creation time) orarchive.UpdatedTime(Update time). These fields usually store 10-digit Unix timestamps (second-level).Format (second parameter)This is the specific format you want the date and time string to appear. It is very important to note that the AnQi CMS template engine follows the Go language time formatting rules rather than using like many other systems
Y-m-dorYYYY-MM-DDSuch placeholder.Go language uses a fixed 'reference time' to define the format.You just need to write the reference time in the display format you want, and the system will automatically convert it.This magical reference time is:
2006年01月02日 15时04分05秒Sometimes it is also written as2006-01-02 15:04:05)That is to say, if you want to display the year, you write
2006; If you want to display the month, you write01; If you want to display the day, you write02and so on.
To better understand, let's look at some common formatting examples:
| The format you want to display | Go language format string | Sample output (assuming the timestamp is1678886400Represents2023-03-15 10:00:00 UTC+08:00) |
|---|---|---|
| Year-Month-Date | 2006-01-02 |
2023-03-15 |
| Year/Month/Day (Chinese) | 2006年01月02日 |
2023年03月15日 |
| Hour:Minute:Second | 15:04:05 |
10:00:00 |
| Year-Month-Date Hour:Minute | 2006-01-02 15:04 |
2023-03-15 10:00 |
| Year/Month/Day Hour:Minute:Second | 2006/01/02 15:04:05 |
2023/03/15 10:00:00 |
| Month/Day/Year (US format) | 01/02/2006 |
03/15/2023 |
| Sunday, Month-Date-Year Time:Minute | Monday, 02-Jan-06 15:04 |
Wednesday, 15-Mar-23 10:00 |
Actual application example
Now, let's takestampToDateApply the label to the specific template code.
Display the publish date in the document list.Assuming you are iterating over a document list (
archives), and hope to display the creation time of each article.{% archiveList archives with type="list" limit="10" %} {% for item in archives %} <li> <a href="{{item.Link}}">{{item.Title}}</a> {# 显示格式为“2023-03-15” #} <span>发布于:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span> {# 显示格式为“2023年03月15日 10:00” #} <span>更新于:{{stampToDate(item.UpdatedTime, "2006年01月02日 15:04")}}</span> <span>阅读量:{{item.Views}}</span> </li> {% endfor %} {% endarchiveList %}Show detailed time on the document detail pageYou can directly use it on the detail page of a single document.
archive.CreatedTimeorarchive.UpdatedTime.<article> <h1>{{archive.Title}}</h1> <div> <span>发布日期:{{stampToDate(archive.CreatedTime, "2006年01月02日 15时04分05秒")}}</span> <span>最后更新:{{stampToDate(archive.UpdatedTime, "2006/01/02 15:04")}}</span> <span>浏览次数:{{archive.Views}}</span> </div> <div class="content"> {{archive.Content|safe}} </div> </article>
By these examples, you can seestampToDateThe flexibility and ease of use of the label. Just adjust the format string, and the timestamp can be presented in the way you want.
aboutdateFilter description
When using the AnQi CMS template, you may also notice that