Manage website content in AnqiCMS, we often need to display the publishing time, update time, or registration and login time of users, etc.These time information is usually stored in the database in the form of a timestamp, which is a sequence of numbers.If displayed directly on the website front end, these numbers are not intuitive and 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.
AnqiCMS's template system, draws on the syntax style of the Django template engine, powerful and easy to use. It contains a dedicated tag for formatting timestamps -stampToDateBy this tag, we can convert the original timestamp into various custom date and time strings, whether it is displaying the year, month, date, or accurate to the hour, minute, and second, it can be easily realized.
Understand the timestamp and formatting mode of AnqiCMS
Further understandstampToDateBefore the label, we first need to know the source and formatting mechanism of the timestamp in the AnqiCMS template.A 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.AnqiCMS'stampToDateThe label mainly handles 10-digit second-level timestamps.
And the formatting mode is a layout string unique to AnqiCMS, based on the Go language style. Unlike the commonY-m-d H:i:sThis pattern is different, Go language uses a specific reference date-2006-01-02 15:04:05Define the parts of the date and time. This means that you want to display the year, write '2006';Want to display 'month', write '01'; want to display 'day', write '02', and so on.
Here are some commonly used formatting patterns examples, convenient for us to understand and use:
- Year:
2006 - Month (two digits):
01 - Month (abbreviation):
Jan - Month (full):
January - Date (two digits):
02 - Weekday (abbreviation):
Mon - Weekday (full):
Monday - Hour (24-hour format):
15 - Hour (12-hour format):
03or04 - Minute:
04 - Seconds:
05 - AM/PM:
PMorpm
You can freely combine these "reference values" to create any date-time format you want.
Core operation: usestampToDateTag
stampToDateThe usage method of tags is very intuitive, its basic syntax is:
{{ stampToDate(时间戳变量, "格式化模式字符串") }}
here,时间戳变量Fields usually obtained from the backend, such as the object of the article,CreatedTime(Creation time) orUpdatedTime(update time), the object of the user,LastLogin(last login time), etc.格式化模式字符串This is the date and time format mentioned above, combined according to the Go language style.
Let's see how to apply it with some practical examples.
Suppose we are looping through a list of articles, each of which has 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 %}
On the article detail page, we may need to display the detailed publication 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 the scenario where user information needs to be displayed, such as the user's last login time:
{% 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>
By these examples, we can seestampToDateThe power of tags, which makes the display of time information extremely flexible
Practical tips
- Remember the reference date of the Go language:“2006 January 02 at 15:04:05” is the key to understanding all time formats.Which part do you want, use the corresponding number or text in this reference date to represent it.
- The source of timestamps:AnqiCMS's document model(
archive), classification model(category), user model(user) and others, usually all containCreatedTimeandUpdatedTimeThis timestamp field. It is convenient to get these timestamps when calling the corresponding tags. - Ensure it is a timestamp:
stampToDateThe tag is used specifically for handling timestamps. If your data field is already a formatted date string (e.g., "2023-10-26"), then it is not necessary 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,