In website operation and content management, displaying accurate dates and times is a basic and important requirement.Whether it's the copyright year at the footer, the publication or update date of the article, or the countdown for a specific event, flexibly controlling the date and time format can greatly enhance user experience and the professionalism of the website.In AnQiCMS, you can easily display the current year or a custom date-time format in templates by using several simple and practical methods.


An, display the current system year or date and time: using{% now %}tags

When you need to display the current year of the website, for example in the footer copyright information, or need to show the current time to the minute and second,{% now %}Tags are a very convenient choice. This tag will fetch the current system time of the AnQiCMS server and output it in the format you specify.

Its basic usage is:

{% now "格式化字符串" %}

Here“格式化字符串”It is crucial.AnQiCMS uses Go's date and time formatting rules, which are different from the formats you may be familiar with, such as 'YYYY-MM-DD HH:mm:ss'.2006-01-02 15:04:05Come as a formatting template. You need to replace each number in this reference time point with the actual date and time components that need to be displayed.

For example, if you want to display the current year, just replace2006with2006:

© {% now "2006" %} 版权所有

This will display on your website footer as© 2023 版权所有(assuming the current year is 2023).

If you need to display the full current date, you can do it like this:

今天是:{% now "2006年01月02日" %}

Or, if you need a more precise time, including hours and minutes:

当前时间是:{% now "15:04" %}

By flexible combination2006(Year),01(Month),02(Day),15(Hour),04(Minute),05and other elements, you can customize various date and time formats.


Secondly, format the date and time in the content: usestampToDatefunction

In AnQiCMS, many content fields, such as articles,CreatedTime(Creation time) andUpdatedTime[Updated time], which is stored in the database in the form of a timestamp (usually a 10-digit or 13-digit number).These timestamps are not user-friendly to display directly.stampToDatea function to convert these timestamps into a readable date and time format.

stampToDateThe usage of the function is:

{{ stampToDate(时间戳变量, "格式化字符串") }}

Similarly, here,“格式化字符串”Also follow the Go language.2006-01-02 15:04:05Refer to the format.

Assuming you are looping through content on the article detail page or article list page,itemIt is the variable of the current article, and you can display the publishing date of the article like this:

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

If you want to display a more detailed publishing time, including hours, minutes, and seconds, on the article detail page:

<p>发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04:05") }}</p>

or only show the month and date:

<p>发表于:{{ stampToDate(item.CreatedTime, "01月02日") }}</p>

Also, if you only want to extract the year from the timestamp, for example, to display a brief year in the article list:

<p>年份:{{ stampToDate(item.CreatedTime, "2006") }}</p>

stampToDateThe strength of functions lies in their ability to convert any valid timestamp into the precise format you need, thereby satisfying the needs of different content displays.


Summary and Tips

AnQiCMS through{% now %}Tags andstampToDateFunction, provides flexible and diverse date and time display and formatting capabilities.

  • {% now %}tags:For scenarios that require displaying the current system time (such as copyright year, real-time time).
  • stampToDatefunctionFor scenarios that require formatting stored timestamps (such as article publication/update time).

A tip.:The date format string in Go language indeed requires some time to adapt. Remember2006年01月02日 15时04分05秒This reference date, what do you want to display, replace it with2006/01/02numbers correspond to. For example, if you want to display a two-digit year, use06; if you want to display a two-digit month, use01The best way to learn is through practice, you can try different combinations in your own AnQiCMS template to see the effect.

Master these methods, and you can better control the date and time display of website content, enhancing the overall professionalism and user experience of the website.


Common Questions (FAQ)

  1. Why is the date format string of AnQiCMS in this form?2006-01-02 15:04:05Instead of the common form,YYYY-MM-DD?Answer: This is because AnQiCMS is developed based on Go language. Go language does not use likeYYYY-MM-DDSuch symbols are placeholders, instead using a fixed reference time2006年01月02日15时04分05秒Define the format. You just need to replace the date and time part you want to display with the corresponding number in this reference time (for example, the year with2006, the month with01, the hour with15),The system will output the corresponding date and time according to your template.

  2. Question: I only want to display the current year in the footer, for example,2023How should I write it?Answer: You can use{% now "2006" %}Label. This will directly obtain the current year of the server and display it. For example, the complete copyright information might be&copy; {% now "2006" %} 您的网站名称。

  3. Question: If I want to display the publication time of the article, in the format of2023年6月30日 12点30分How should I do that?Answer: You can combinestampToDatefunction formatting rules of Go language to achieve. Assuming the article publish time is stored initem.CreatedTime, you can write it like this:{{ stampToDate(item.CreatedTime, "2006年1月2日 15点04分") }}. Note that,1 represents the month without leading zeros,2Represents dates without leading zeros, which provides greater flexibility.