In website operation, it is often necessary to keep the web content up-to-date or accurately display the time of specific events. Whether it is the copyright year in the footer, the publication date of the article, or the update time of the product, these dynamic time information can make the website look more professional and active.AnQiCMS provides a very flexible and intuitive way, allowing you to easily display the current year or a specified format of time in templates.
Next, we will explore two main methods together, allowing your AnQiCMS website to flexibly display various time information.
一、Display the current year or real-time time in a specified format: using{% now %}tags
When you need to display the current year at any location on the website, such as in the copyright statement of the website footer, or to display the current full date and time in some dynamic module, AnQiCMS provides a concise{% now %}Label.
This tag is specifically used to retrieve and format the current system time.
To display the current year, simply add this line of code to the template file:
{% now "2006" %}
So, your website footer can automatically update to the current year without manual modification each year.
If you need to display more specific, more real-time dates or times, such as:
Display the full current date (year-month-day):
{% now "2006-01-02" %}Effect:
2023-10-27Display the current time with hours, minutes, and seconds:
{% now "2006-01-02 15:04:05" %}Effect:
2023-10-27 10:30:15
By adjusting{% now %}The format string after the label, you can let the current time be presented in any style you want.
二、Format existing timestamps: use{{ stampToDate() }}tags
In AnQiCMS, articles (archive), comments (comment), or users (user) content usually stores publishing time or update time information, which is generally stored in the database in the form of timestamps.When you want to display these data on the front-end page, you need to convert these timestamps into a date or time format that is easy to read.
This is,{{ stampToDate() }}this built-in function comes in handy. It can accept a timestamp and a format string, and then output the formatted time.
For example, on the article detail page or article list page, you want to display the publication date of the article:
{% archiveList archives with type="list" limit="10" %}
{% for item in archives %}
<div>
文章标题:{{ item.Title }}<br>
发布日期:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}
</div>
{% endfor %}
{% endarchiveList %}
Here,item.CreatedTimeThat is the timestamp of the article,"2006年01月02日"which is the output format we hope for. If the article'sCreatedTimeThe corresponding time is2023-10-27 10:30:15so thatstampToDateThe function will display it as2023年10月27日.
Similarly, if you need to display the update time of the article, you can useitem.UpdatedTime:
更新时间:{{ stampToDate(item.UpdatedTime, "2006-01-02 15:04") }}
Deep understanding of time formatting: Golang style "magic numbers"
You may have noticed that whether{% now %}Or{{ stampToDate() }}, they all use the time format string:2006-01-02 15:04:05such a combination of numbers, rather than commonYYYY-MM-DDEnglish.This is actually a feature developed by AnQiCMS based on Go language.
2006年1月2日 15点04分05秒 -0700 MST
You can understand this date and time as a "template", by selectively using or combining the various numbers and characters within it to construct the date and time format you want.
The following are some commonly used correspondences and examples to help you quickly master them:
- Year:
2006(Four-digit year) or06(Two-digit year) - Month:
01(Two-digit month),1(Month without leading zero),Jan(Month abbreviation),January(Month full name) - Date:
02(Two-digit date),2(No leading zero date) - Hour:
15(24-hour format),03(12-hour format, with leading zero),3(12-hour format, without leading zero) - Minutes:
04(Two-digit minutes),4(No leading zero minutes) - Seconds:
05(Two-digit seconds),5(No leading zero seconds) - AM/PM marker:
PMorpm - Weekday:
Mon(Monday abbr),Monday(Monday full name)
More formatting examples:
| Target format | Format string | Example output (assuming the time is)2023年10月27日 10点30分15秒) |
|---|---|---|
| Year-Month-Day (Hyphen) | "2006-01-02" |
2023-10-27 |
| Year-Month-Day (Slash) | "2006/01/02" |
2023/10/27 |
| Month-Day-Year | "01/02/2006" |
10/27/2023 |
| Day-Month-Year | "02/01/2006" |
27/10/2023 |
| Chinese Year-Month-Day | "2006年01月02日" |
2023年10月27日 |
| Time, minutes, and seconds (24-hour format) | "15:04:05" |
10:30:15 |
| Time (12-hour format, with AM/PM) | "03:04 PM" |
10:30 AM |
| Day of the week | "Monday" |
Friday |
| Full date and time (including day of the week) | "Monday, 02 Jan 2006 15:04" |
Friday, 27 Oct 2023 10:30 |
By proficiently using these 'magic numbers', you will be able to easily handle various date and time display requirements.
Summary
In AnQiCMS, displaying the current year or a specified format of time mainly depends on{% now %}and{{ stampToDate() }}these two powerful tags.{% now %}suitable for obtaining the real-time current time,{{ stampToDate() }}Used to format existing timestamps in the content.Mastering the time formatting rules in Go language style is the key to flexibly using these tags.By simple code, you can keep your website's time information accurate and beautiful.
Common Questions (FAQ)
{% now %}and{{ stampToDate() }}What is the difference?{% now %}Used to obtain and display the real-time date and time of the current server. It does not depend on any existing data, and the latest time will be displayed every time the page is loaded.{{ stampToDate() }}Used to format timestamps stored in the database. For example, the publication time of the articlearchive.CreatedTimeor update timearchive.UpdatedTimeare timestamps that need to bestampToDate()formatted in order to be readable by humans