In daily website content operation, time information plays a crucial role.Whether it is the publication date of the article, the launch time of the product, or the latest update of the content, these time points all directly affect the user's perception and trust in the information.However, in the templates of content management systems, we often encounter dates stored in the form of "timestamps", which are precise but not intuitive.How should we correctly format these timestamps in the AnQiCMS template so that the content can be displayed in the way we expect?
Understanding how AnQiCMS handles time information
In AnQiCMS, the content creation time (CreatedTimeAnd update time(UpdatedTime)are usually stored in databases in the form of standard Unix timestamps.These timestamps are typically 10 or 13 digits, representing the number of seconds or milliseconds since January 1, 1970, 0:0:0 (UTC) to the present.For website visitors, directly display1678886400Such numbers are obviously meaningless and unfriendly. To improve user experience, we need to convert them into the date and time format we are accustomed to reading in our daily lives, such as "March 15, 2023
AnQiCMS provides a very practical template tag for this——stampToDateIt can help us easily convert the original timestamp into various readable date and time formats.
stampToDateThe core usage of tags
stampToDateThe label usage is very concise and clear, its basic syntax is: {{stampToDate(时间戳, "格式")}}.
This is especially noteworthy, the date format string used by AnQiCMS is based on the special reference time of the Go language——2006-01-02 15:04:05This means that you cannot use placeholders as you would in other systemsYYYY-MM-DDInstead, you should use the corresponding numbers in the reference time to represent year, month, day, hour, minute, and second.
Let us understand this format through some specific examples:
- Year (Year): Use
2006Represent a four-digit year. - Month (Month): Use
01orJanTo represent two-digit months or month abbreviations. - Date (Day): Use
02or_2To represent a two-digit date or a single-digit date. - Hour (Hour): Use
15(24-hour format) or03To represent an hour in 12-hour format. - Minute (Minute): Use
04To represent a minute. - Seconds (Second): Use
05to represent seconds.
The following are some common formatting examples and their expected outputs:
- Display the full year, month, and day:
{{stampToDate(时间戳, "2006-01-02")}}→2023-03-15{{stampToDate(时间戳, "2006年01月02日")}}→2023年03月15日
- Display the year, month, and day with time:
{{stampToDate(时间戳, "2006-01-02 15:04")}}→2023-03-15 14:30{{stampToDate(时间戳, "2006/01/02 15:04:05")}}→2023/03/15 14:30:00
- Show only the month and date:
{{stampToDate(时间戳, "01-02")}}→03-15
- Only show the time:
{{stampToDate(时间戳, "15:04")}}→14:30{{stampToDate(时间戳, "03:04 PM")}}→02:30 PM(12-hour AM/PM notation)
Having mastered this Go language reference time format, you can flexibly combine it according to your needs to display various complex date and time effects.
Apply in content template
Now we know how to usestampToDateNext, it is how to get the timestamp and format it in the actual content template of AnQiCMS.
Suppose we are developing a template for an article detail page that needs to display the article's publication time. In AnQiCMS, the context of the article detail page usually has aarchivean object that containsCreatedTimea field. We can format it like this:
<article>
<h1>{{archive.Title}}</h1>
<p>发布于:{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04")}}</p>
<div>
{{archive.Content|safe}}
</div>
</article>
here,archive.CreatedTimewill pass the timestamp of the article creation,"2006年01月02日 15:04"Then it defines the format we want it to display as "Year-Month-Day Hour:Minute".
If we need to traverse multiple articles and display their publish time on an article list page, we can do it like this:
<ul>
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<li>
<h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
<p>
<span>分类:{% categoryDetail with name="Title" id=item.CategoryId %}</span>
<span>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
<span>浏览量:{{item.Views}}</span>
</p>
<p>{{item.Description}}</p>
</li>
{% empty %}
<li>目前还没有内容发布。</li>
{% endfor %}
{% endarchiveList %}
</ul>
In this example,item.CreatedTimeisforLoop to obtain the timestamp of each article and format it as年-月-日in a concise form.
Some practical skills and considerations
- Maintain consistencyIn different pages and different types of content on the website, try to maintain consistency in the date and time format to avoid confusing users.
- Consider multilingual support: If your website supports multilingual, you may need to use date formats that are in line with local customs in different language versions. Although
stampToDateIt does not directly provide multilingual localization, but you can combine conditional judgment or the multilingual support feature of AnQiCMS in the template logic. - Processing without time setting: If a content's time field may be empty, it may cause problems during template rendering. In actual development, it can be combined with
ifDetermine to ensure that formatting and display only occur when the timestamp exists. For example:{% if archive.CreatedTime %} <p>发布于:{{stampToDate(archive.CreatedTime, "2006年01月02日")}}</p> {% else %} <p>发布时间未知</p> {% endif %}
By flexible applicationstampToDateTags and Go language date formatting rules, you can easily implement various date and time display as needed in the AnQiCMS template, thus providing your website users with a more friendly and professional browsing experience.
Frequently Asked Questions (FAQ)
Q1: Why is the time formatting string of AnQiCMS not common?Y-m-doryyyy-MM-dd?A1: AnQiCMS is developed based on Go language, it follows the unique specification of Go language in date and time formatting. Go language does not use symbol placeholders, but uses a specific reference date2006-01-02 15:04:05Also known as "Go time" or "reference time" to define various formats.This means that you need to provide the numbers or text in the format string that correspond to the reference time you want to output.For example, to display a four-digit year, use the year from the reference time2006.
Q2: How can I display the publication year of the content only?A2: If you want to display only the publication year, you can setstampToDatethe format parameter of the tag to `"