In the website template of AnQi CMS, displaying the publication and update time of articles is a key link to improve user experience and content management efficiency.Clearly present these time information, not only can it help visitors quickly understand the timeliness of the content, but also helps search engines better crawl and index.AnQiCMS provides a powerful and flexible template tag that allows you to format timestamps into various easy-to-read date and time formats as needed.
Core Mechanism:stampToDatetags
The template system of Anqi CMS is built-in with a namedstampToDateThe formatted timestamp label, which is the core tool for handling article publishing and update times. This label is used to convert the 10-digit Unix timestamp stored in article data (for example1609470335)Converted to the date and time string commonly used in our daily life.
stampToDateThe usage of tags is very intuitive, it requires two main parameters:
- Timestamp (timestamp)This is the original time data you want to format, usually obtained from the article object, such as
archive.CreatedTimeoritem.UpdatedTime. - format string (format_string)This is a string defining the output format. Used by many CMS systems
Y-m-d H:i:sThis placeholder is different, AnQiCMS follows the time formatting rules of the Go language, it uses a fixed reference time2006-01-02 15:04:05Come as a format definition "template". You need to replace the year, month, day, hour, minute, second, and other elements in this reference time with the display style you want.
For example, if you want to display年-月-日Then the format string is"2006-01-02";if you want to display月/日 时:分Then the format string is"01/02 15:04".
Get the article timestamp
In AnQiCMS templates, the publication time of articles is usually obtained throughCreatedTimefield, while the update time is obtained throughUpdatedTimefield. These fields are stored in the form of Unix timestamps.
When you are on the article detail page (such asdetail.html)时,可以直接通过全局的archive对象来访问这些时间戳:
archive.CreatedTime用于发布时间archive.UpdatedTime用于更新时间
如果您在循环输出文章列表(例如list.htmlor homepage) at that time, article data is usually defined as a loop variable, for exampleitem. In this case, you can access the timestamp like this:
item.CreatedTimeitem.UpdatedTime
formatting practice examples
Below are some common formatting scenarios and their corresponding template codes:
1. Show only year-month-day
This is one of the most commonly used date display formats, for example2023-10-26.
{# 在文章详情页显示发布日期 #}
<span>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</span>
{# 在文章列表中显示更新日期 #}
{% for item in archives %}
{# ... 其他文章信息 ... #}
<span>更新于:{{ stampToDate(item.UpdatedTime, "2006-01-02") }}</span>
{% endfor %}
2. Show the full year-month-date time:minute:second
If you need time information accurate to the second, for example2023-10-26 14:35:01.
{# 显示发布时间的完整格式 #}
<span>发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04:05") }}</span>
3. Show the date in Chinese format
For example2023年10月26日.
{# 显示中文格式的发布日期 #}
<span>发布于:{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}</span>
4. Only show the month and day, or only show the hour and minute
You can extract a specific part of the timestamp as needed.
{# 只显示月日,例如 10-26 #}
<span>{{ stampToDate(archive.CreatedTime, "01-02") }}</span>
{# 只显示时分,例如 14:35 #}
<span>{{ stampToDate(archive.CreatedTime, "15:04") }}</span>
5. Combine other text and HTML tags
You can combine formatted time with other text, icons, or HTML structures to achieve a richer display effect.
<p>
<i class="fa fa-calendar"></i> 发布于 <time datetime="{{ stampToDate(archive.CreatedTime, "2006-01-02T15:04:05Z07:00") }}">
{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}
</time>
</p>
In the above example,datetimeProperty uses the ISO 8601 format, which is more friendly to search engines and accessibility tools, and users see a more readable Chinese format.
Summary
PassstampToDateLabel, AnQiCMS provides great flexibility to control the display of article publishing and update times.Understanding the reference time formatting rules of the Go language is the key to mastering its usage.Once familiar, you can easily present various date and time formats that meet design and user needs in templates, thereby optimizing the display of your website content.
Common Questions (FAQ)
1. Why do I set the format string according toYYYY-MM-DDbut the result displayed is2006-01-02?This is usually because you have misunderstood the Go language's time formatting rules. In Go language (as well as AnQiCMS templates), the format string is not usedYYYY/MMInstead of using placeholders, a specific reference date is used2006-01-02 15:04:05You need to replace the corresponding part of this reference time with the style you want to output. For example,年-月-日you should use"2006-01-02"As a format string, notYYYY-MM-DD.
2. How can I display the current date and time in the template, in addition to the publication and update time of the article?You can use the AnQiCMS providednowTag to get and display the current date and time. This tag also follows the formatting rules of the Go language. For example, to display the current year-month-day, you can use{% now "2006-01-02" %}To display the current full date and time, use{% now "2006-01-02 15:04:05" %}.
3. Can I display the date as "todayAnQiCMSstampToDateLabels are mainly used to format timestamps into fixed pattern date and time strings, and do not directly support relative time displays such as "todayIf you need such dynamic relative time display, it usually needs to be implemented through JavaScript on the front end, or passed to the template after the business logic is calculated on the back end.stampToDateOutput the date in standard format (e.g., ISO format), and then use a JavaScript library (such as Moment.js or date-fns) or a custom script to convert it to relative time after the page is loaded.