Of Security CMSstampToDateLabel: Customize time format flexibly, easily display month abbreviations
As an experienced website operations expert, I know that both the accuracy and beauty of time are equally important in content display.AnQiCMS (AnQiCMS) provides strong support for content operations with its efficient and customizable features.In daily operations, we often need to convert timestamps in databases to user-friendly date formats.AnQi CMS provides a very practical template tag -stampToDate.
Recently, many friends have asked about Anqi CMSstampToDateCan the label be customized to display the English abbreviation of the month, such as "Jan", "Feb", and such?The answer is affirmative, not only can it, but it is also very flexible, thanks to the time formatting mechanism of the underlying Go language in AnQiCMS.
Deep understandingstampToDateLabel and Go language time formatting
In AnQiCMS template design,stampToDateLabels are the core tools for processing timestamp conversion. Their basic usage method is{{stampToDate(时间戳, "格式")}}. The 'timestamp' is typically a 10-digit Unix timestamp, and the 'format' is the key to determining the output date and time style.Understand this "format" parameter, it is the first step to customize the display of month abbreviations.
Go language has a unique and very efficient 'reference time' concept when handling time formatting. This reference time is not a randomly chosen date, but a fixed one:Mon Jan 2 15:04:05 MST 2006. We just need to replace the corresponding part of this reference time with the display format we want, and the Go language will intelligently apply it to the actual timestamp. For example:
2006Represent a four-digit year (e.g., 2023)01Represent a two-digit month (e.g., 01, 02...12)JanRepresent the English abbreviation of the month (such as Jan, Feb…Dec)JanuaryRepresent the full English name of the month02Represent the two-digit date15Represent the hour in 24-hour time format03Represents the hour in 12-hour format04Represents the number of minutes05Represents the number of secondsMonRepresents the English abbreviation for the day of the weekMondayRepresents the full English name for the day of the week
mastered this reference time mapping, we can customize the time display format at will.
easily achieve the display of month English abbreviations
Now let's return to our core question: how to display the English abbreviation of the month? According to the formatting rules of the Go language, we only need to use the format string "JanThis identifier can be used tostampToDateLabels display the month as an English abbreviation.
For example, if we want to display the publication time of an article (usually stored as a timestamp, such asitem.CreatedTimeDisplayed in the format "Jan 02, 2023", the template code can be written like this:
<div>发布日期:{{stampToDate(item.CreatedTime, "Jan 02, 2006")}}</div>
If you want to add the year in front of the date, for example, "2023 Jan 02", you can adjust the format string to:
<div>发布日期:{{stampToDate(item.CreatedTime, "2006 Jan 02")}}</div>
Even more complex combinations, for example, displaying “02-Jan-2023”, can also be easily achieved:
<div>发布日期:{{stampToDate(item.CreatedTime, "02-Jan-2006")}}</div>
In this way, we can see that AnQiCMS'sstampToDateTags provide high flexibility, allowing content operators to freely adjust the display format of date and time according to the style of the website, target audience, or internationalization needs, without the need for complex secondary development.
More practical time formatting techniques
In addition to the English abbreviations for months, mastering the reference time format of the Go language can unlock more practical date and time display methods, greatly enriching our content presentation.
- Full English month name: If you need to display the full month name, such as "January", just use the format string
Januaryand it is done:<div>完整月份:{{stampToDate(item.CreatedTime, "January 02, 2006")}}</div> - Display of the day of the weekTo display the abbreviation of the day of the week, such as 'Mon', use
MonTo display the full name, such as 'Monday', useMonday:<div>发布时间:{{stampToDate(item.CreatedTime, "Mon, Jan 02, 2006")}}</div> <div>发布时间:{{stampToDate(item.CreatedTime, "Monday, January 02, 2006")}}</div> - Time to the second: Combined with the format of hours, minutes, and seconds, it can display more precise time:
Here<div>精确时间:{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}}</div>15Represents 24-hour time format, if you need 12-hour format with AM/PM, you can use03:04:05 PM.
In the AnQiCMS template files, such as the article detail page ({模型table}/detail.html) or the list page ({模型table}/list.html), we can obtain the timestamp through variables such asitem.CreatedTimeorarchive.CreatedTimeand then combine withstampToDateTags and the above formatting techniques, present time information in the most fitting way for content and user habits.This meticulous customization capability is exactly the embodiment of the 'efficient and customizable' value pursued by AnQiCMS as an enterprise-level content management system.It allows the content of the website to appear in a more professional and international manner after publication, enhancing the overall user experience and brand image.
Frequently Asked Questions (FAQ)
1.stampToDateDoes the label support displaying the full month name, such as "January" or "February"?Of course it does. Similar to displaying the month abbreviation, you just need tostampToDateThe format string contains, willJanReplaceJanuaryit is. For example,{{stampToDate(item.CreatedTime, "January 02, 2006")}}It will display as “January 02, 2023”. Go language time formatting is achieved by matching the corresponding parts of the reference time.
2.stampToDateCan the label directly display Chinese months, such as 'January' or 'February'?
stampToDateThe tag directly works through the built-in date formatting rules of the Go language, and its reference time (Mon Jan 2…) uses English.Therefore, it cannot directly output Chinese months like 'January' and 'February' through a format string.If you need to display Chinese months, you may need to combine the AnQiCMS multi-language feature (if a corresponding language package is set), or process it twice in the template through conditional judgment and mapping table, converting English month abbreviations or numeric months manually into Chinese.
3. Why?stampToDateThe format of the label string looks strange, for example, '2006-01-02 15:04:05' instead of 'YYYY-MM-DD HH:mm:ss'?This is indeed a feature of Go language time formatting. Go language does not adopt commonYYYY-MM-DDsuch symbol placeholders, but uses a fixed "reference time"-2006年1月2日(周一)下午3点04分05秒,位于MST时区. Any numeric or alphabetical combination you write in the format string, Go will try to match it to the corresponding part of this reference time to generate the date and time format you want. For example,2006represents the year,01represents the month,02represents the date,15Represents hours in a 24-hour format and so on. Although it may seem a bit unusual at first glance, once you grasp its rhythm, you will find it very flexible and powerful.