How to get and display the current year in a template for dynamic content such as copyright information?

Calendar 👁️ 79

When operating a website, we often need to display the current year in the footer copyright statement, for example, “© 2023 All Rights Reserved.”. As time goes by, this year needs to be updated every year. If it is manually modified, it is undoubtedly a repetitive and easily overlooked task.AnQi CMS fully considers this common requirement and provides a very intuitive and powerful template tag, allowing you to easily implement dynamic year display, so that your website's copyright information is always up to date.

The core method of dynamically obtaining the current year

The Anqi CMS template system is based on the powerful capabilities of Go language, providing a rich set of tags and filters. To get and display the current year, you just need to add a specific tag to the template, which is{% now %}.

This tag can directly obtain the current date and time of the server in the template. What we are concerned about is just the year. Therefore, we can use{% now %}Specify a formatting string to precisely control its output.

To get and display the current year, you just need to add it in the template:

{% now "2006" %}

You might be curious about this."2006"What does it mean? It does not refer to a specific year, but rather a special reference time used to define time formats in the Go language.2006This numeric string represents the four-digit format of the year. When you use it in{% now %}tags, the system will automatically extract and display the four-digit year of the current date on the server.

For example, if the current year is 2023, then write in the template{% now "2006" %}and it will be displayed on the final page2023. When it comes to 2024, it will automatically change to2024Do not make any manual modifications.

More time formatting options

Except for the year,{% now %}Labels can display full date and time in multiple formats.Once you master the Go language time formatting string rules, you can flexibly combine the required date and time format.Some common formatting strings and their effects include:

  • Display the full date and month and day: {% now "2006-01-02" %}(For example: 2023-10-27)
  • Display the date in Chinese format: {% now "2006年01月02日" %}(For example: October 27, 2023)
  • Show hours, minutes, and seconds: {% now "15:04:05" %}(For example: 10:30:00)
  • Show the full date and time: {% now "2006-01-02 15:04:05" %}(For example: 2023-10-27 10:30:00)

By these combinations, you can dynamically display current date and time information in any format according to the specific requirements of the website design.

It is worth mentioning that if your requirement is to display the publication year of the article instead of the current year, AnQi CMS also provides{{stampToDate(时间戳, "格式")}}Such a filter. It can format the timestamp field stored in the database (such as theCreatedTimeorUpdatedTime), but it is different from{% now %}the purpose of obtaining the current system time.

Actual application in template

Generally, we would place the copyright information at the footer of the website,<footer>Within the tag. To ensure that all pages of the website are displayed uniformly and to avoid adding the same code repeatedly on each page, it is recommended to set it in the public template file of the website, such asbase.htmlorpartial/footer.htmlto configure.

Before yourbase.htmlorpartial/footer.htmlIn the file, find the location where copyright information is usually placed, and then replace the static year with a dynamic tag:

<!-- 示例:将静态年份替换为动态年份 -->
<footer>
    <p>&copy; {% now "2006" %} 您的公司名称. All Rights Reserved.</p>
    <!-- 其他页脚内容 -->
</footer>

After saving the template file, refresh your website, and you will find that the copyright year at the footer has automatically displayed as the current year.

Brings convenience and value

By using in Anqi CMS template{% now %}Tag dynamically displays the year, you have gained the following significant convenience and value:

  • Save time and energy:Say goodbye to the tedious work of manually updating the copyright year every year, giving you more time to focus on website content and operations.
  • Ensure information accuracy:The website's copyright information is always kept up to date to avoid outdated information due to forgetting to update.
  • Enhance the professionalism of the website:A real-time updated website detail, leaving visitors with a more professional, well-maintained impression.
  • Reduce maintenance costs:Automated processing details, reducing the need for manual intervention, thereby reducing the long-term maintenance costs of the website.

The AnQi CMS is committed to providing an efficient, customizable and easy-to-use content management solution.Such small features as dynamic year display are a reflection of the design philosophy in the details, aiming to help users manage and operate the website more conveniently.


Frequently Asked Questions (FAQ)

  1. Question:{% now "2006" %}and{{stampToDate(archive.CreatedTime, "2006")}}What is the difference?Answer: The main difference between them lies in the source of time acquisition.{% now "2006" %}The year displayed is the current year of the server, used for dynamic content that changes with the actual date, such as copyright information, real-time timestamps, and so on. And{{stampToDate(archive.CreatedTime, "2006")}}is used for formattingThe creation or update timestamp of specific content (such as articles or products)It displays the year of content publication or last modification and does not automatically update with the current year.

  2. Ask: Have I set up{% now "2006" %}Do I need to manually change the year every year?Answer: Absolutely not. Once you have correctly used it in the template{% now "2006" %}Label, the system will automatically retrieve and display the current year from the server. This means that your copyright information will always be up to date without any manual intervention.

  3. Question: Can I use{% now %}Does the label display other time information?Answer: Of course you can.{% now %}The label supports Go language time formatting strings. For example, you can use{% now "2006-01-02" %}Display the complete year, month, and day (e.g., 2023-10-27), or{% now "15:04" %}Display the current time (e.g., 10:30). Once you master the Go language time formatting reference rules (for example2006Represents the year,01Represents the month,02represents the day,15represents hours,04Represents minutes, etc.), you can flexibly combine it according to your needs to display any date and time format you want.

Related articles

How to retrieve and display the contact information configured on the website backend (such as phone number, address, social media links)?

It is crucial to display a company's contact information clearly and conveniently in website operations, as it not only enhances customer trust but also serves as a direct way for users to obtain services and consult.AnQiCMS (AnQiCMS) is a powerful content management system that provides intuitive backend configuration options and flexible template calling mechanisms, allowing you to easily manage and display this information.Next, we will explore how to configure and display your website's contact information in AnQiCMS, including phone number, address, social media links, and more.--- ## First Part

2025-11-08

How to create and display custom single-page content (such as About Us, Contact Information)?

In website operations, content such as 'About Us', 'Contact Information', and 'Privacy Policy' is usually displayed in the form of independent pages. This content is relatively fixed and plays an important role in the website structure.AnQiCMS (AnQiCMS) provides an intuitive and powerful set of features for creating and managing these custom single-page applications, allowing you to easily publish and update without writing code. ### Create Custom Single Page Content Creating a single page in Anqi CMS is a simple and direct process.Firstly, you need to log in to the back-end management interface. 1. **Enter the page management

2025-11-08

How to add and display dynamic breadcrumb navigation on a webpage?

In modern web design, breadcrumb navigation plays a crucial role.It is like a 'footprint' of the user on the website, clearly showing the position of the current page in the overall website structure, making it easy for visitors to understand, thereby greatly enhancing the usability and user experience of the website.At the same time, a reasonably set breadcrumb navigation also helps search engines better understand the structure of the website, which is very beneficial for SEO optimization.For users who use AnQiCMS to manage websites

2025-11-08

How to precisely control the display content, level, and selected status of a website navigation menu?

Website navigation, like a signpost of a website, a clear and intuitive navigation system can greatly enhance the user experience and help visitors quickly find the information they need.At the same time, a good navigation structure is also crucial for search engines to understand the hierarchy of website content, to effectively crawl and rank.In AnQi CMS, we can flexibly and accurately control the display content, level, and selected status of the website navigation, making it both beautiful and practical.

2025-11-08

How can AnQi CMS protect the copyright of original content through anti-crawling and watermark features when displayed?

On a day when digital content is increasingly abundant, the value of original content is self-evident, serving as the cornerstone for attracting users and building brand images on websites.However, the issues of content theft and malicious collection that follow also make countless creators and operators headache.Hardly written articles, carefully designed images, may be borrowed by others in a moment, even copied and pasted directly, which not only severely打击s the original enthusiasm, but also actually harms the intellectual property rights.For website operators, original content is the core competitiveness of the website.

2025-11-08

How to define and temporarily store variables in a template for easy content display and reuse?

When building a website, we often encounter situations where we need to repeat certain information or adjust content flexibly according to different contexts.If each time is hard-coded, it is not only inefficient but also quite麻烦 to maintain later.AnQiCMS (AnQiCMS) fully understands this, its template system provides various flexible ways to define and temporarily store variables in templates, thus achieving convenient display and efficient reuse of content.Why do you need to define and store variables in templates?

2025-11-08

How to determine whether the current item is the first or last in a looped article list so that special styles can be displayed?

In website content management, it is often encountered that there is a need: we hope that the first or last element of the article list, product list, or other data list can have special styles, such as the first article title being particularly prominent, or the last product detail not having a bottom separator line.This not only helps to highlight the key points, but also makes the visual effect of the page more hierarchical.AnQiCMS (AnQiCMS) powerful template engine provides us with a simple and efficient way to meet these customization needs.In Anqi CMS template system, we usually use `{% for ...

2025-11-08

How can AnQi CMS automatically replace keywords to display article or product content?

In content operations, we often need to ensure that specific keywords in articles or product descriptions are consistent, even automatically associated with related pages, which is crucial for SEO optimization and improving user experience.But manually modifying one by one takes time and is prone to errors, especially when the content of the website is massive, it is also a huge challenge.Anqi CMS knows this, and therefore provides us with a very practical feature, that is, the automatic replacement and display of keywords in articles or product content.This feature can intelligently identify the keywords or phrases you preset and automatically replace them with the specified text or link

2025-11-08