How to dynamically obtain and display the current year in the template?

Calendar 👁️ 87

In website operations, it is crucial to maintain the timeliness and accuracy of content, especially information like copyright years, which, if updated manually, is not only cumbersome but also prone to omissions.The Anqi CMS provides a very convenient way for us to dynamically retrieve and display the current year in templates, ensuring that the website information is up-to-date.

The core of dynamically obtaining the current year:nowTag

The Anqi CMS template system adopts a syntax similar to the Django template engine, which has a very practicalnowThe tag is used specifically to retrieve and display the current date and time in the template.The strength of this tag lies in the flexibility to specify the output time format according to your needs, and getting the current year is one of its most common applications.

To display the current year only, we just need tonowSpecify a special formatting string after the label. This string is not a common oneYYYYoryearbut a time formatting reference specific to the Go language.2006So, when you need to display the current year in a template, just write:

{% now "2006" %}

This way, regardless of when your website is accessed, the displayed year will always be the current year of the server, without the need for manual modification, greatly enhancing the efficiency of content maintenance.

Apply the current year to the actual scene

UnderstoodnowAfter the basic usage of the tag, we can then apply it to various places on the website where dynamic year display is needed

1. The copyright information at the bottom of the website

The copyright statement at the footer of the website usually includes the current year, this isnowThe most typical application scenario of tags. We can combine the provided by Anqi CMS.systemLabel to retrieve the website name, forming a complete dynamic copyright statement:

<p>&copy; {% now "2006" %} {% system with name="SiteName" %} All Rights Reserved.</p>

This code will automatically display the current year, followed by the website name configured in the background \

2. Date提示 in the content or on the page

In addition to copyright information, you may also want to display the current date, even more detailed time, in certain specific pages or content areas.nowTags can also handle it easily. For example, if you want to prompt "Today is: [current date]" on a page:

<p>今天是:{% now "2006年01月02日" %}</p>
<p>当前时间是:{% now "15:04:05" %}</p>

Please note,"2006年01月02日"and"15:04:05"The same is the time formatting reference value in Go language, which represents year, month, day, hour, minute, and second.By adjusting this formatted string, you can obtain any date or time display effect you want.

Some details need to be noticed.

While usingnowWhen dynamically retrieving and displaying the current year, there are several details that are worth paying attention to:

  • Go language time formattingAs mentioned before, the time formatting string in Anqi CMS template follows the Go language specification, which means2006represents the year,01represents the month,02represents the date,15represents the hour (24-hour format),04represents the minute,05Represents seconds. At first glance, it may seem a bit special, but once you remember this set of numbers (2006-01-02 15:04:05), you can easily master various time formats.
  • Server time:{% now %}The tag retrieves the current time of the server on which the website is deployed. Therefore, make sure your server time is set correctly to avoid displaying inaccurate dates and times.

BynowThe flexible use of tags allows us to keep the date information on the Anqi CMS website dynamic, reducing the cost of manual maintenance, and also providing visitors with more timely and accurate content experience.


Frequently Asked Questions (FAQ)

Q1: Why{% now %}The formatted string of the tag should be used2006Instead of likeYYYYSuch a general representation?

This is because AnQi CMS is developed based on Go language, its template rendering engine follows the standard of Go language in time formatting. In Go language, time formatting is not done through predefined letters such asY/M/D),instead through a reference date(2006年01月02日15时04分05秒)to define. The date-time string you write in your template is actually parsed by Go language, representing the part you need to output with the numbers at the corresponding positions in the reference date. For example,“}]2006Represents the year,01Represents the month,02Understanding this will allow you to easily master the Go language's time formatting rules.

Q2: I can use{% now %}Do you want to display the publication year of the article?

Cannot be used directly{% now %}Do you want to display the publication year of a specific article?{% now %}Tags are used to retrieve and displaycurrentThe server time. If you need to display the publication year of an article or content, you should use the publication time field contained in the article data itself (for exampleitem.CreatedTime), and combine it with the information provided by AnqicmsstampToDateLabel formatting. For example:{{ stampToDate(item.CreatedTime, "2006") }}This can accurately display the publication year of each article.

Q3:{% now %}What time zone is the time label obtained in?

{% now %}The label gets the current time of the time zone where the website server is located.If you need to display the time in a specific time zone, you may need to adjust the server's system time settings, or look for a related time zone configuration option in the Anqi CMS backend global settings (if the system provides one).In most cases, websites use the local time of the server as the standard.

Related articles

How to display a custom shutdown notice to visitors when the website is under maintenance?

As website operators, we always encounter situations where we need to maintain and upgrade the website, whether it is database optimization, system updates, or content adjustments. These operations sometimes require temporarily shutting down the website to avoid unexpected errors or incomplete content display during maintenance.However, simply displaying a "cannot open" error page on a website undoubtedly gives visitors a bad experience, and may even make search engines mistakenly believe that the website is permanently offline.AnQiCMS (AnQiCMS) fully understands this pain point and provides a graceful and flexible solution that allows you to maintain your website while it is closed for maintenance

2025-11-09

How to ensure that the template file uses UTF-8 encoding to avoid garbled Chinese content?

In website operation, the clear and accurate presentation of content is crucial.Especially when your website contains Chinese content, if you encounter a "garbled code" problem, it not only seriously affects the user experience, but may also damage the professional image of your website.For users of Anqi CMS, ensuring that all template files use the correct encoding, especially UTF-8, is the first step to avoid such problems and is also the most fundamental and critical guarantee.### Why is UTF-8 so important? UTF-8 is the full name of Unicode Transformation

2025-11-09

How to truncate long text content in a template and add an ellipsis for display?

In website content operation, we often encounter scenarios where it is necessary to display long text, such as article abstracts, product descriptions, or user comments.If the complete content is displayed directly, it may cause the page to be long and disorganized, affecting the reading experience of the user.At this time, the long text is intelligently truncated and an ellipsis (...) is added at the end, making it an elegant and practical solution.AnQi CMS as a powerful content management system, its flexible template engine provides a variety of convenient ways to meet this need.### Efficient Core Tool for Extracting Long Text Content

2025-11-09

How to define a temporary variable in a template and use it to display content?

In Anqi CMS, when managing website content, we often find that using temporary variables flexibly in templates can make the code more concise, efficient, easy to understand, and maintainable.The Anqi CMS template system has borrowed the syntax of the Django template engine, which provides a straightforward way to define and use these temporary variables. ### Temporary variables make the template more concise and efficient Why do we need to define temporary variables in the template?

2025-11-09

How to convert multi-line text content into HTML format using a newline character?

In content management, we often encounter situations where we need to enter multiline text, such as product details, service introductions, company profiles, or contact information.This content is typically segmented by pressing the Enter key during backend editing.However, when this content is directly presented on the website front end, you may find that it is 'squeezed' together, with all line breaks disappearing, which not only affects the reading experience but also makes the page look disorganized.AnQi CMS as an efficient enterprise-level content management system, of course, has considered this point and provided a variety of elegant solutions

2025-11-09

How to embed and display a留言验证码in a front-end form?

In website operation, the message board and comment area are often important channels for interaction with users.However, the garbage information and malicious submissions that follow are also a headache.An effective method of integrating captcha functionality to filter out this noise.Today, let's talk about how to easily embed and display a message captcha in the frontend form of AnQiCMS (AnQiCMS), building a defense line for your website.

2025-11-09

How to dynamically filter and display a list of articles through URL parameters or search keywords?

Today, with the increasing refinement of content management, how to make your website content smarter and more considerate in serving visitors is the key to improving user experience and website value.AnQiCMS (AnQiCMS) is an efficient and flexible content management system that provides powerful functions to help us achieve this, especially in terms of dynamic filtering and displaying article lists. The combination of template tags with URL parameters allows for easy construction of highly customizable content presentation methods.This article will give a detailed introduction on how to use URL parameters and search keywords

2025-11-09

How to manage and display category Banner images and thumbnails?

In website operation, the category page is an important link in guiding users to browse and deepen the brand impression.To configure a unique banner image and thumbnail for the category page, it can not only enhance the visual appeal of the website, but also allow users to intuitively understand the category content, effectively optimizing the user experience.AnQi CMS provides flexible and easy-to-operate functions in this regard, helping us manage and display these visual elements efficiently.

2025-11-09