As a website operator deeply familiar with AnQi CMS, I know the importance of content timeliness and presentation to user experience.In website content management, the timestamp (Timestamp) is a common method for recording the time of content publication and update, but directly displaying the timestamp is not intuitive for users.Therefore, formatting timestamps into readable date and time formats is a crucial step in enhancing the professionalism and user-friendliness of a website.stampToDateMake this operation easy.
UnderstandingstampToDateLabel and its usage
In the AnQiCMS template system,stampToDateLabels are a powerful auxiliary tool, used specifically 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, without the need for complex programming logic, and can be achieved with simple template syntax.
stampToDateThe syntax and parameter parsing of the label
stampToDateThe calling syntax of the label is very intuitive:{{stampToDate(时间戳, "格式")}}. Let's analyze the two core parts of this syntax in detail.
The first parameter isTimestamp.This usually represents a 10-digit Unix timestamp, which is the number of seconds from the Unix epoch (00:00:00 UTC on January 1, 1970) to a specific time.archiveDetailorarchiveList), and the values of fields such asCreatedTimeorUpdatedTimeThe original value is such a timestamp. You need to take the value of these fields directly asstampToDateas the first parameter.
The second parameter isFormat String.This is the key to determine the specific way in which the output date and time are presented.AnQiCMS's template engine is based on Go language, therefore the time formatting string here follows the unique reference time format of Go language.2006-01-02 15:04:05.999999999 -0700 MSTto serve as the template for all date-time formatting. This means you cannot arbitrarily useYYYY-MM-DDSuch a general placeholder, but instead you need to use the corresponding numbers or text in this reference time to represent the year, month, day, hour, minute, and second you want.
The following are some commonly used format string examples and their corresponding output effects to help you quickly understand and apply:
"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 example:Wed Jun 30 12:30:00 2021
Please pay special attention to the accuracy of the format string, even though it is01and1Such subtle differences will also affect the final display results. For example,01represents the month as a two-digit number (with leading zeros if necessary)1It represents a month with one or two digits (without leading zeros).
stampToDateactual application cases
In AnQiCMS template development,stampToDatetags are usually related to content-related tags (such asarchiveDetailto get the details of a single document,archiveListGet document list) combined to format the publication time or update time of these content items.
Assuming you are building a detailed article page, you need to display the publication date of the article. You can usearchiveDetailtags to retrieve article data, thenCreatedTimepass the field 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 %}
In 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 %}
Through these examples, you can seestampToDateHow to flexibly format timestamps in different scenarios to meet various content display needs.
stampToDateThe importance and **practice**
Properly formatting time information is crucial for enhancing the user experience of a website.A clear date and time can help users quickly understand the publication and update status of the content, enhancing the reliability of the 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 consistencyThroughout the website, try to use the same format for similar time information to avoid confusion for users.
- 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
MM/DD/YYYYor European dateDD/MM/YYYY),AnQiCMS's multilingual support and custom template capabilities can help you achieve this. - Avoid redundant informationIf you only need the date, do not show the time, and vice versa. Clarity is the best.
Summary
stampToDateLabels are a practical and efficient tool in the AnQiCMS template system, making timestamp formatting simple and powerful.By accurately controlling the format string, you can convert the original timestamp data into dates and times that meet various display requirements, thereby optimizing the content presentation of the website and enhancing the user reading experience.Mastering the use of this tag will bring greater flexibility and convenience to the operation of your AnQiCMS website.
Common Questions and Answers (FAQ)
1. WhystampToDateThe format string should be2006-01-02 15:04:05such a combination of numbers, rather thanYYYY-MM-DDinstead of such general placeholders?
This is because the template engine of AnQiCMS is based on Go language. When designing its date and time formatting feature, Go language chose a fixed reference time2006-01-02 15:04:05.999999999 -0700 MSTAs a template.The format string you provided actually tells the Go language how to “parse” or “rewrite” the various parts of this reference time to generate the date and time format you expect in English.2006When, it represents the extracted year; written01When set to auto, it indicates that the month is extracted (with leading zero). This design may seem a bit special at first glance, but once you understand its logic, you can very flexibly customize various time formats.
2. I get the timestamp that is not 10 digits, but 13 digits (millisecond level),stampToDateCan the tag still be used?
stampToDateLabels are used to process 10-digit Unix second-level timestamps.If your timestamp is a 13-digit millisecond timestamp, you need to process it before passing it in, converting it to a second-level timestamp.In some template environments, you may need to divide it by 1000 using mathematical operations.item.MilliCreatedTimeIt is a millisecond timestamp, you may need{{stampToDate(item.MilliCreatedTime / 1000, "2006-01-02")}}. Please confirm the source and precision of your timestamp and make the corresponding adjustments.
3. How tostampToDateWhat day of the week is displayed (for example, "Monday
To display the name of the day of the week, 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 useMon, for example:{{stampToDate(item.CreatedTime, "Mon, 2006-01-02")}}EnglishifThe program makes a conditional judgment on the English name of the week and outputs the corresponding Chinese.