The time of content release is an important component of conveying information to visitors in the process of website operation.A clear and readable date format can not only improve user experience but also make the website look more professional and timely.In AnQiCMS, the content publish time is usually stored in the form of a series of numbers (i.e., a timestamp).How can we convert these numbers into the familiar 'year-month-date' or 'month/day time:minute' formats that we are accustomed to?stampToDate.

Understand the timestamp in AnQiCMS

In the template development of AnQiCMS, we often encounter things likeitem.CreatedTime(Content creation time) orarchive.UpdatedTime(Content update time) such variables. Their values are usually a 10-digit Unix timestamp, for example1609470335.This original timestamp is very convenient for machine processing, but it is not very intuitive for human reading.In order to enhance the reading experience of the website content, we need a method to translate these timestamps into a more easily understandable date and time format.

IntroductionstampToDateTag

At this time,stampToDateThe tag comes into play. Its basic usage is very intuitive, following the common syntax of the AnQiCMS template engine:

{{stampToDate(时间戳, "格式")}}

This tag accepts two main parameters:

  1. Timestamp:A 10-digit Unix timestamp that is usually obtained fromarchive/itemsuch objects.CreatedTimeorUpdatedTimefield.
  2. Format:A string that defines the date and time display format you want.

Master the time formatting rules of Go language

The "format" parameter is to understand and master herestampToDateThe key tag. AnQiCMS template engine uses Go language at the bottom, so the time format definition also follows the unique way of Go language. Unlike the commonYYYY-MM-DDThe format of the equal sign placeholder is different, Go language uses a specific oneReference timeTo define the various parts of the date and time. This reference time is:

January 2, 2006 3:04:05 PM(The time zone is usually MST or UTC-0700)

The principle is: you want which part of the date and time to be displayed in what form, use the corresponding number or character of this reference time to represent it. For example:

  • Year:Use2006Represent a four-digit year.
  • Month:Use01Represent a two-digit month (with leading zeros if necessary), using1Represent a month without leading zeros.
  • Day:Use