User-friendly date and time display is crucial for website content.A clear and readable date format not only enhances the visitor's reading experience but also helps them quickly understand the timing of the content's publication or update.In AnQiCMS, formatting the publication timestamp of articles into a format such as '2023-01-01 14:30' is both flexible and efficient.

Understanding the use of timestamps in AnQiCMS

In AnQiCMS managed content, whether it is articles, products, or other custom models, the publication or update time is usually stored in the form of a timestamp (Unix Timestamp).These timestamps are essentially numbers representing the number of seconds from 00:00:00 UTC (Coordinated Universal Time) on January 1, 1970, to a specific moment.In template files, we usually come acrossCreatedTimeAnd (content creation time) as well asUpdatedTime(content update time) such fields are variables that store these original timestamps.

Directly displaying the original timestamp number is not intuitive for users, therefore we need a tool to convert it into a date and time format that people are accustomed to reading.

Core Tool:stampToDateTag

AnQiCMS provides a namedstampToDateThe template tag, specifically used for converting timestamps to user-friendly date and time formats. This tag's usage is very concise and clear, it mainly accepts two parameters:

  1. The timestamp to be formatted:This is usually the value you get from articles, products, or other content data.CreatedTimeorUpdatedTimeof the field. For example, on the article detail page it might bearchive.CreatedTimeIn the article list loop it may beitem.CreatedTime.
  2. Formatting string:This is the core parameter that determines the final format in which the date and time are displayed.AnQiCMS is a system developed based on the Go language, and its date and time formatting follows the specific rules of the Go language.

Deep understanding of formatted strings (Go language date and time reference)

The date and time formatting mechanism of Go language and other languages (such as PHP'sY-m-d H:i:sIt is slightly different. It does not use abstract placeholders, but uses onea fixed reference date and timeas a format template. This reference time is:

2006年01月02日 15时04分05秒 -0700 MST

You can simply remember each number in this reference time representing the date and time component:

  • 2006Represents the year
  • 01Represents the month (two digits)
  • 02Represents the date (two digits)
  • 15representing hours (24-hour system)
  • 04representing minutes
  • 05representing seconds
  • PMRepresenting the AM/PM indicator (if 12-hour format is needed)

When you want to format a timestamp into a specific style, just build your format string according to the style you want, using the corresponding numbers and words from the above reference time.

For example, if we want to get the format '2023-01-01 14:30', then the format string should be written as:"2006年01月02日 15:04".Please note, here,15Represents the hour part in 24-hour time format,04Represents the minutes part.

Some common format string examples and their meanings:

  • "2006-01-02": Displays as '2023-01-01'
  • "2006年01月02日": Displays as '2023 January 01'
  • "15:04": Displayed as '14:30' (24-hour time format)
  • "03:04 PM": Displayed as '02:30 PM' (12-hour time format with AM/PM indicator)
  • `“2006-01-0