In website content operations, time information plays a vital role.Whether it is the release time of an article, the listing date of a product, or the submission time of a comment, a clear and easy-to-read date and time format can greatly enhance user experience.AnQi CMS as an efficient content management system, fully considers this point, and provides a flexible way to format and display these timestamp data.
Understanding the timestamp in Anqicms
In the AnQi CMS backend, when we publish articles, products, or perform other content management operations, the system usually records the time of these events as a "timestamp".In simple terms, a timestamp is a series of numbers representing the seconds from a specific moment (usually the Unix epoch, January 1, 1970, 00:00:00 UTC) to the occurrence of the event.Although it is convenient for system storage and calculation, this string of numbers is difficult for visitors to understand.
Therefore, we need to convert these timestamps into a familiar date and time format on the front-end template, such as
Core Tool:stampToDateTag
The AnQi CMS provides a very practical built-in tag in the template, calledstampToDateThis is used to format a 10-digit timestamp into a readable date and time string.Its usage is intuitive and simple, only two parameters are needed: the timestamp you want to format, and the date-time format you expect.
The basic syntax is as follows:
{{stampToDate(您的时间戳, "您想要的格式")}}
The 'Your timestamp' here is usually the one you get fromarchiveList/archiveDetail/commentListThe content obtained from the tagsCreatedTimeorUpdatedTimefields.
Master the time formatting rules of Go language
UnderstandingstampToDateThe key is to understand the "Format You Want" parameter.AnQi CMS is developed in Go language, therefore it follows the special time formatting rules of Go language.different from commonY-m-d H:i:sThis format is directly represented, Go language uses a unique reference time to define the date and time format.
This reference time is fixed:2006年01月02日15点04分05秒 -0700 MST.
You do not need to remember the meaning behind all these numbers, just remember this date, and then replace the corresponding numbers or words in the reference time according to the display effect you want, with the format delimiter you want.
For example:
- If you want to display the year, write
2006. - If you want to display the month (with leading zero), write
01. - If you want to display the date (with leading zero), write
02. - If you want to display the hour in 24-hour format (with a leading zero), write
15. - If you want to display the minutes (with a leading zero), write
04. - If you want to display the seconds (with a leading zero), write
05. - If you want to display the full name of the day of the week, write
Monday; If you only want to display the abbreviation, writeMon.
Some common format examples:
| The format you want to display | Go language formatting string | Sample output |
|---|---|---|
| Year-Month-Date | 2006-01-02 |
2023-10-27 |
| Year/Month/Day (Chinese) | 2006年01月02日 |
2023年10月27日 |
| Year/Month/Day Time:Minute | 2006/01/02 15:04 |
2023/10/27 14:30 |
| Month/Day Time:Second | 01/02 15:04:05 |
10/27 14:30:00 |
| Day of the Week, Year Month Day | Mon, 02 Jan 2006 |
Fri, 27 Oct 2023 |
| AM/PM Time:Minute | 03:04PMor3:04 PM |
02:30PM |
| Accurate to milliseconds (or more) | 2006-01-02 15:04:05.000 |
2023-10-27 14:30:00.123 |
Actual application: Formatting time in templates
When you need to display the publication time or update time of an article, it is usually inarchiveListorarchiveDetailthe loop body of the tagstampToDate.
For example, display the publish and update time on the document detail page:
<article>
<h1>{{archive.Title}}</h1>
<div>
发布时间:<span>{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04:05")}}</span>
{% if archive.CreatedTime != archive.UpdatedTime %}
<span>更新时间:{{stampToDate(archive.UpdatedTime, "2006年01月02日 15:04:05")}}</span>
{% endif %}
</div>
<div>
{%- archiveDetail articleContent with name="Content" %}
{{articleContent|safe}}
</div>
</article>
Or, display the publish date of each article in the article list:
<div>
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<li>
<a href="{{item.Link}}">
<h5>{{item.Title}}</h5>
<div>{{item.Description}}</div>
<div>
<span>发布于:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
<span>阅读量:{{item.Views}}</span>
</div>
</a>
</li>
{% empty %}
<li>
当前没有任何内容
</li>
{% endfor %}
{% endarchiveList %}
</div>
By flexible applicationstampToDateTags and Go language-specific time formatting rules allow you to easily convert timestamps on websites into user-friendly date and time displays, making your content more vivid and practical.
Other time-related features
exceptstampToDateTags, AnQi CMS also providesnowTags, If you need to display the current server time on the page, you can use it directly, and its formatting rules are the same asstampToDatethe same:{% now "2006年01月02日 15:04:05" %}
Frequently Asked Questions (FAQ)
1. Why does the time formatting string of AnQi CMS look so strange (for example2006-01-02)?This is because Anqi CMS is developed based on Go language, and Go language uses a fixed reference date and time as a template for time formatting. This reference time is2006年01月02日15点04分05秒 -0700 MST. You just need to remember this "template", and then replace the part you want to display with the corresponding number or letter in the reference time. For example,2006represents the year,01represents the month,02Represents a date. This approach, while initially may seem unusual, once you understand its principles, you will find it very flexible and precise.
2. I want to display relative time such as 'today', 'yesterday', or 'a few minutes ago',stampToDateCan it be done?
stampToDateTags are mainly used to format timestamps asabsolutedate and time string (for example, "2023 October 27 14:30").It does not have the function to calculate relative time. If you need to display "N minutes ago", "M hours ago" such relative time, you may need to combine with custom Go language functions or front-end JavaScript logic to implement.At present, AnQi CMS template tags mainly focus on absolute time display, and future versions may consider adding more rich relative time display features.
3. Why is the scheduled publication time of my article not displayed on the front page?When editing articles on the Anqi CMS backend, you can set the "publish time".If this time is set to a future moment, then this article will not be displayed on the front page before reaching the set time.This is the AnQi CMS 'Time Factor - Scheduled Release Feature' designed to improve operational flexibility.Please check if the publication time you set has arrived. Once the time arrives, the article will be automatically published and displayed.