Flexibly display the current date and custom time format in the Anqi CMS template
In website operation, we often need to dynamically display date and time information on the page, whether it is the current year in the copyright statement, or the publication time of articles, event countdowns, and so on.AnQi CMS provides a very flexible and easy-to-use method to display the current year or a custom date and time format in templates, keeping your website content up-to-date and enhancing user experience.
The Anqi CMS template system adopts syntax similar to the Django template engine, making the display of dynamic content intuitive. The core of handling date and time is two powerful tags:stampToDateandnow.
Get the current year and time dynamically:nowThe clever use of tags
When you need to display the current year directly in the template, for example in the copyright statement of a website footer,nowThe label is your ideal choice. It can directly obtain the current date and time of the system and output it in the specified format.
For example, to display the current year in the website footer, we can use it like this:{% now "2006" %}
Here2006It is not an ordinary number, but a convention for date formatting in the Go language.In Go language, date formatting is based on a specific reference point: "2006-01-02 15:04:05 -0700 MST".You just need to remember the numbers corresponding to each part of this reference time point, and you can flexibly combine it into any format you want. For example:
2006Represents the year01Represents the month02Represents the date15representing hours (24-hour system)04representing minutes05representing seconds
So, if you want to display the current date in full format, such as '2023-01-02', you can write it as: {% now "2006-01-02" %}
If you need to be accurate to hours, minutes, and seconds, for example, “2023-01-02 15:04:05”, it can be done like this:{% now "2006-01-02 15:04:05" %}
nowThe power of tags lies in their ability to keep your website always displaying the latest year or time, without the need to manually update template files, greatly reducing the maintenance burden.
Format the existing timestamp:stampToDateThe application of tags
In addition to getting the current time, websites often need to display the time stored in the database, such as the publication time of articles (CreatedTime) or update time(UpdatedTimeThese times are usually stored in the form of Unix timestamps. AnQi CMS providesstampToDatetags to convert these timestamps into readable date and time formats.
stampToDateThe use of tags is:{{ stampToDate(时间戳, "格式") }}.
For example, on the article detail page, we often need to display the publication date of the article. Assuming the publication time field of the article object isCreatedTimeYou can format it like this:{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}
Or, if you want to display a simpler 'Year-Month-Day Hour:Minute' format:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04") }}
This is the same as using the date formatting rules of the Go language. By adjusting the format string, you can easily achieve various date display requirements, such as:
- Only show the month and date:
{{ stampToDate(archive.CreatedTime, "01-02") }} - Show Abbreviated English Month Names:
{{ stampToDate(archive.CreatedTime, "Jan 02, 2006") }} - Show the day of the week:
{{ stampToDate(archive.CreatedTime, "Mon, 02 Jan 2006") }}
tostampToDatewith the tag andarchiveDetailorarchiveListTag combination can be easily used to display the creation or update time of each article in a unified and beautiful format on the article list or detail page, greatly enhancing the readability of the content and the professionalism of the website.
Example of practical application scenarios
Website footer copyright statement:
© {% now "2006" %} {{ system.SiteName }} 版权所有。Display publication time on article detail page:
<p>发布于:<span>{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}</span></p>Display brief date on article list:
<small>更新日期:{{ stampToDate(item.UpdatedTime, "2006/01/02") }}</small>
MasternowandstampToDateThese tags, along with the Go language date formatting rules, allow you to freely control the display of date and time on the website in Anqi CMS.This can make your website content more dynamic and attractive, and also brings great convenience to the maintenance and update of the website.
Frequently Asked Questions (FAQ)
Q1: Why is the date format I set showing up as "2006-01-02 15:04:05" or another incorrect date?
A1: This is usually because you did not set the string formatting completely according to the Go language date formatting reference point. The date formatting in Go is quite special, it does not useY-m-dThis placeholder, instead, uses a fixed reference time point “2006-01-02 15:04:05” to represent the year, month, day, hour, minute, and second.Please carefully check that each number or letter in your format string matches the corresponding part at the reference time point.For example, the year must be2006instead ofYYYY, the month is01instead ofMM.
Q2:stampToDateandnowWhat is the difference between these tags, when should I use them?
A2:nowTags are used to get and displayCurrent system time. For example, the copyright year at the footer of a website ({% now "2006" %}It will dynamically display the current year of your website visit.stampToDateTags are used for formatting.Timestamps that already exist.These timestamps usually come from the content fields in databases, such as the publication time of articles (archive.CreatedTime). In simple terms, if you need to display the 'current' time, usenowUse it to display the time of a past point in timestampToDate.
Q3: Can I display a customized Chinese date format, such as “February 2, 2023”, and make it work?
A3: The date formatting function of Anqi CMS is mainly based on the reference time format of the Go language, which does not directly support converting numbers to Chinese numerals in uppercase. However, you can implement a Chinese date format like "2023 year 01 month 02 day", by directly including Chinese characters in the format string, for example{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}. For more complex Chinese uppercase number requirements, it may be necessary to use JavaScript for secondary processing on the front-end, or to develop a custom Go language backend function to meet the needs.