Master Anqi CMSstampToDate:Easily customize the time display format

In the daily work of content operation, the presentation of time information is crucial.Whether it is the publication date of the article, the listing time of the product, or the submission time of the message, a clear and easy-to-read date and time format can greatly enhance the user experience.1609470335Such numbers. Don't worry, AnQiCMS (AnQiCMS) provides us with an elegant and powerful solution: stampToDateLabel.

As an experienced website operations expert, I am well aware of the importance of transforming technical information into practical applications. Today, let's delve deeply intostampToDateTags, making you also become a wizard of time formatting in AnQi CMS, easily customizing various time display formats.

stampToDate: The core tool for timestamp formatting.

In the template system of AnQi CMS,stampToDateis a label used specifically to format a 10-digit Unix timestamp into any custom date-time string.Its design philosophy is simple and efficient, perfectly fitting the performance and ease of use pursued by AnqiCMS based on the Go language.

The usage of this tag is very intuitive:{{stampToDate(时间戳, "格式")}}

You only need to provide a 10-digit timestamp (usually obtained from the content field, such asitem.CreatedTime),and a description of the string format you expect. But the 'format' string here is not the one we are familiar withY-m-d H:i:sThis placeholder follows the unique time formatting rules of the Go language, which is the key we need to deeply understand.

Unveiling the magic of time formatting in the Go language

The Anqi CMS adopts Go language as its underlying development language, therefore, its time formatting mechanism also follows the unique "reference date" method of Go language. Other programming languages use placeholders (such as%YRepresenting the year)to define the different formats, the Go language uses a specific, fixed date asas the reference template for formatting. This "magic" reference date is:

2006-01-02 15:04:05 MST -0700

Understanding this reference date is the core of mastering Go language's time formatting. Each number and English abbreviation represents a specific component of a date and time:

  • 2006:represents the year (four digits)
  • 01:represents the month (two digits, such as01represents January)
  • 02:represents the date (two digits, such as02represents two days)
  • 15:represents hour (24-hour format, such as15represents 3 PM)
  • 04:represents minutes (two digits)
  • 05:represents seconds (two digits)
  • MST:represents time zone name
  • -0700:represents the time zone offset

If you want to include other time information in the output, such as the day of the week, AM/PM, you just need to replace the corresponding English abbreviation or number in the reference date with the style you want to display.

For example:

  • Jan:represents the abbreviation of the month (such asJanrepresents JanuaryJunrepresents June)
  • Mon:represents the abbreviation of the week (such asMonrepresents Monday,Sat表示星期六(English)
  • PM:代表下午(与English)03搭配使用时,表示12小时制的小时和上午/下午标识(English)

When you need to format time, simply represent the target format you want to display using the corresponding number or text combination in this reference date.The system will intelligently convert timestamps to the time structure represented by the reference date and output them in the format string you provide.

实战演练:常见格式化场景(English)

now, let's combine the commonly useditem.CreatedTime(usually the timestamp of creating articles, products, etc.) field, and demonstrate through actual casesstampToDateOf the powerful functions.

Assumeitem.CreatedTimeThe value of1678886400This corresponds to Beijing time `2023 year