How to get and display the current year in AnQiCMS template?

Calendar 👁️ 60

When building and maintaining websites, we often need to display some dynamic information on the page, such as the current year.This is very useful for copyright statements, annual reports, or any scenario that requires real-time year information.AnQiCMS with its concise and efficient template system makes it easy to obtain and display the current year.

AnQiCMS's template system has borrowed the syntax of the Django template engine, which means it provides intuitive tags and variables to manipulate content.Next, we will discuss how to easily implement this feature in your AnQiCMS template.

The core method to get the current year:{% now %}Tag

AnQiCMS built-in a powerfulnowTags, used specifically to retrieve and display the current time in templates. To get the current year, we just need to use it in conjunction with the Go language's time formatting rules.

The basic usage is:{% now "格式字符串" %}.

This "format string" is crucial. For years, we usually use"2006"As a format string. You may wonder why it is "2006" instead of the current year, such as "2023" or "2024"?This is the unique feature of Go language time formatting.In Go language, the time format is not used withYYYY/MMsuch placeholders, but uses a fixed reference time2006-01-02 15:04:05.999999999 -0700 MSTDefine the output format. You need to write the reference value for the time part you want into the format string, and Go will infer the output format you want based on your input.

So, when you use{% now "2006" %}it will output the current year based on the year part "2006" in the reference time.

Actual operation: add the current year to your template

One of the most common use cases is to display the current year in the copyright statement at the bottom of the website.

For example, if you want to display “© 2024 Your website name All rights reserved.” in the footer, you can do so in your template file (such asfooter.htmlorbash.html) as follows:

<footer>
    <p>&copy; {% now "2006" %} {% system with name="SiteName" %} 版权所有.</p>
</footer>

In this code block:

  • {% now "2006" %}It will be automatically replaced with the current year.
  • {% system with name="SiteName" %}It is a tag used in AnQiCMS to get the website name from the backend settings, it ensures that the website name is also dynamically displayed.

This will automatically update the year in the footer of your website every year, without the need for manual modification, making it convenient and worry-free.

More time formatting options

Except for the year,nowLabel combined with Go language reference time format, it can also output various other time information. Just modify the format string:

  • Only display two digits of the year: {% now "06" %}(For example: 24)
  • Display the full date: {% now "2006-01-02" %}(For example: 2024-03-15)
  • Display time (hours:minutes:seconds): {% now "15:04:05" %}(For example: 10:30:00)
  • Display the full date and time: {% now "2006-01-02 15:04:05" %}(For example: 2024-03-15 10:30:00)
  • Display the English abbreviation of the month: {% now "Jan" %}(For example: Mar)
  • Display the English abbreviation of the day of the week: {% now "Mon" %}(For example: Fri)

For example, if you want to display the current date and time on a page, you can do it like this:

<p>当前更新时间:{% now "2006年01月02日 15时04分" %}</p>

For existing timestamps:stampToDateTag

In addition to getting the current time, sometimes you may need to format a timestamp obtained from a database or other place (usually a 10-digit or 13-digit number). AnQiCMS also providesstampToDateLabels to handle this situation.

For example, if you have a variable namedarchive.CreatedTimeThe variable stores the document's creation timestamp, you can format it like this:

<p>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</p>

HerestampToDateThe tag follows the Go language's time formatting rules, allowing you to flexibly display any timestamp data.

Tips and **Practice

  1. Memorize the Go language time formatting rules:The Go language's time formatting is indeed unique, remember2006-01-02 15:04:05This reference time, then replace the corresponding numbers and letters according to what you want, will make you twice as effective.
  2. Clear the cache:After modifying the template code, if the page does not update immediately, remember to clear the system cache of AnQiCMS backend to ensure that the new code takes effect.
  3. Template test:Before going live, test several time formats in the development environment to ensure they display as expected.

By{% now %}and{{ stampToDate }}These simple yet powerful tags, AnQiCMS allows you to easily obtain and display various dynamic time information in templates, adding flexibility and automation to your website.


Frequently Asked Questions (FAQ)

Q1:{% now "2006" %}What does the '2006' in the tag mean? Why isn't it the current year?

A1: “2006” is not a specific year but one of the Go language time formatting rulesReference valueThe Go language uses a fixed date and time.2006年01月02日 15时04分05秒 -0700 MSTAs a formatted template. When you use{% now "2006" %}When, the Go language will extract and display the current year based on the format string you provide (here is the year part corresponding to the reference date “2006”). If you want to display the month, you will use{% now "01" %}Because the reference date contains "01", it represents January.

Q2: Besides the year, can I also use{% now %}tags to get what time information?

A2: Combine the reference time format of the Go language,{% now %}Labels are very flexible. You can get almost all the date and time information you want, such as:

  • Month numbers: {% now "01" %}(such as 03)
  • Date number: {% now "02" %}(such as 15)
  • Hour (24-hour format): {% now "15" %}(such as 10)
  • Minute: {% now "04" %}(such as 30)
  • seconds: {% now "05" %}(such as 00)
  • Full name of month: {% now "January" %}(such as March)
  • Full name of the day {% now "Monday" %}(such as Friday)

You can combine these reference values as needed to create a custom date and time format.

Q3: If I need to format the timestamp stored in the database (such as the article posting time), instead of the current time, which tag should I use?

A3: If you need to format the data stored in the database of timestamp(usually an integer representing the number of seconds), you should use the tags provided by AnQiCMS of{{ stampToDate(时间戳变量, "格式字符串") }}. For example, ifarchive.CreatedTimeis a timestamp, you can use{{ stampToDate(archive.CreatedTime, "2006-01-02") }}to format it asYYYY-MM-DDin the form of.{% now %}tags are used to get and formatCurrent system time.

Related articles

How to integrate the message form into the AnQiCMS template and dynamically display the custom form fields configured in the backend?

In today's digital world, websites are not just platforms for displaying information, but also important bridges for interaction and communication with users.A well-designed, feature-complete feedback form that can greatly enhance user experience, help websites collect feedback, and gain business opportunities.AnQiCMS (AnQiCMS) is well-versed in this, providing powerful message form integration capabilities. Particularly impressive is that it allows us to flexibly configure custom form fields through the backend and dynamically present these fields in the frontend template, thereby meeting various personalized business needs

2025-11-07

How to display the comment list of articles in AnQiCMS template and support pagination?

Display article comment list and pagination elegantly in AnQiCMS template Article comments are an important part of website interaction, not only can they enhance user engagement, but also bring richer discussions and value to the website content.For operators, how to clearly and efficiently display these comments on the page and support pagination is the key to improving user experience.AnQiCMS provides a set of intuitive and powerful template tags, allowing you to easily achieve this goal.### Understand AnQiCMS template basics AnQiCMS uses similar

2025-11-07

How to implement the parameter filtering function of the article list in AnQiCMS (such as filtering by custom properties)?

As the content of the website becomes richer, users often hope to find the information they are interested in more accurately, rather than searching for a needle in a haystack of disordered content.For operators, providing flexible article list filtering functions can not only significantly improve user experience but also effectively help users discover more related content, thereby increasing the time spent on the website and interaction.AnQiCMS fully understands this need, through its highly flexible content model and powerful template tag system, making it extremely simple and intuitive to implement the parameter filtering function for the article list.Even if it is a custom attribute

2025-11-07

How to display Markdown formatted article content in AnQiCMS template and render it correctly to HTML?

In a content management system, Markdown has become the preferred writing format for many content creators, it is concise, efficient, and easy to convert to structured HTML.For AnQiCMS users, using Markdown to write articles can not only improve writing efficiency but also better organize content structure.Luckyly, AnQiCMS deeply supports Markdown formatted article content and provides a perfect mechanism to ensure that they are correctly rendered into beautiful HTML pages on the website front-end.### Start

2025-11-07

How to implement multilingual site switch link display in AnQiCMS template and set hreflang?

Building multilingual websites in AnQiCMS and providing convenient language switching functions and correct SEO settings is a key step in expanding the international market and enhancing user experience.As a modern content management system developed based on the Go language, AnQiCMS provides strong and flexible support in this aspect, allowing content operators to easily manage global content publishing needs.

2025-11-07

How to dynamically fetch and display the cover thumbnail of an article or category in the AnQiCMS template?

In website operations, carefully designed thumbnails are crucial for attracting user clicks, enhancing page aesthetics, and optimizing search engine (SEO) performance.AnQiCMS as a powerful content management system fully considers this point, providing a flexible and convenient way to manage and call the cover thumbnails of articles, categories, and other content. ### AnQiCMS Thumbnail Mechanism Overview The AnQiCMS thumbnail mechanism is very intelligent and user-friendly.When publishing an article, the system allows you to manually upload a thumbnail.

2025-11-07

How to display any custom content of the background custom settings in the template of AnQiCMS (such as the custom help page URL)?

In website operation, we often encounter situations where we need to quickly adjust some information, such as updating the help page link, changing the status of a feature switch, or displaying some infrequently changed but flexible custom text on the page.If each modification requires delving into the code file, it will undoubtedly greatly reduce efficiency.AnQiCMS (AnQiCMS) fully understands this pain point and therefore provides a very convenient custom setting mechanism that allows you to easily configure any content in the background without writing code, and display it in the website template.### Backend Settings

2025-11-07

How to print the detailed structure and value of variables for debugging in AnQiCMS template?

During AnQiCMS template development, we sometimes encounter variables that are not displayed as expected, or need to gain a deeper understanding of the specific structure and value of a variable in order to better perform content layout and logical control.AnQiCMS uses a syntax similar to the Django template engine, simple yet powerful, but even so, mastering debugging skills can also make our development process smoother and more efficient. Today, let's talk about how to accurately view the detailed structure and value of variables in the AnQiCMS template, allowing you to have a deep insight into the data in the template.###

2025-11-07