In the daily operation of AnQiCMS, the formatting of Unix Timestamp is a common requirement, especially when our website content needs to be targeted at international users, the flexibility of the date display format is particularly important. Today, let's delve into AnQiCMS in detail.stampToDateLabel, see if it can meet our needs to format timestamps as 'MM/DD/YYYY' in this international date format.
AnQiCMSstampToDateLabel: A powerful assistant for time formatting
AnQiCMS as an enterprise-level content management system provides strong flexibility in template design, its tag system is similar to the Django template engine, making the control of front-end content display very intuitive. Among them,stampToDateTags are specifically designed for handling timestamp formatting.
According to the official documentation of AnQiCMS,stampToDatethe usage of the tag is very concise and clear:{{stampToDate(时间戳, "格式")}}. The 'timestamp' here refers to a 10-digit Unix timestamp, for example1609470335. The 'format' part is the key to its function, the document clearly states that it supportsFormat supported by Golang.
What does this 'Format supported by Golang' mean? Compared with many usesY-m-dor%Y-%m-%dThis placeholder system is different, the Golang time formatting mechanism is unique, it uses aReference timeto define the output format. This reference time is2006年1月2日15时04分05秒specificallyMon Jan 2 15:04:05 MST 2006How do you want the time to be displayed, use the corresponding part of this reference time to spell it.
For example:
- If you want to display the year, write
2006. - If you want to display the month, write
01(Representing January). - If you want to display the date, write
02(Representing the second.). - Hours are
15(24-hour format) or03(12-hour clock), minutes are04, seconds are05.
It is this unique "what you see is what you get" formatting method that givesstampToDatetags extremely high flexibility.
Implement the 'MM/DD/YYYY' international date format
Now, let's return to the core issue:stampToDateDoes the tag support formatting timestamps into the international date format "MM/DD/YYYY"?
The answer is affirmative.
Due to the Golang time formatting mechanism allowing us to freely combine the various parts of the reference time, we can completely combine01(months),02(dates) and2006(Year) to construct the format “MM/DD/YYYY”.
Specifically, you just need tostampToDatepass the following format string in the
{# 假设有一个名为 article.CreatedTime 的时间戳变量 #}
<div>发布日期 (MM/DD/YYYY): {{stampToDate(article.CreatedTime, "01/02/2006")}}</div>
By using01(representing month) at the forefront,02representing the day, placed in the middle,2006representing the year, placed at the end, and separated by a slash/We have successfully formatted the timestamp into the international date format of "MM/DD/YYYY".
This flexibility is not limited to "MM/DD/YYYY". If you need "DD-MM-YYYY" (for example, the commonly used format in Europe), you can use"02-01-2006"; If you need other custom formats such as 'YYYY.MM.DD', it can also be easily achieved by simply combining according to the Golang reference time rules.
This is undoubtedly a very practical feature for a website that needs to support multilingual and multi-regional content display.It ensures that website operators can accurately control the display of dates and times according to different user habits, thereby enhancing user experience and reducing potential cultural misunderstandings.
Conclusion
AnQiCMS'stampToDateThe label, based on its flexible time formatting standard of Golang, not only can easily achieve the conversion of international date formats such as 'MM/DD/YYYY', but also grants website operators a high degree of control over the display of date and time.This means that regardless of which corner of the global market your business is facing, AnQiCMS can help you provide a localized, user-friendly date display experience.On the road to website content internationalization, this meticulous functional support is undoubtedly a highlight of AnQiCMS.
Frequently Asked Questions (FAQ)
Question:
stampToDateCan the label support other international date formats besides "MM/DD/YYYY"?Answer: Absolutely. AnQiCMS'stampToDateThe tag is based on the Golang time formatting standard, which is very flexible. You can combine reference time (2006年1月2日15时04分05秒The various parts can be used to create any desired date or time format. For example, to implement the "DD/MM/YYYY" format, you can use"02/01/2006"; To implement a format with the day of the week, such as "Mon, Jan 2, 2006", you can use"Mon, Jan 2, 2006".Question: If I want to display the date and time specifically,
stampToDateCan the tag do that?Of course, it can. The reference time of Golang includes hours, minutes, and seconds (15:04:05),you can combine them with the date part. For example, to display "MM/DD/YYYY HH:MM:SS", you can use the format string"01/02/2006 15:04:05". This allows you to precisely control the display of any combination of dates and times.Question: There is also one in AnQiCMS.
dateFilter, it andstampToDateWhat are the differences between the tags? Which one should I choose?Answer: The AnQiCMS document mentionsdateFor example, the filter (such as infilter-date.mdIn) it is mainly used for formattingGo languagetime.TimeType objectwhilestampToDateThe tag is specifically used for formattingUnix timestamp (usually a 10-digit integer). If you get a Unix timestamp from a database or other API, you should usestampToDateIf your variable is already of Go'stime.Timeobject (this may be encountered in some advanced scenarios or internal data structures), then you can usedateFilter. It is more common to handle Unix timestamps in the vast majority of daily content operation scenarios, thereforestampToDateTags will be your more common choice.