In website content operations, time information plays an indispensable role.Whether it is the publication time of the article, the listing date of the product, or the submission time of the comments, a clear and easy-to-read date and time format can greatly enhance the user experience.Auto CMS is an efficient content management system that fully considers this point and provides flexible ways to format and display these timestamp data.
Understanding the timestamp in AnQi CMS
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 sequence of numbers representing the number of seconds from a specific moment (usually the Unix epoch, January 1, 1970, 00:00:00 UTC) to the time the event occurred.Although it is convenient for the system to store and calculate, this string of numbers is hard for visitors to understand.
Therefore, we need to convert these timestamps into a date and time format that people are familiar with on the front-end template, such as "October 27, 2023 14:30:00" or "this morning".
Core Tools:stampToDatetags
The AutoCMS provides a very practical built-in tag in the template, calledstampToDate专门用于将10位的时间戳格式化为可读的日期时间字符串。Its usage is intuitive and simple, only requiring two parameters: the timestamp you want to format and the date-time format you expect.
The basic syntax is like this:
{{stampToDate(您的时间戳, "您想要的格式")}}
The "Your timestamp" here is usually the one you get fromarchiveList/archiveDetail/commentListGet the content from the [auto] tags.CreatedTimeorUpdatedTimeand other fields.
Master the time formatting rules of Go language.
UnderstandingstampToDateThe label's key lies in understanding its "format" parameter.The AutoCMS is developed in Go language, therefore it follows the specific time formatting rules of Go language.Y-m-d H:i:sThis direct representation format, Go language uses a unique reference time to define date and time format.
This reference time is fixed:2006年01月02日15点04分05秒 -0700 MST.
You do not need to remember the meanings behind all these numbers, just remember this date, and then, according to the display effect you wish, replace the corresponding number or word in this reference time with the format separator 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 hours in 24-hour format with a leading zero, write
15. - If you want to display minutes with a leading zero, write
04. - If you want to display the number of seconds with a leading zero, write
05. - If you want to display the full name of the week, write
MondayIf you only want to display the abbreviation, writeMon.
Some common format examples:
| The format you want to display | Go language format string | Example output |
|---|---|---|
| Year-Month-Day | 2006-01-02 |
2023-10-27 |
| Year, Month, Day (Chinese) | 2006年01月02日 |
2023年10月27日 |
| Year/Month/Day Hour:Minute | 2006/01/02 15:04 |
2023/10/27 14:30 |
| Month/Day Hour:Minute: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 |
| Morning/Afternoon Time:Minute | 03:04PMor3:04 PM |
02:30PM |
| Precision 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 the article, it is usually the case thatarchiveListorarchiveDetailThe loop uses the labelstampToDate.
For example, display the publication 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 publication 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 using flexibilitystampToDateLabels and Go language-specific time formatting rules allow you to easily convert timestamps in your website to user-friendly date and time displays, making your content more vivid and practical.
Other time-related functions
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" %}
Common Questions (FAQ)
1. Why does the time formatting string of Aanqi CMS look so strange (for example2006-01-02)?This is because the Anqi CMS is developed based on Go language, and the 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.2006Represents the year,01Represents the month,02Represents a date. This approach may seem unusual at first glance, but once you understand its principles, you will find it very flexible and accurate.
2. I want to display 'today', 'yesterday', or 'a few minutes ago' such relative time,stampToDateCan it be done?
stampToDateTags are mainly used to format timestamps.AbsoluteThe date and time string (e.g., "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 custom Go language functions or front-end JavaScript logic to achieve it.Currently, the security CMS template tags of our company mainly focus on absolute time display, and future versions may consider adding more rich relative time display functions.
3. My article is set to be published at a scheduled time, but it is not displayed on the front end. What could be the reason?When editing articles on the AnQi CMS backend, you can set the 'Publish Time'.If this time is set to a future moment, this article will not be displayed on the front page before reaching the set time.This is the 'Time Factor - Scheduled Release Feature' of AnQi CMS, which aims to improve operational flexibility.Please check if the publish time you set has arrived.Once the time arrives, the article will be automatically published and displayed.