How to display the current year or a custom formatted current date and time in the template?

Calendar 👁️ 76

Flexibly display the current date and custom time format in the Anqi CMS template

In website operation, we often need to dynamically display date and time information on the page, whether it is the current year in the copyright statement, or the publication time of articles, event countdowns, and so on.AnQi CMS provides a very flexible and easy-to-use method to display the current year or a custom date and time format in templates, keeping your website content up-to-date and enhancing user experience.

The Anqi CMS template system adopts syntax similar to the Django template engine, making the display of dynamic content intuitive. The core of handling date and time is two powerful tags:stampToDateandnow.

Get the current year and time dynamically:nowThe clever use of tags

When you need to display the current year directly in the template, for example in the copyright statement of a website footer,nowThe label is your ideal choice. It can directly obtain the current date and time of the system and output it in the specified format.

For example, to display the current year in the website footer, we can use it like this:{% now "2006" %}

Here2006It is not an ordinary number, but a convention for date formatting in the Go language.In Go language, date formatting is based on a specific reference point: "2006-01-02 15:04:05 -0700 MST".You just need to remember the numbers corresponding to each part of this reference time point, and you can flexibly combine it into any format you want. For example:

  • 2006Represents the year
  • 01Represents the month
  • 02Represents the date
  • 15representing hours (24-hour system)
  • 04representing minutes
  • 05representing seconds

So, if you want to display the current date in full format, such as '2023-01-02', you can write it as: {% now "2006-01-02" %}

If you need to be accurate to hours, minutes, and seconds, for example, “2023-01-02 15:04:05”, it can be done like this:{% now "2006-01-02 15:04:05" %}

nowThe power of tags lies in their ability to keep your website always displaying the latest year or time, without the need to manually update template files, greatly reducing the maintenance burden.

Format the existing timestamp:stampToDateThe application of tags

In addition to getting the current time, websites often need to display the time stored in the database, such as the publication time of articles (CreatedTime) or update time(UpdatedTimeThese times are usually stored in the form of Unix timestamps. AnQi CMS providesstampToDatetags to convert these timestamps into readable date and time formats.

stampToDateThe use of tags is:{{ stampToDate(时间戳, "格式") }}.

For example, on the article detail page, we often need to display the publication date of the article. Assuming the publication time field of the article object isCreatedTimeYou can format it like this:{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}

Or, if you want to display a simpler 'Year-Month-Day Hour:Minute' format:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04") }}

This is the same as using the date formatting rules of the Go language. By adjusting the format string, you can easily achieve various date display requirements, such as:

  • Only show the month and date:{{ stampToDate(archive.CreatedTime, "01-02") }}
  • Show Abbreviated English Month Names:{{ stampToDate(archive.CreatedTime, "Jan 02, 2006") }}
  • Show the day of the week:{{ stampToDate(archive.CreatedTime, "Mon, 02 Jan 2006") }}

tostampToDatewith the tag andarchiveDetailorarchiveListTag combination can be easily used to display the creation or update time of each article in a unified and beautiful format on the article list or detail page, greatly enhancing the readability of the content and the professionalism of the website.

Example of practical application scenarios

  • Website footer copyright statement: © {% now "2006" %} {{ system.SiteName }} 版权所有。

  • Display publication time on article detail page: <p>发布于:<span>{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}</span></p>

  • Display brief date on article list: <small>更新日期:{{ stampToDate(item.UpdatedTime, "2006/01/02") }}</small>

MasternowandstampToDateThese tags, along with the Go language date formatting rules, allow you to freely control the display of date and time on the website in Anqi CMS.This can make your website content more dynamic and attractive, and also brings great convenience to the maintenance and update of the website.


Frequently Asked Questions (FAQ)

Q1: Why is the date format I set showing up as "2006-01-02 15:04:05" or another incorrect date?

A1: This is usually because you did not set the string formatting completely according to the Go language date formatting reference point. The date formatting in Go is quite special, it does not useY-m-dThis placeholder, instead, uses a fixed reference time point “2006-01-02 15:04:05” to represent the year, month, day, hour, minute, and second.Please carefully check that each number or letter in your format string matches the corresponding part at the reference time point.For example, the year must be2006instead ofYYYY, the month is01instead ofMM.

Q2:stampToDateandnowWhat is the difference between these tags, when should I use them?

A2:nowTags are used to get and displayCurrent system time. For example, the copyright year at the footer of a website ({% now "2006" %}It will dynamically display the current year of your website visit.stampToDateTags are used for formatting.Timestamps that already exist.These timestamps usually come from the content fields in databases, such as the publication time of articles (archive.CreatedTime). In simple terms, if you need to display the 'current' time, usenowUse it to display the time of a past point in timestampToDate.

Q3: Can I display a customized Chinese date format, such as “February 2, 2023”, and make it work?

A3: The date formatting function of Anqi CMS is mainly based on the reference time format of the Go language, which does not directly support converting numbers to Chinese numerals in uppercase. However, you can implement a Chinese date format like "2023 year 01 month 02 day", by directly including Chinese characters in the format string, for example{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}. For more complex Chinese uppercase number requirements, it may be necessary to use JavaScript for secondary processing on the front-end, or to develop a custom Go language backend function to meet the needs.

Related articles

How to use `{filename}` or `{catname}` in pseudo-static rules to generate SEO-friendly custom URLs for articles, categories, and single pages?

In website operation, generating SEO-friendly URL addresses for content is a key link to improving website SEO performance.A clear, keyword-rich URL not only makes the page content easy for users to understand, but also helps search engines better understand and crawl web pages.AnQiCMS provides powerful custom static rule functionality, allowing us to flexibly use variables such as `{filename}` and `{catname}` to generate highly customized URLs for articles, categories, and even single pages.### Optimize URL

2025-11-08

How to set up image resource management in AnQi CMS and support batch regeneration of thumbnails in different sizes?

In website operation, images are not only an important part of the content, but also a key factor affecting page loading speed and user experience.Efficient image management can greatly enhance the performance and maintainability of a website.AnQiCMS (AnQiCMS) is well-versed in this field, providing users with comprehensive image resource management functions, especially excelling in thumbnail settings and batch processing, making image operations more convenient and flexible.### Core Feature Overview: AnqiCMS Image Management System AnqiCMS Image Resource Center is a collection of upload, classification, editing

2025-11-08

How to reuse common HTML fragments in a template by using the `include` tag or define reusable code blocks by `macro`?

In the development and maintenance of website templates, we often encounter the need to reuse HTML code snippets or logical structures.If you always copy and paste, it is not only inefficient, but also once you need to modify it, you have to repeat the operation in multiple places, which is very prone to errors.AnQi CMS knows this point, it provides a template engine that adopts Django's syntax, through `include` tags and `macro` macro functions, allowing us to easily achieve code reuse, greatly enhancing the maintainability and development efficiency of templates.### Reuse the public HTML fragment: `include`

2025-11-08

How to retrieve and display user group details, such as group name, level, and purchase price, for a membership system?

Today in the operation of the website, building an efficient member system is crucial for content monetization, user stickiness cultivation, and community construction.AnQiCMS (AnQiCMS) provides a solid foundation for building such a system with its flexible design and rich features.Among them, the fine management of user groups is the core of the member system's operation, which allows us to provide differentiated service experiences based on the different identities and rights of users.### User Group Management in AnQi CMS: Basics and Value AnQi CMS built-in user group management function

2025-11-08

How to split a string into an array or concatenate array elements into a single string in a template?

During the development of Anqi CMS templates, we often encounter situations where we need to process strings, such as converting a text segment separated by a specific symbol into a list, or concatenating multiple items in a list into a continuous text.The Anqi CMS template engine provides powerful filters (Filters) to help us easily implement these operations, greatly enhancing the flexibility of the template. ### AnQi CMS Template Engine Basics The AnQi CMS template engine syntax is designed to be very user-friendly, similar to the Django template engine.It is mainly through double curly brackets

2025-11-08

How to find the number of occurrences or the first occurrence position of a keyword in a string on a line in a template?

In AnQi CMS template design, sometimes we may need to perform more detailed analysis and display of content, such as finding the position of the first occurrence of a specific keyword in a text, or counting how many times it appears.These requirements are very practical in aspects such as dynamic content display, information extraction, or辅助SEO.Benefiting from the template engine syntax similar to Django adopted by Anqi CMS, we can utilize its powerful filter functions to achieve these goals.Next, we will discuss how to use the built-in `index` and `count` in Anqi CMS template

2025-11-08

How to safely escape HTML code in a template to prevent XSS attacks, or force non-escaping of HTML content?

When building a website, ensuring the security of the content, especially the prevention of cross-site scripting (XSS) attacks, is a crucial aspect.AnQiCMS (AnQiCMS) provides powerful tools at the template level to manage the escaping of HTML content, thereby effectively protecting the website and its users.Understanding how to safely handle HTML code in templates is essential knowledge for every AnQi CMS user when performing content operations and template development.### Default security mechanism of AnQi CMS template The template engine of AnQi CMS adopts a design philosophy similar to Django

2025-11-08

How to determine if a string or array contains a specific keyword in a template?

In the template development of Anqi CMS, we often need to dynamically adjust the page display based on specific attributes or text fragments of the content.Determine whether a string or array contains a specific keyword is a key step in implementing this dynamic logic.AnQiCMS is a powerful Django style template engine with built-in filters, making this operation very intuitive and efficient.## Core Tool: `contain` Filter The most direct way in AnQiCMS template system is

2025-11-08