In the daily operation of AnQiCMS (AnQiCMS), we often need to handle various time information, whether it is to display the publication date of articles or to dynamically display the current time on the page, an accurate and friendly time display format is crucial for improving user experience and website professionalism. As an enterprise-level content management system based on Go language, AnQiCMS also inherits the unique style of Go language in time processing, especially innowHow to format time labels, their cleverness is worth exploring.

nowLabel: A convenient choice for page real-time time.

In AnQiCMS template design,nowTags are 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 module needs a dynamically updated 'last update time'.nowLabels can easily meet these needs.

UsenowTags are very intuitive, and their basic form is{% now "格式字符串" %}The key here is that 'format string', which is not what we traditionally understand.Y-m-d H:i:sPlaceholder, but follow the unique time formatting rules of the Go language.

Interpret the time formatting of the Go language: the mystery of those 'magic numbers'.

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

This means that when you want tonowWhen formatting time in labels, you need to use this set of "magic numbers" as the template of the expected output format. For example, if you want to display the year, you would use2006If you want to display the month, use01(orJanuary/Jan); The date is02); The hour is15(24-hour format) or03(12-hour format); The minute is04); The second is05. Including the day of the week (MondayorMon), time zone (MSTor-0700) and others are part of this set of reference times.

You can imagine this set of numbers and words as a standard sample, 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 Go language time format applications

After understanding the reference time format of the Go language, we can easily apply various common time formats to the AnQiCMSnowin the 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 format 'Year-Month-Day':{{ now "2006年01月02日" }}This will result in2023年10月26日.

Want to be precise to the minute{{ now "2006-01-02 15:04" }}The output will be2023-10-26 10:30.

If you need to include second-level accuracy{{ now "2006-01-02 15:04:05" }}The result is2023-10-26 10:30:45.

Sometimes, we may just need to display the current year, such as in the copyright information of a website:{{ now "2006" }}It will output it succinctly2023.

For example, some international 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 whatever specific needs your website has, whether it be a concise date or a detailed timestamp.

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

Conclusion

AnQiCMS'nowThe tag and the Go language time formatting mechanism behind it, although it looks unique at first glance, its simplicity and powerful functionality can help website operators and developers easily implement diverse time display needs. Whether it is to enhance the timeliness of content, improve user reading experience, or meet specific SEO standards, mastering its usenowThe label and formatting rules will add a professional touch and operational efficiency to your AnQiCMS website.


Frequently Asked Questions (FAQ)

1. Why does AnQiCMS not use the time formatting thatY-m-dthese 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 method, although different from PHP, Python, and other languages, is very intuitive and less prone to errors once you get used to it, as it avoids ambiguity that may arise from different placeholders (such asmmMay represent months or minutes).Reference time in Go language2006-01-02 15:04:05 -0700 MSTClearly defines each number and word representing the components of time.

2. BesidesnowLabel, how do I format the article's publish time (usually a timestamp)?

For the article publishing time obtained from the database (usually stored in Unix timestamp format), you need to use the AnQiCMS providedstampToDatethe filter. Its usage is similar tonowTags are similar, they also follow the time formatting rules of the Go language. For example, ifitem.CreatedTimeis 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, the reference time in Go language