How to use the `stampToDate` tag to format timestamps in AnQiCMS templates?
As an experienced website operator deeply familiar with AnQi CMS, I know the importance of content timeliness and presentation for user experience.In website content management, a timestamp (Timestamp) is a common method for recording the time of content publication and updates, but directly displaying a timestamp is not intuitive for users.Therefore, formatting timestamps into a readable date and time format is a key aspect in enhancing the professionalism and user-friendliness of a website.AnQiCMS provides a very convenient template tagstampToDateMake this operation easy.
UnderstandingstampToDateTags and their uses
In the AnQiCMS template system,stampToDateThe tag is a powerful auxiliary tool, specifically used to convert Unix timestamps into human-readable date and time strings. Whether you want to display the publication date of an article, the update time of a product, or any other data based on timestamps,stampToDateCan help you present this information in a clear and unified format.The existence of this tag greatly simplifies the work of front-end developers handling time data, eliminating the need for complex programming logic, and can be achieved with simple template syntax.
stampToDateThe syntax and parameter parsing of the tag
stampToDateThe calling syntax of the tag is very intuitive:{{stampToDate(时间戳, "格式")}}. Let's analyze the two core parts of this syntax in detail.
The first parameter isTimestamp (Timestamp). It is usually a 10-digit Unix timestamp representing the number of seconds from the Unix epoch (January 1, 1970, 00:00:00 UTC) to a specific time.In AnQiCMS content tags (such asarchiveDetailorarchiveList) obtained.CreatedTimeorUpdatedTimefields, their original value is such a timestamp. You need to pass these field values directly asstampToDateas the first parameter.
the second parameter isFormat StringThis is the key to determining how the date and time will be displayed.AnQiCMS's template engine is based on the Go language, therefore, the time formatting string here follows the specific reference time format of the Go language.Go language uses a fixed reference time2006-01-02 15:04:05.999999999 -0700 MSTIt serves as the template for all date and time formatting. This means you cannot use arbitrarilyYYYY-MM-DDSuch a general placeholder, but instead you need to use the corresponding numbers or words in this reference time to represent the year, month, day, hour, minute, and second you want.
Here are some commonly used formatting string examples and their corresponding output effects to help you quickly understand and apply them:
"2006-01-02": Will output年-月-日For example2021-06-30"2006年01月02日": Will output年年年年年年年年For example2021年06月30日"15:04:05": Will output时:分:秒For example12:30:00"2006-01-02 15:04": Will output年-月-日 时:分For example2021-06-30 12:30"Mon Jan _2 15:04:05 2006": Complete reference time format in Go language, for exampleWed Jun 30 12:30:00 2021
Pay close attention to the accuracy of the format string, even if it is01and1Such subtle differences can affect the final display results. For example,01Represents a two-digit month (leading zeros if less than ten). For example,1It represents a month represented by one or two digits (without leading zeros).
stampToDateActual application cases
In the template development of AnQiCMS,stampToDateTags are usually tags related to content (such asarchiveDetailTo get the details of a single document,archiveListRetrieve document list) combined to format the publication time or update time of these content items.
Assuming you are building an article detail page, you need to display the article's publish date. You can usearchiveDetailtags to retrieve the article data, and thenCreatedTimethe field passed instampToDate:
{# 假设当前页面是文章详情页,archiveDetail 默认获取当前文档 #}
<div>
<span>文章标题:{% archiveDetail with name="Title" %}</span>
<span>发布日期:{{stampToDate(archive.CreatedTime, "2006年01月02日")}}</span>
<span>精确到秒:{{stampToDate(archive.CreatedTime, "2006-01-02 15:04:05")}}</span>
</div>
{# 如果您想获取指定ID文章的更新时间,也可以这样做: #}
{% archiveDetail specifiedArchive with id="123" %}
<span>文章ID 123的更新时间:{{stampToDate(specifiedArchive.UpdatedTime, "2006/01/02 15:04")}}</span>
{% endarchiveDetail %}
On the article list page, you may need to iterate over multiple articles and display their publication time:
{% archiveList archives with type="list" limit="5" %}
{% for item in archives %}
<div>
<h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
<p>发布于:{{stampToDate(item.CreatedTime, "2006-01-02")}}</p>
</div>
{% endfor %}
{% endarchiveList %}
By these examples, you can seestampToDateHow to flexibly format timestamps in different scenarios to meet various content display requirements.
stampToDateThe importance and practice
Properly formatting time information is crucial for enhancing the user experience of a website.Clear dates and times help users quickly understand the publication and update of content, enhancing the reliability of information.At the same time, a unified time format also makes the website look more professional and standardized.
In practice, it is recommended that you:
- Maintain consistencyAvoid using the same format for similar time information throughout the website to prevent user confusion.
- Consider the user's region.If your website is aimed at global users, you may need to consider using different date formats (such as American date format
MM/DD/YYYYor European date formatDD/MM/YYYY),AnQiCMS's multilingual support and custom template capabilities can help you achieve this. - Avoid redundant informationIf you only need the date, don't show the time, and vice versa. Concise and clear is the best.
Summary
stampToDateThe label is a practical and efficient tool in the AnQiCMS template system, making timestamp formatting simple and powerful.By precisely controlling the format string, you can convert the original timestamp data into dates and times that meet various display requirements, thereby optimizing the presentation of website content and enhancing the reading experience of users.Mastering the use of this tag will bring greater flexibility and convenience to the operation of your AnQiCMS website.
Frequently Asked Questions (FAQ)
1. WhystampToDateThe format string should be used2006-01-02 15:04:05Instead of such a number combination,YYYY-MM-DDsuch general placeholders?
This is because the template engine of AnQiCMS is based on Go language. When designing its date and time formatting function, Go language chose a fixed reference time2006-01-02 15:04:05.999999999 -0700 MSTAs a template. The format string you provide actually tells the Go language how to parse or rewrite the parts of the reference time to generate the date and time format you expect.For example, when you write2006When, it indicates the extraction of the year; write01When, it indicates extracting the month (with leading zeros). This design may seem special at first glance, but once you understand its logic, you can very flexibly customize various time formats.
2. The timestamp I received is not 10 digits, but 13 digits (millisecond level),stampToDateCan the tag still be used?
stampToDateThe tag is designed to handle 10-digit Unix second-level timestamps.If your timestamp is a 13-digit millisecond timestamp, you need to process it before passing the label, converting it to a second timestamp.In some template environments, you may need to use mathematical operations to divide it by 1000.For example, ifitem.MilliCreatedTimeIt is a millisecond timestamp, you may need{{stampToDate(item.MilliCreatedTime / 1000, "2006-01-02")}}Please confirm the source of your timestamp and its accuracy, and make the corresponding adjustments.
3. How tostampToDateWhich day of the week is displayed (e.g., "Monday", "Monday")?
To display what day of the week it is, you need to use the corresponding part of the time representation in the Go language.
- If you want to display the full name of the week (such as
Monday), you can useMondayAs part of the format string, for example:{{stampToDate(item.CreatedTime, "Monday, 2006-01-02")}}. - If you want to display abbreviated weekday names such as
Mon), you can useMonFor example:{{stampToDate(item.CreatedTime, "Mon, 2006-01-02")}}. To display "Monday" and so on in Chinese, you need to combine the AnQiCMS multi-language translation function or use it in the template.ifThe statement outputs the Chinese equivalent after conditional judgment of the English week.