In website operation, keeping your content up-to-date in real-time, especially system time information like years or dates, not only enhances the professionalism of the website but also provides users with more accurate references.AnQiCMS as a flexible and efficient content management system, provides various convenient ways for you to easily implement this requirement in the template.

Next, let's learn together how to flexibly display the current year or other system time information in the AnQiCMS template.

Display the current system time directly.{% now %}Tag

In AnQiCMS templates, if you need to directly obtain and display the current year, date, or specific time when visiting the website,{% now %}Label is your helpful assistant.This label is very intuitive, you just need to follow it with a string representing the time format.Y-m-dformat.

For example, if you only want to display the current year as copyright information at the bottom of the website:

{# 只显示当前年份 #}
© {% now "2006" %} 您的网站版权所有。

If you need to display the full date (year, month, day), for example in the header or a specific area:

{# 显示完整的年月日 #}
今天是:{% now "2006年01月02日" %}

If the content requires greater precision, it is necessary to display detailed dates and times:

{# 显示详细的日期和时间 #}
当前时间:{% now "2006-01-02 15:04:05" %}

This is very useful in the footer copyright statement of the website, once set, the year can be automatically kept up to date, saving the trouble of manually updating every year, making the website content always feel fresh.

Format timestamps in articles or data —{{ stampToDate() }}Function

In addition to displaying the current system time, what we often encounter more frequently is the need to format the time information stored in the database (usually in timestamp format) for display.For example, the publication date or the latest update time of an article.

AnQiCMS provides a very convenient stampToDateA function that can convert a 10-digit Unix timestamp to the desired date and time format. You can find the document data obtained fromarchiveList/archiveDetailthese tags.CreatedTime(Creation time) orUpdatedTime(Update time), they are all timestamps.

The following are some examples of usingstampToDatefunction:

To display the publication date of the article, only keep the year, month, and day:

{# 显示文章的发布日期 #}
发布于:{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}

If you need to display update time accurate to the minute:

{# 显示精确的更新时间 #}
上次更新:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04") }}

If your design requires a more concise format, such as displaying only the month and date:

{# 只显示月份和日期 #}
{{ stampToDate(item.CreatedTime, "01-02") }}

This is an indispensable feature for scenarios that require displaying specific time points, such as blog posts, news updates, and product release dates. It ensures that users can easily obtain time information while browsing the content.

Go Language Tip for Time Formatting String

Understanding the time formatting rules in Go language is the key to mastering AnQiCMS time display. It is unlike some other systems that useY-m-d H:i:sSuch a placeholder, but use a special reference time:2006年01月02日 15时04分05秒(Also can be understood as)1月2日周一 下午03:04:05 MST 2006}]

You can consider each number or letter in this reference time as a placeholder for its corresponding time part. For example:

  • 2006Represents a four-digit year (e.g., 2023)
  • 01Represents two-digit months (e.g., 09)
  • Represents the Chinese characters for months
  • 02Represents two-digit dates (e.g., 15)
  • Represents the Chinese characters for dates
  • 15Represents the hour in 24-hour format (e.g., 14)
  • 04Represents minutes as a two-digit number (e.g., 30)
  • 05Represents seconds as a two-digit number (e.g., 59)
  • MonRepresent the English abbreviation of the day of the week (e.g., Wed)
  • MondayRepresent the English full name of the day of the week (e.g., Wednesday)
  • PMRepresent AM/PM (e.g., AM)
  • MSTor-0700Represent the time zone

By flexibly combining these elements, you can build any date-time format you want.For example, you can try such a format combination to display "2023-09-15 Wednesday 10:30 AM".

Overall, AnQiCMS handles time information in templates with both strength and flexibility.Whether it is to display the real-time system time of a website or present the publication update time of articles in an aesthetically pleasing manner, it can be easily achieved with simple tags and functions.Understanding the time formatting rules in Go can help you better master these features, making your website content management more intuitive.


Frequently Asked Questions (FAQ)

Q1: Why is the{{ stampToDate(...) }}function displaying time wrong?

A: First, please check the one you passed tostampToDateDoes the timestamp of the function is a 10-digit integer, which is usually a Unix second-level timestamp.If your timestamp is a 13-digit millisecond timestamp, you need to divide it by 1000 to convert it to a second timestamp.{{ stampToDate(archive.CreatedTime / 1000, "2006-01-02") }}. Then, confirm that your formatting string strictly follows the reference time format in Go language `2006`