Manage website content in AnqiCMS, we often need to display the publication time, update time, or user registration and login time.These time information is usually stored in the database in the form of 'timestamp', which is a series of numbers.If the numbers are directly displayed on the website front-end, they are not only not intuitive but also lack aesthetic appeal.Fortunately, AnqiCMS provides very flexible template tags, allowing us to easily format these timestamps into the date and time display format we want.
The template system of AnqiCMS, drawing inspiration from the syntax style of Django template engine, is powerful and easy to learn. It includes a dedicated tag for formatting timestamps.stampToDateThrough this tag, we can convert the original timestamp into various custom date and time strings, whether it is displaying the year, month, day, or down to the hour, minute, and second, it can be easily achieved.
Understanding the timestamp and formatting pattern of AnqiCMS
Before getting a deeper understandingstampToDateBefore the tag, we first need to know the source and formatting mechanism of the timestamp in the AnqiCMS template.The timestamp is typically a 10-digit or 13-digit integer (Unix timestamp) representing the number of seconds or milliseconds from 00:00:00 UTC on January 1, 1970, to the current time.stampToDateThe tag mainly handles 10-digit second-level timestamps.
While the formatting mode is a unique feature of AnqiCMS, based on the Go language style of "layout strings," which is different from what we commonly see.Y-m-d H:i:sThis pattern is different, Go language uses a specific reference date -2006-01-02 15:04:05Define the components of dates and times.This means that if you want to display 'year', write '2006'; if you want to display 'month', write '01'; if you want to display 'day', write '02', and so on.
Here are some common formatting pattern examples for easy understanding and use:
- Year:
2006 - Month (two-digit):
01 - Month (abbreviation):
Jan - Month (full name):
January - Date (two digits):
02 - Day of the week (abbreviation):
Mon - Day of the week (full name):
Monday - Hour (24-hour format):
15 - Hour (12-hour format):
03or04 - Minutes:
04 - Seconds:
05 - AM/PM:
PMorpm
You can freely combine these 'reference values' to create any date-time format you want.
Core operation: UsestampToDatetags
stampToDateThe usage of tags is very intuitive, its basic syntax is:)
{{ stampToDate(时间戳变量, "格式化模式字符串") }}
Here,时间戳变量It is usually a data field obtained from the backend, such as the article object's)CreatedTime(Creation time) orUpdatedTime[Update time], the user object'sLastLogin[Last login time], etc.格式化模式字符串That is the date and time layout combined according to the Go language style mentioned above.
Let's look at some actual examples to see how to apply:
Suppose we are looping through a list of articles, each with aCreatedTimefield, which is a timestamp.
{% archiveList archives with type="list" limit="5" %}
{% for item in archives %}
<div class="article-card">
<h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
<p class="description">{{item.Description}}</p>
<p class="meta">
<!-- 显示为 "2023-10-26" -->
发布于:<span>{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
<!-- 显示为 "2023年10月26日" -->
<span>{{stampToDate(item.CreatedTime, "2006年01月02日")}}</span>
<!-- 显示为 "10-26" -->
<span>{{stampToDate(item.CreatedTime, "01-02")}}</span>
<!-- 显示为 "15:30:45" -->
<span>{{stampToDate(item.CreatedTime, "15:04:05")}}</span>
<!-- 显示为 "Oct 26, 2023" -->
<span>{{stampToDate(item.CreatedTime, "Jan _2, 2006")}}</span>
</p>
</div>
{% endfor %}
{% endarchiveList %}
In the article detail page, we may need to display the detailed publishing and update time of the article:
{% archiveDetail archive with name="Id" %} {# 假设这里获取了当前文章详情 #}
<article>
<h1>{{archive.Title}}</h1>
<p class="info">
发布时间:<span>{{stampToDate(archive.CreatedTime, "2006-01-02 15:04:05")}}</span>
更新时间:<span>{{stampToDate(archive.UpdatedTime, "2006年01月02日 星期一 下午03点04分")}}</span>
</p>
<div class="content">
{{archive.Content|safe}}
</div>
</article>
Even in scenarios where user information needs to be displayed, such as the last login time of the user:
{% userDetail userInfo with name="Id" id="1" %} {# 假设获取了ID为1的用户信息 #}
<div class="user-profile">
<h2>{{userInfo.UserName}}</h2>
<p>注册时间:<span>{{stampToDate(userInfo.CreatedTime, "2006年01月02日")}}</span></p>
<p>上次登录:<span>{{stampToDate(userInfo.LastLogin, "2006/01/02 15:04")}}</span></p>
</div>
Through these examples, we can seestampToDatethe powerful features of tags, which make the display of time information extremely flexible.
Useful tips
- Remember the reference date of the Go language:The key to understanding all time formats is “2006-01-02 15:04:05”.Which part do you want, use the corresponding number or text in this reference date to indicate it.
- The source of timestamps:AnqiCMS's document model (
archive), category model (category), user model (user) and others, usually all containCreatedTimeandUpdatedTimeThis timestamp field. You can easily obtain these timestamps when calling the corresponding tags. - Ensure it is a timestamp:
stampToDateTags are specifically used to handle timestamps. If your data field is already in a formatted date string (e.g., "2023-10-26"), then there is no need to usestampToDateIt can be directly output. If you are unsure whether a field is a timestamp, you can try using{{ item.CreatedTime|dump }}to view its type and value,