As an experienced CMS website operation personnel of an enterprise, I know that every detail of user experience is crucial in content management.The clear presentation of dates and times often directly affects readers' perception of the timeliness and professionalism of the content.Today, I will give a detailed explanation of how to convert those seemingly mysterious timestamps in AnQiCMS templates into the readable date and time format that we are accustomed to.
Understand the timestamp formatting mechanism of AnQiCMS
In AnQiCMS, the database usually stores Unix timestamps, which are represented as integers indicating the number of seconds elapsed since 00:00:00 UTC (Coordinated Universal Time) on January 1, 1970.Although this format is convenient for system processing and storage, it lacks intuitiveness for users.stampToDateUsed to format timestamps into familiar dates and times in front-end templates.
stampToDateThe core usage of tags
stampToDateThe usage of tags is very direct, and its basic syntax structure is as follows:
{{ stampToDate(时间戳, "格式") }}
Here时间戳Generally, fields are obtained from AnQiCMS data objects (such as articles, products, etc.), for example:item.CreatedTimeorarchive.UpdatedTime.格式The parameter is used to define the style in which you want the date and time to be displayed.
It should be noted that,格式The parameter follows the specific time formatting rules of the Go language. Go language does not use it like PHP or PythonY-m-d H:i:ssuch symbols, but rather adopted a fixed reference time2006-01-02 15:04:05Define the format. This means that you just need to replace each part of this reference time string with the time component you want to display.
Common timestamp field acquisition
In the AnQiCMS template, articles (archive) or list items (item) usually contain the following timestamp fields, which are all 10-digit Unix timestamps:
CreatedTime: Indicates the creation time of the content.UpdatedTime: Indicates the last update time of the content.
For example, on a document detail page, you might directly go througharchive.CreatedTimeGet the creation timestamp; while in a document list loop, you will go throughitem.CreatedTimeto get the creation timestamp of each list item.
Go language time formatting example
The reference time to master the Go language is to use it correctlystampToDateThe key. Here are some common formatting examples that can help you quickly understand and apply:
- Only display the date (year-month-date):
{{ stampToDate(publishStamp, "2006-01-02") }}- Example Output:
2021-06-30
- Example Output:
- Show only the date (Year/Month/Day):
{{ stampToDate(publishStamp, "2006/01/02") }}- Example Output:
2021/06/30
- Example Output:
- Show the full date and time (Year-Month-Day Hour:Minute:Second):
{{ stampToDate(publishStamp, "2006-01-02 15:04:05") }}- Example Output:
2021-06-30 12:00:00
- Example Output:
- Show Chinese date:
{{ stampToDate(publishStamp, "2006年01月02日") }}- Example Output:
2021年06月30日
- Example Output:
- Show hour and minute:
{{ stampToDate(publishStamp, "15:04") }}- Example Output:
12:00
- Example Output:
- Show the day of the week and the date:
{{ stampToDate(publishStamp, "Mon Jan _2 2006") }}- Example Output:
Wed Jun 30 2021(Note in Go language_2Used for placeholder single-digit day characters, if you expect two-digit display, you should use02)
- Example Output:
Actual application scenarios and code examples
In the AnQiCMS template, you can use timestamps anywhere you need to display themstampToDate.
Display the publication time of articles on a document list page:
Suppose you are usingarchiveListTag loops to display the article list and hopes that each article shows its publication date.
{% archiveList archives with type="page" limit="10" %}
{% for item in archives %}
<div class="article-item">
<h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
<p>发布时间:{{ stampToDate(item.CreatedTime, "2006年01月02日 15:04") }}</p>
<p>{{ item.Description }}</p>
</div>
{% endfor %}
{% endarchiveList %}
Show the article update time on the document detail page:
On the detail page of a single document, you may want to display the latest update time of the article.
<article>
<h1>{{ archive.Title }}</h1>
<p>作者:{{ archive.Author }}</p>
<p>更新时间:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05") }}</p>
<div class="article-content">
{{ archive.Content|safe }}
</div>
</article>
Display the copyright year of the website in the footer (using the current year):
Although it is not a timestamp, butnowThe label is also related to time, it can obtain the current time and format it, often used to display the dynamic year in the footer.
<footer>
<p>版权所有 © {% now "2006" %} {{ system.SiteName }}。保留所有权利。</p>
</footer>
Summary
stampToDateThe tag is an indispensable tool for handling timestamps in the AnQiCMS template.By using it, you can flexibly convert the Unix timestamp passed from the backend into a user-friendly date and time format, thereby greatly enhancing the professionalism and user experience of the website content.Remember the reference time format of Go language and adjust it according to your actual needs to easily handle the time display on the website.
Frequently Asked Questions
Q1: Why does my timestamp formatting show incorrect or directly show the year 1970?
The most common reason is provided时间戳An invalid 10-digit Unix timestamp was entered or provided格式The string does not match the time formatting rules of the Go language. Please ensure that the timestamp obtained from the database is a numeric type and the length is correct (usually 10 digits), and carefully check that the format string you are using matches the Go language exactly.2006-01-02 15:04:05Refer to the time rule. For example, if you want to display “month”, you should use01instead ofMM.
Q2: Can I usearchive.CreatedTimeoritem.CreatedTimein the template withoutstampToDateprocessing
directly{{ archive.CreatedTime }}or{{ item.CreatedTime }}The original 10-digit Unix timestamp is usually output.Although it is technically feasible, this will severely affect user experience because most users cannot intuitively understand the dates and times represented by these numbers.stampToDateLabel formatting to provide more readable information.
Q3:stampToDateDoes the label support displaying relative time, such as '5 minutes ago'?
Built-in in AnQiCMSstampToDateThe tag is mainly used to format timestamps into fixed date and time strings.It does not provide the relative time display function such as 'N minutes ago' directly.If you need to implement relative time display, you may need to combine it with a frontend JavaScript library (such as Moment.js or date-fns) to handle it, or look for whether AnQiCMS community provides custom filters or plugins with this feature.