Time Wizard: Safe CMS in EnglishstampToDateHow to easily handle 10 timestamps with tags in English
AnQi CMS, as an enterprise-level content management system developed based on Go language, is well-versed in this field. In order to make content operators easily handle these timestamps and convert them into a friendly and understandable date format, AnQi CMS provides a powerful and flexible template tag: stampToDate。It is like a time wizard, able to instantly transform those boring 10-digit numbers into the various date and time expressions you want.
stampToDate:Convert machine time to human-readable time
Imagine when you need to display the publish time in a list of articles on a website, or mark the update date on a product detail page, and you see something like1609470335When this string of numbers is used instead of '2021-01-01' or '07:05 AM', the user experience will undoubtedly be greatly reduced.stampToDateTags are born to solve this pain point.
Its basic usage is very intuitive:{{stampToDate(时间戳, "格式")}}
The "timestamp" here is what we call a 10-digit number, usually in the documents of the CMS (such asCreatedTimeorUpdatedTimeField) is stored in this form. The "format", on the other hand,stampToDateis the key where the magic of labels happens. It follows the unique and highly flexible time formatting rules of the Go language.
Unveiling the Time Formatting of Go Language: Unique 'Reference Time'
May be used by some other content management systemsY-m-dorH:i:sThis symbol is different, the date formatting in Go language uses a unique 'reference time' -2006-01-02 15:04:05This does not mean that the timestamp will be formatted as 2006 or 3:15, but rather using each digit in this date as the reference for formatting.positionas the reference for formatting.
For example, if you want to convert a 10-digit timestamp1609470335This means2021-01-01 07:05:35 UTCFormatted into different common date formats, you can do this:
- Only display year, month, and day (separated by hyphens):
{{stampToDate(1609470335, "2006-01-02")}}it will be displayed as2021-01-01 - Only display hours, minutes, and seconds:
{{stampToDate(1609470335, "15:04:05")}}it will be displayed as07:05:35 - Display the complete year, month, day, hour, minute, and second:
{{stampToDate(1609470335, "2006-01-02 15:04:05")}}it will be displayed as2021-01-01 07:05:35 - Custom Chinese format:
{{stampToDate(1609470335, "2006年01月02日 15时04分")}}it will be displayed as2021年01月01日 07时05分 - Change date order (e.g., day/month/year):
{{stampToDate(1609470335, "02/01/2006")}}it will be displayed as01/01/2021
This "reference time" setting makes formatting extremely flexible. You just need to match the output format you expect,2006-01-02 15:04:05This is the reference time, write the corresponding number and separator. For example, if you want to display the abbreviation of the month, you can refer to the rule of the Go language and useJanto replace01,so that{{stampToDate(1609470335, "02 Jan 2006")}}it will be displayed as01 Jan 2021.
Practice in the Anqi CMS template.stampToDate
In the actual security CMS template development,stampToDateTags are usually witharchiveList/archiveDetailthese tags are used together to format the timestamp fields retrieved from the database.
For example, on an article list page, you might use it to display the publication date of each article:
{% archiveList latestArticles with type="list" limit="5" %}
{% for article in latestArticles %}
<article>
<h2><a href="{{ article.Link }}">{{ article.Title }}</a></h2>
<p>发布于:{{ stampToDate(article.CreatedTime, "2006年01月02日") }}</p>
<p>{{ article.Description }}</p>
</article>
{% endfor %}
{% endarchiveList %}
Or on the article detail page, to display the update time of the article:
<article>
<h1>{% archiveDetail with name="Title" %}</h1>
<p>
最后更新:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04") }}
</p>
<div>
{%- archiveDetail articleContent with name="Content" %}
{{articleContent|safe}}
</div>
</article>
Whether you need an update log precise to the second, or a concise release date,stampToDateThe label can be adjusted by simple format parameters to meet all your needs.This not only greatly improves the readability of the content, but also makes the overall information presentation of the website more professional and friendly.The Auto CMS is dedicated to providing such practical and efficient features, making content operations more convenient.
Common Questions and Answers (FAQ)
1. The timestamp I get from the database is not 10 digits,stampToDateCan the tag still be used?
stampToDateThe tag is designed for a standard 10-digit Unix timestamp.If your timestamp is 13 digits (millisecond level), you need to divide it by 1000 to convert it to 10 digits, or handle it in front-end JavaScript and other ways.stampToDateIt may result in incorrect output.
2. Besides the examples in the article, what are some common date formatting references in the Go language? Where can I find more?Go language's date formatting is very rich, in addition to2006-01-02 15:04:05, there are also many combinations, such as:
Mon Jan _2 15:04:05 MST 2006(Standard CST format, such as:)周一 1月 01 07:05:35 UTC 2021)2006-01-02T15:04:05Z07:00(ISO 8601 standard format)3:04PM(Time format) You can find the detailed instructions for the "Timestamp Formatting Tag" in the "Template Creation" document of Anqi CMS (i.e.tag-stampToDate.md),where it lists more detailed formatting examples.
**3.