How to format time with the `now` tag, and which common Go language time formats are supported (such as year month day hour minute second)?

Calendar 👁️ 65

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

Related articles

How to display the current date and time in the Anqi CMS template and output them in a custom format?

As an expert with many years of experience in website operations, I am well aware of the importance of content timeliness and display professionalism to the value of the website.In an AnQiCMS content management system that is flexible and efficient, accurately controlling the display of dates and times on the page and formatting the output according to actual needs is an indispensable part of improving user experience and optimizing information delivery.Today, let's delve into how to display and format dates and times effortlessly in the AnQiCMS template.

2025-11-07

What do `{{item.Title}}` and `{{archive.Title}}` represent in different template contexts?

As an experienced website operations expert, I know that understanding the meaning and usage scenarios of template variables is crucial for efficient website operation in a content management system.AnQiCMS (AnQiCMS) provides great convenience for content display with its flexible and powerful template engine.However, beginners or users not familiar with its template mechanism often become confused about the two seemingly similar variables `{{item.Title}}` and `{{archive.Title}}`.}]

2025-11-07

If a variable's value is an array or slice, how can you use a `for` loop in a template to iterate over and output its contents?

As an experienced website operations expert, I know that efficiently displaying dynamic data is very important in daily content management.AnQiCMS (AnQiCMS) leverages the powerful performance and flexible template system of the Go language to provide us with an excellent solution.Today, let's delve deeply into a practical and common scenario in AnQi CMS template development: how to use `for` loops to traverse arrays or slices and elegantly present their content on web pages.

2025-11-07

How to directly call built-in methods (such as getting thumbnails) of a Go language struct object in a template?

As an experienced website operations expert, I am well aware of the importance of an efficient and flexible CMS system for enterprise content management.AnQiCMS (AnQiCMS) leverages its powerful performance based on the Go language and the ease of use of the Django template engine, bringing us an unprecedented degree of freedom.In daily content operations, we not only hope that the data can be displayed accurately, but also hope that these data can come alive and be intelligently processed according to different needs.Today, let's delve into a seemingly advanced but actually very practical skill

2025-11-07

How to automatically display the current year in the footer of a website (for example “© 2023”)?

As an experienced website operations expert, I am well aware of the importance of the details of website content, especially the subtle aspects such as the copyright year at the bottom of the website page, which not only concerns the professional image of the website but also indirectly affects the judgment of search engines on the freshness of the website content.In such an efficient and flexible content management system as AnQiCMS, achieving this is naturally easy.Today, let's talk about how to automatically display the current year in the footer of your website, keeping your website forever young!

2025-11-07

What is the special role of the `fake` parameter in the `now` tag, and in what scenarios would it be used?

As an experienced website operations expert, I have a deep understanding of AnQiCMS, an enterprise-level content management system developed based on the Go language.It is undoubtedly the powerful support for content operation with its high efficiency, flexibility, and is a capable assistant for small and medium-sized enterprises and self-media teams.In the process of dealing with templates every day, we always encounter various tags and parameters, which may seem trivial but can greatly affect development efficiency and content display effect.Today, let's delve into a seemingly mysterious yet extremely practical parameter - the `fake` parameter of the `now` tag.

2025-11-07

If you need to convert timestamp data (such as `CreatedTime`) to a readable date format, which auxiliary tag or filter should you use?

In AnQiCMS (AnQiCMS) daily operations and template design, we often encounter situations where we need to display time data, such as the article's published time `CreatedTime` or the update time `UpdatedTime`. These time data are usually stored in the database in the form of Unix timestamps, and a string of numbers as a timestamp is obviously not as intuitive and easy to read as

2025-11-07

How to quickly generate random Latin text content for placeholder testing during the early development of AnQi CMS templates?

## Enterprise CMS Template Development: Use `lorem` tag to quickly fill placeholder content and test layout effects At the beginning of website template development, every section of the design draft needs to be filled with content to visually display the layout effect.Imagine if every time you adjust the layout, you had to manually input a lot of text to verify the effect, that would be such a time-consuming and tedious task.As an experienced website operations expert, I know that efficiency is the key to success.Fortunately, AnQiCMS (AnQiCMS) relies on its powerful functionality based on a similar Django template engine

2025-11-07