Anqi CMS'sstampToDate标签:Flexible custom date format, easily display month abbreviations

As an experienced website operations expert, I know that both the accuracy and beauty of time display are equally important.AnQiCMS (AnQiCMS) is known for its high efficiency and customizable features, providing strong support for content operations.In daily operations, we often need to convert timestamps in the database to user-friendly date formats.stampToDate.

Recently, many friends have asked about, Anqi CMS'sstampToDate

Deep understandingstampToDateTags and time formatting in Go language

In the template design of AnQiCMS,stampToDateLabels are the core tools for handling timestamp conversion. Their basic usage method is{{stampToDate(时间戳, "格式")}}.Here, the "timestampUnderstand this "format" parameter, it is the first step to customize the display of month abbreviations.

Go language has a unique and highly efficient 'reference time' concept when handling time formatting. This reference time is not an arbitrary date but a fixed one:Mon Jan 2 15:04:05 MST 2006We only need to replace the corresponding part in the reference time with the desired display format, and the Go language will intelligently apply it to the actual timestamp. For example:

  • 2006Represents a four-digit year (such as 2023)
  • 01Represents a two-digit month (such as 01, 02…12)
  • JanRepresent the English abbreviation of the month (such as Jan, Feb…Dec)
  • JanuaryRepresent the full English name of the month
  • 02Represent the two-digit date
  • 15Represent the hour in 24-hour clock format
  • 03Represents the hour number in 12-hour format
  • 04Represents the number of minutes
  • 05Represents the number of seconds
  • MonRepresents the English abbreviation of the day of the week
  • MondayRepresents the full English name of the day of the week

We can freely customize the time display format by mastering this reference time mapping relationship.

Easily implement the display of month English abbreviations

now we come back to our core problem: 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 stringJanThis identifier can be used to。“stampToDateThe label displays the month in English abbreviation form。“

For example, if we want to display the publication time of the article (usually stored as a timestamp, like “item.CreatedTime)显示为“Jan 02, 2023”这样的格式,模板代码可以这样编写:

<div>发布日期:{{stampToDate(item.CreatedTime, "Jan 02, 2006")}}</div>

If you want to add the year at the beginning 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, such as 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'sstampToDateThe label provides 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 requirements, 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 ways to display date and time, greatly enriching our content presentation.

  • Complete English Month NamesIf you need to display the complete month name, such as "January", just use the format stringJanuaryas follows:
    
    <div>完整月份:{{stampToDate(item.CreatedTime, "January 02, 2006")}}</div>
    
  • Display of days of the week:To display the abbreviation of the day of the week, such as 'Mon', useMon;To 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:Combine the hour, minute, and second format to display more precise time:
    
    <div>精确时间:{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}}</div>
    
    Here are the15Represents 24-hour time format, if you need 12-hour time 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 by using variables such asitem.CreatedTimeorarchive.CreatedTime,and 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 value pursuit of "efficient and customizable" as an enterprise-level content management system, which is AnQiCMS.It allows the website content to be presented to readers in a more professional and internationalized manner after publication, enhancing the overall user experience and brand image.


Common Questions (FAQ)

1.stampToDateDoes the label support displaying the full month name, such as “January” or “February”?Of course, it supports. Similar to displaying the month abbreviation, you just need to instampToDateThe format string of the label, willJanwithJanuaryand it is. For example,{{stampToDate(item.CreatedTime, "January 02, 2006")}}The content will be displayed as “January 02, 2023”. The time formatting in Go language is implemented by matching the corresponding parts of the reference time.

2.stampToDateLabel can directly display Chinese months, such as 'January' or 'February'? stampToDate标签直接通过Go语言内置的日期格式化规则工作,其参考时间(Mon Jan 2…)使用的是English。Therefore, it cannot directly output "JanuaryIf you need to display Chinese months, you may need to combine the multi-language function of AnQiCMS (if there is a corresponding language package set) or manually convert English month abbreviations or numeric months to Chinese through conditional judgment and mapping table in the template.

3. WhystampToDateThe label format string looks strange, such as "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 use commonYYYY-MM-DDsuch placeholder symbols, but uses a fixed "reference time" -2006年1月2日(周一)下午3点04分05秒,位于MST时区.You write any number or letter combination 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.2006Represents the year,01Represents the month,02represents the date,15Represent the hours in a 24-hour format and so on. Although it may seem a bit unusual at first glance, once you master its regularity, you will find it very flexible and powerful.