In website operation, it is a common requirement to flexibly display the current year or format specific time, whether it is the automatic update of the year in the copyright statement or the clear display of the publication time on the article detail page.AnQiCMS provides two main ways to meet these needs, they are both simple to use and follow the elegant time handling of the Go language.
Method one: Directly obtain and display the current year or time ({% now %}Label)
When you need to directly obtain and display the current year or the current time to the second in the template,{% now %}The tag is your preference. This tag is very intuitive, you just need to specify the Go language style time formatting string you want in it.The system will output according to the current time of the server, in the format you define.
For example, if you only want to display the current year in the website footer for copyright statements, you can write it like this:
© {% now "2006" %} 您的公司名称. All Rights Reserved.
After running, if the current year is 2024, it will automatically display as:© 2024 您的公司名称. All Rights Reserved.
If you need to display the full current date, for example “2024-07-29”:
今天的日期是:{% now "2006-01-02" %}
It will display as:今天的日期是:2024-07-29
If you need to display the current exact time, for example, "10:30:45":
当前时间是:{% now "15:04:05" %}
It will display as:当前时间是:10:30:45
{% now %}The flexibility of the tag lies in its ability to output various forms of current time information based on your formatted string, which greatly facilitates the display of dynamic content.
Method two: Format the existing timestamp variable ({{ stampToDate() }}function)
More often than not, we are not displaying the current time, but rather need to display the timestamp stored in the database (such as the publication time of an articleCreatedTime, update timeUpdatedTimePresented in a human-readable format. AnQiCMS provides{{ stampToDate() }}Function.
This function accepts two parameters: the first is the timestamp variable you want to format (usually a 10-digit or 13-digit number), and the second is a time formatting string in Go language style.
Assuming you are iterating through a list of articlesarchivesYou would like to display the publish time of each article, formatted as "July 29, 2024 10:30 AM":
{% archiveList archives with type="list" limit="5" %}
{% for item in archives %}
<div>
<h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
<p>发布于:{{ stampToDate(item.CreatedTime, "2006年01月02日 15时04分") }}</p>
</div>
{% endfor %}
{% endarchiveList %}
After running, the publication time of each article will be displayed in the specified Chinese format.
If you only want to display the date part, for example, "2024/07/29":
更新时间:{{ stampToDate(archive.UpdatedTime, "2006/01/02") }}
This will format the update timestamp of the article2024/07/29.
Understand time formatting strings (Golang style)
AnQiCMS continued to use the unique time formatting method of the Go language. Unlike many programming languages that use the symbol placeholder 'Y-m-d H:i:s' to define time format, Go language uses a fixed reference time2006-01-02 15:04:05Define the format. The formatting string you write is actually telling the Go language how to combine the time display effect you want according to the reference time.
- 2006Represents the year (Year)
- 01Represents the month (Month), a two-digit number with a leading zero
- 02Represents the day (Day), a two-digit number with a leading zero
- 15Represents hour (Hour), 24-hour format, two digits with a leading zero
- 04Represents minute (Minute), two digits with a leading zero
- 05Representing seconds (Second), two-digit numbers with a leading zero
By combining these reference numbers, you can create various time display formats:
"2006": Only shows the year, such as2024"01-02": Display month and day, such as07-29"2006-01-02": Standard date format, such as2024-07-29"2006年01月02日": Chinese date format, such as2024年07月29日"15:04": Hour and minute, such as10:30"2006-01-02 15:04:05": Complete date and time, such as2024-07-29 10:30:45
Mastered this reference time format in Go language, you can freely control the display of time and date in AnQiCMS templates.
Summary
Whether it is necessary to display the current year or time or to format the timestamp stored in the database, AnQiCMS provides intuitive and powerful template tags and functions. Through{% now %}Tags and{{ stampToDate() }}Functions, combined with the unique and flexible time formatting strings of Go language, you can easily implement dynamic display of various time information in website templates, providing users with a better browsing experience.
Frequently Asked Questions (FAQ)
- Why is the time format string '2006-01-02 15:04:05' this kind of numeric combination, not 'Y-m-d H:i:s'?AnQiCMS adopts the Go language time formatting standard. In Go language, unlike other languages, no letters are used to represent time units (such as Y representing the year), but a fixed reference time is used
2006年1月2日 15点04分05秒Come as a formatting template. Every number you write in the formatting string (such as2006/01/02These, as well as others, are all telling the Go language that you want to display the corresponding time units (years, months, days, etc.) at that position and in the format of that number.