In the daily operation of AnQiCMS, we often need to handle various time information, whether it is displaying the publication date of articles or dynamically showing the current time on the page. An accurate and friendly time presentation format is crucial for improving user experience and website professionalism. As an enterprise-level content management system built on the Go language, AnQiCMS inherits the unique style of the Go language in time handling, especially innowHow to format time in tags, its cleverness is worth in-depth exploration.

nowTag: A convenient choice for real-time page time

In the template design of AnQiCMS,nowTags is a very practical built-in feature that allows us to directly output the real-time time of the current server at any position on the page. Imagine that your website's footer needs to display the copyright year, or a certain module needs a dynamically updated "last update time".nowTags can easily meet these requirements.

UsenowTags are very intuitive, their basic form is{% now "格式字符串" %}。Here lies the key to that "format string", which is not what we traditionally understand asY-m-d H:i:ssuch placeholders, but follows the unique time formatting rules of the Go language.

Interpreting the time format of Go language: the mystery of that group of 'magic numbers'

Different from what is common in other programming languages or template enginesYYYY-MM-DDSuch symbols are placeholders, Go language uses a set of specific 'reference times' as templates when formatting time. This set of reference times is fixed, specifically:2006年1月2日 15点04分05秒 -0700时区. It might seem confusing at first, but once you understand its logic, you will find it both powerful and flexible.

This means, when you want to innowWhen formatting time in labels, you need to use this set of 'magic numbers' as the template for the expected output format. For example, if you want to display the year, use2006If you want to display the month, use01(or}January/Jan);The date is02;The hour is15(24小时制)或03(12-hour clock);The minutes are04;The seconds are05。Together with the day of the week (MondayorMon)、the time zone (MSTor-0700)are all parts of this reference time.

You can imagine this set of numbers and words as a standard sample, the Go language will output the corresponding part of the current time based on how you arrange these samples in the 'format string'.

Practice makes perfect: Common applications of Go language time formats

Understood the reference time format of Go language, we can then easily apply various common time formats to AnQiCMSnowthe tag.

For example, the date format we use most commonly in our daily lives, such as 'Year-Month-Day':{{ now "2006-01-02" }}It will output something similar2023-10-26such a result.

If you need to display the Chinese formatted 'Year-Month-Day':{{ now "2006年01月02日" }}This will result in2023年10月26日.

想要精确到分钟,显示“year-month-day hour:minute”:{{ now "2006-01-02 15:04" }}The output will be2023-10-26 10:30.

如果需要包含秒级精度,显示“year-month-day hour:minute:second”:{{ now "2006-01-02 15:04:05" }}结果便是2023-10-26 10:30:45.

Sometimes, we may just need to display the current year, for example, in the copyright information of a website:{{ now "2006" }}It will output succinctly.2023.

For example, some internationalized date formats, or displays with the day of the week:

  • “Month/Day/Year”:{{ now "01/02/2006" }}
  • “Day, Month Year”:{{ now "Mon, 02 Jan 2006" }}
  • “Hour:Minute AM/PM”:{{ now "3:04 PM" }}

This formatting flexibility allows you to elegantly present, according to the specific needs of the website, whether it is a simple date or a detailed timestamp.

It is worth mentioning that AnQiCMS also providesstampToDateThe filter is used to format timestamps, it also follows the formatting rules of the Go language. This means that once you master the reference time format in Go language, whethernowLabel gets real-time time, orstampToDateHandle timestamps stored in the database with ease.

Concluding remarks

AnQiCMSnowTags and the underlying Go language time formatting mechanism, although it may look unique at first glance, its simplicity and powerful functionality can help website operators and developers easily meet various time display needs. Whether it is to enhance the timeliness of content, improve user reading experience, or meet specific SEO standards, proficient use ofnowTags and formatting rules will add a professional color and operational efficiency to your AnQiCMS website.


Common Questions (FAQ)

1. Why does AnQiCMS not use the time formattingY-m-dfor these more common placeholders?

This is because AnQiCMS is developed based on Go language, and Go language chose to use "reference time" as the standard for time formatting from the beginning. This approach, although different from languages like PHP, Python, etc., is very intuitive and less prone to errors once you get used to it, as it avoids ambiguity that might come from different placeholders (for example,mmMay represent a month or minute).The reference time in Go language clearly defines the time components represented by each number and word.2006-01-02 15:04:05 -0700 MSTIt clearly defines the time components represented by each number and word.

2. BesidesnowLabel, how do I format the publication time of an article (usually a timestamp)?

For the article publish time retrieved from the database (usually stored in Unix timestamp format), you need to use the AnQiCMS providedstampToDatethe filter. Its usage is similar tonowThe label is similar, and it also follows the time formatting rules of the Go language. For example, ifitem.CreatedTimeIt is a timestamp, you can format it like this:{{stampToDate(item.CreatedTime, "2006年01月02日 15:04")}}.

**3. If I need to display a very special time format, Go language's reference time