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

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

Directly display the current system time——{% now %}Tag

In AnQiCMS templates, if you need to directly obtain and display the current year, date, or specific time of the website visit,{% now %}Tags are your good helper. The use of this tag is very intuitive, you just need to follow it with a string representing the time format.It should be noted that, AnQiCMS is developed based on Go language, so the time format string follows the special conventions of Go language rather than the common onesY-m-dformat.

For example, if you 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 more accuracy, detailed date and time should be displayed:

{# 显示详细的日期和时间 #}
当前时间:{% 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 manual updates every year, keeping the website content always fresh.

Format the timestamp in the article or data -{{ stampToDate() }}function

In addition to displaying the current system time, we are more often faced with 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 convenientstampToDateA function that can convert a 10-digit Unix timestamp to the desired date and time format. You can findarchiveList/archiveDetailthe document data obtained from the tags.CreatedTime(Creation Time) orUpdatedTime(Update time), they are all timestamps.

Here are some uses ofstampToDateExample of a function:

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 the update time 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 for blog posts, news updates, product release dates, etc., ensuring that users can clearly obtain time information while browsing the content.

Go language time formatting string tips

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

You can treat 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 character representation of the month
  • 02Represents two-digit dates (e.g., 15)
  • Represents the Chinese character representation of the date
  • 15represents the hour in 24-hour format (e.g., 14)
  • 04represents the two-digit minute (e.g., 30)
  • 05represents the two-digit second (e.g., 59)
  • MonAbbreviation for the day of the week (e.g., Wed)
  • MondayFull name for the day of the week (e.g., Wednesday)
  • PMRepresenting AM/PM (e.g., AM)
  • MSTor-0700Representing time zone

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

In general, AnQiCMS is powerful and flexible in handling time information in templates.Whether it is to display the real-time system time of a website or to present the release update time of an article in an aesthetic way, it can be easily achieved through simple tags and functions.Understanding the time formatting rules in Go language will help you better master these features, making your website content management more convenient.


Frequently Asked Questions (FAQ)

Q1: Why my{{ stampToDate(...) }}The function displays the time incorrectly?

A: First, please check what you pass tostampToDateIs the timestamp of the function a 10-digit integer, which is usually a Unix second timestamp.If your timestamp is a 13-digit millisecond timestamp, you need to divide it by 1000 first to convert it to a second-level timestamp. For example:{{ stampToDate(archive.CreatedTime / 1000, "2006-01-02") }}. Secondly, confirm that your format string strictly follows the Go language's reference time `2006