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.