In website operation and content management, displaying accurate date and time is a basic and important requirement.Whether it is the copyright year at the footer, the publication or update date of the article, or the countdown for a specific event, flexible control over date and time formats can greatly enhance user experience and the professionalism of the website.In AnQiCMS, you can easily implement the display of the current year or a custom date and time format in templates using several simple and practical methods.
1. Display the current system year or date and time by using{% now %}Tag
When you need to display the current year of the website, for example, in the footer copyright information, or need to display the current time to the minute and second,{% now %}The tag is a very convenient choice. This tag will get the current system time of the AnQiCMS server and output it in the specified format.
The basic usage is:
{% now "格式化字符串" %}
Here“格式化字符串”It is crucial. AnQiCMS uses Go language date and time formatting rules, which may be different from the formats you are familiar with such as 'YYYY-MM-DD HH:mm:ss'.Go language uses a specific reference point2006-01-02 15:04:05Come as a formatting template. You need to replace each number in this reference timestamp with the actual date and time components that need to be displayed.
For example, if you want to display the current year, just2006Replace2006:
© {% now "2006" %} 版权所有
This will be displayed in the footer of your website as© 2023 版权所有(assuming the current year is 2023).
If you need to display the current complete 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),05(Second) elements, you can customize various date and time formats.
Secondly, format the date and time in the content: usingstampToDatefunction
In AnQiCMS, many content fields, such as the articles'CreatedTime(creation time) andUpdatedTime(Updated time), it is stored in the database in the form of a timestamp (usually a 10-digit or 13-digit number).Directly displaying these timestamps is not user-friendly. At this point, we needstampToDatefunction to convert these timestamps into a readable date and time format.
stampToDateThe usage of the function is:
{{ stampToDate(时间戳变量, "格式化字符串") }}
Similarly, here is“格式化字符串”Also follow the Go language2006-01-02 15:04:05reference format.
Suppose you are looping through the article detail page or article list page anditemIs the variable of the current article, you can display the publication date of the article like this:
<p>发布日期:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}</p>
If you want to display more detailed publication time on the article detail page, including hours, minutes and seconds:
<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>
In addition, 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 power of functions lies in their ability to convert any valid timestamp into the precise format you need, thus meeting the needs of different content displays.
3. Summary and Tips
AnQiCMS uses{% now %}Tags andstampToDateFunctions, providing flexible and diverse date and time display and formatting capabilities for you.
{% now %}Tag: Suitable for scenarios that require displaying the current system time (such as copyright year, real-time time).stampToDatefunction: Suitable for scenarios where you need to format timestamps stored in content (such as article publication/update time).
A tip.: The date format string in Go language indeed needs some time to adapt. Remember2006年01月02日 15时04分05秒This reference date, what do you want to display, replace it with2006/01/02corresponding numbers. For example, if you want to display a two-digit year, use06; if you want to display a two-digit month, use01. Practice is the best way to learn, you can try different combinations in your own AnQiCMS template and 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.
Frequently Asked Questions (FAQ)
Ask: Why is the date formatting string of AnQiCMS
2006-01-02 15:04:05in this form instead of the commonYYYY-MM-DD?Answer: This is because AnQiCMS is developed based on Go language. Go language does not use likeYYYY-MM-DDThis symbol placeholder is instead used a fixed reference time2006年01月02日15时04分05秒Define the format. You only need to replace the date and time part you want to display with the corresponding number in this reference time (for example, the year is2006, the month is01, the hour is15),The system will output the corresponding date and time based on your template.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 retrieve the current year of the server and display it. For example, the complete copyright information may be© {% now "2006" %} 您的网站名称。Ask: If I want to display the publication time of the article, the format is
2023年6月30日 12点30分How do I do that?Answer: You can combinestampToDatefunction and formatting rules of Go language to implement. 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,1represents the month without a leading zero,2Represents a date without a leading zero, which provides greater flexibility.