User-friendly date and time display is crucial for website content.An easily readable and clear time format can enhance the visitor's reading experience and help them quickly understand the publication or update time of the content.In AnQiCMS, it is flexible and efficient to format the article's publish timestamp into a format like "2023-01-01 14:30".

Understanding the application of timestamps in AnQiCMS

In AnQiCMS managed content, whether it is articles, products, or other custom models, their publish 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.CreatedTime(Content creation time) andUpdatedTime(Content update time) such fields, they 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 Tools:stampToDatetags

AnQiCMS provides a namedstampToDateThe template tag, specifically used for converting timestamps to user-friendly date and time formats. The usage of this tag is very simple and clear, as it mainly accepts two parameters:

  1. To format the timestamp:This is usually the value you get from articles, products, or other content dataCreatedTimeorUpdatedTimeof the field. For example, on the article detail page it might bearchive.CreatedTimeIn the article list loop it may beitem.CreatedTime.
  2. Formatted string:This is the core parameter, which determines the final format in which date and time are displayed.AnQiCMS as a system developed based on the Go language, 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 is different from other languages (such as PHP's)Y-m-d H:i:sIt is slightly different. It does not use abstract placeholders, but uses afixed reference date and timeas the 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)
  • 15Represents hours (24-hour format)
  • 04Represents minutes
  • 05Represents seconds
  • PMRepresents AM/PM indicator (if needed for 12-hour format)

When you want to format a timestamp into a specific style, simply construct your format string using the corresponding numbers and text from the above reference time, in the style you wish.

For example, we hope to get a format like “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 format.04Represents the minute part.

Some common formatting string examples and their meanings:

  • "2006-01-02": Displayed as “2023-01-01”
  • "2006年01月02日":显示为 “2023年01月01日”
  • "15:04":显示为 “14:30” (24小时制的时间)
  • "03:04 PM":显示为 “02:30 PM” (12小时制带上下午标识)
  • `“2006-01-0