Display the current date and custom time format flexibly 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 time of article publication, event countdown, 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 template system of AnQi CMS adopts syntax similar to Django template engine, which makes the display of dynamic content intuitive. The core of handling date and time is two powerful tags: stampToDateandnow.
English translation: Dynamicly get the current year and time:nowThe magic use of tags
When you need to display the current year directly in the template, for example, in the copyright statement of the website footer,nowLabels are your ideal choice. It can directly obtain the current date and time of the system and output them in the format you specify.
For example, to display the current year at the footer of the website, 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 time point: "January 2, 2006 at 15:04:05 MST".You only need to remember the numbers corresponding to each part of this reference time point, and you can flexibly combine them to form any format you want.
2006Represents the year01represents the month02represents the date15Represents hours (24-hour format)04Represents minutes05Represents seconds
So, if you want to display the current date in full format, like “2023-01-02”, you can write it as: {% now "2006-01-02" %}
If you need to be precise to hours, minutes, and seconds, for example, "2023-01-02 15:04:05", you can do it like this:{% now "2006-01-02 15:04:05" %}
nowThe strength of the label is that it allows your website to always display the latest year or time without manually updating the template file, thus greatly reducing the maintenance burden.
Format the existing timestamp:stampToDateApplication of tags
In addition to getting the current time, more often on the website it is necessary to display the time stored in the database, such as the publication time of articles (CreatedTime) or update time (UpdatedTime)。These times are usually stored in the form of Unix timestamps. The security CMS providesstampToDatetags to convert these timestamps into a readable date and time format.
stampToDateThe usage method of the label 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 isCreatedTimeEnglish, You can format it like this:{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}
Or, if you want to display a more concise 'Year-Month-Day Hour:Minute' format:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04") }}
Here the same Go language date formatting rules are followed. By adjusting the format string, you can easily achieve various date display needs, such as:
- Display only the month and date:
{{ stampToDate(archive.CreatedTime, "01-02") }} - Show English month abbreviations:
{{ stampToDate(archive.CreatedTime, "Jan 02, 2006") }} - Display the day of the week:
{{ stampToDate(archive.CreatedTime, "Mon, 02 Jan 2006") }}
tostampToDateTags andarchiveDetailorarchiveListLabels can be combined to easily display the creation or update time of each article in a unified and beautiful format in article lists or detail pages, 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 publish 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 date formatting rules of the Go language, allow you to freely control the display of date and time on the website in Aiguo CMS.This not only makes your website content more dynamic and attractive, but also brings great convenience to the maintenance and update of the website.
Common Questions and Answers (FAQ)
Q1: Why did I set the date format, but it still shows up as "2006-01-02 15:04:05" or other incorrect dates?
A1: This is usually because you did not set the format string completely according to the Go language's date formatting reference points. The date formatting in Go is quite special, as it does not useY-m-dThis placeholder does not use a specific date and time, but instead uses the corresponding numbers from the fixed reference time point "2006-01-02 15:04:05" to represent year, month, day, hour, minute, and second.Please carefully check whether each number or letter in your format string matches the corresponding part of the reference time point.2006InsteadYYYYThe month is01InsteadMM.
Q2:stampToDateandnowWhat is the difference between these two tags, and when should I use them?
A2:nowTags are used to get and displaythe current system time. For example, the copyright year of the website footer ({% now "2006" %}),It will dynamically display the current year of your website visit.stampToDateTags are used for formatting.Timestamps that already exist.These timestamps are usually sourced from the content field in a database, such as the publication time of articles, likearchive.CreatedTime). In short, if you need to display the time as "now", usenow需要显示“过去某个时间点”的时间就用EnglishstampToDate.
Q3: 我想显示自定义的中文日期格式,比如“二〇二三年一月二日”,可以实现吗?English
A3: 安企CMS的日期格式化功能主要基于Go语言的参考时间格式,它不直接支持将数字转换为中文大写数字。但是,您可以实现像“2023年01月02日”这样的中文日期格式,方法是直接将中文文字包含在格式字符串中,例如{{ 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 custom Go language backend functions to meet the needs.