The website content is updated frequently, each article, product, or comment has its creation and update time.However, these times are usually stored in the background in the form of a string of numbers - that is, what we commonly call timestamps.It is not friendly to visitors to directly display these timestamps; we need to convert them into a clear date and time format.
In AnQiCMS, formatting these timestamps into the date and time format we are accustomed to is actually very simple and intuitive.The system has a powerful template tag built-in that can help us easily achieve this.
Core Tool:stampToDateTag
AnQiCMS provides a namedstampToDateA template tag specifically used to convert Unix timestamps (usually 10 or 13-digit integers) into readable date and time strings. This tag is very flexible and requires two key pieces of information:
- Formatted timestamp:This is usually what you get from the database
CreatedTimeorUpdatedTimefields. - Formatting string:This is the key to define the style of date and time display you want.
Its basic syntax is as follows:{{stampToDate(时间戳, "格式化字符串")}}
It needs to be particularly explained that "formatting strings". AnQiCMS is developed based on Go language, therefore it follows the unique time formatting rules of Go language.used with many other programming languagesY-m-d H:i:ssuch placeholders are different, Go language uses a specific oneReference timeto serve as a template:2006-01-02 15:04:05. Just remember this reference time and replace the corresponding part of the reference time with the style you want to display.
For example:
- If you want to display
2023-10-26Then the format string is"2006-01-02". - If you want to display
2023年10月26日Then the format string is"2006年01月02日". - If you want to display
2023-10-26 14:30:15Then the format string is"2006-01-02 15:04:05".
Application scenarios in practice
ThisstampToDateThe tag can be used anywhere in the AnQiCMS template.No matter whether you are displaying an article list, article details, the user's recent login time, or the comment posting time, whenever you encounter a timestamp field, you can use it to beautify the display.
For example, on the article detail page (usually correspondingarchiveDetailto the tag data obtained) display the article's publishing and update time:
<article>
<h1>{{ archive.Title }}</h1>
<p>
<span>发布于:{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04")}}</span>
<span>更新于:{{stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05")}}</span>
</p>
<div>
{{ archive.Content|safe }}
</div>
</article>
On the article list page (usingarchiveListThe data label is used to display the creation date of each article:
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<li>
<a href="{{item.Link}}">
<h5>{{item.Title}}</h5>
<p>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</p>
</a>
</li>
{% empty %}
<li>暂无文章内容。</li>
{% endfor %}
{% endarchiveList %}
Display the comment publish time in the comment list:
{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
{% for item in comments %}
<div>
<span>{{item.UserName}}</span> 于 <span>{{stampToDate(item.CreatedTime, "2006年01月02日 15:04")}}</span> 评论道:
<p>{{item.Content}}</p>
</div>
{% endfor %}
{% endcommentList %}
More flexible format customization
The reference time format of the Go language is very powerful, you can combine its elements to meet various complex date and time display needs:
- Show the day of the week:Use
Mon(abbreviation for Monday) orMonday(Monday full name).- For example:
"2006年01月02日 Monday"Will be displayed2023年10月26日 Thursday.
- For example:
- Show AM/PM:Use
PMorpm.- For example:
"15:04 PM"Will be displayed02:30 PM(2:30 PM).
- For example:
- Only show month and year:
- For example:
"2006年01月"Will be displayed2023年10月.
- For example:
You can freely combine these reference elements to create a unique date and time format according to your needs.
Other date/time related tags and filters
exceptstampToDate,AnQiCMS also provides some other auxiliary functions related to date and time:“
- Display the current system time:“If you need to display the current server time on the page, you can use
{% now "格式化字符串" %}tags. It also follows the formatting rules of the Go language.<p>当前时间:{% now "2006年01月02日 15:04:05" %}</p> dateFilter:for those who are already using Go languagetime.TimeType variables, you can also usedateFormatter. But please note,dateFormatter only applies totime.TimeThe type, used directly on integer timestamps will cause an error. Most of the time data in AnQiCMS templates are provided in timestamp format, thereforestampToDateIs the preferred method for handling timestamps.
In summary, AnQiCMS is intuitive.stampToDateTags and the flexible reference time format of Go language make the time display of website content simple and expressive.Master this little trick, and it will make your website content more readable and professional.
Frequently Asked Questions (FAQ)
1. Why does AnQiCMS use the number combination '2006-01-02 15:04:05' as the formatting string when formatting dates?
This is the special time formatting method of Go language. It is different from the use of symbols (such asYrepresents the year,mrepresenting the month) in many programming languages, where Go language provides a specific reference time2006-01-02 15:04:05(That is, at 3:04:05 PM on January 2, 2006) as the formatted "template".You just need to write the desired display format according to the meaning of the numbers in this reference time, and the system will automatically apply it to the actual timestamp.For example, to display the full year, use2006.
2. How can I set the format to display the day of the week or AM/PM?
To display the day of the week, you can use the reference time in theMon(abbreviation for Monday) orMondayRepresenting the full name of Monday. For example,"2006年01月02日 Monday"It will display `2023