In website operation, we often need to dynamically display the current year, date, or time on the page, such as the copyright information in the footer, or the publication or update time of articles.This information, if manually updated, is not only time-consuming and labor-intensive, but also prone to errors.Fortunately, AnqiCMS provides a concise and efficient method for easily obtaining and displaying these dynamic time information in templates.
Dynamically retrieve and display the current year or other time information
In the AnqiCMS template, to retrieve and display the current year or any format of the current time, we can use the built-in{% now %}The label is very direct. It will output the current date and time of the server according to the specified format.
For example, if you want to display the current year in the footer of the website, you can use it like this:
© {% now "2006" %} 您的网站名称。保留所有权利。
Here"2006"It is not an arbitrary year, but a special reference to the time format defined in Golang. For example, if you want to display the current full date,2023-10-26It can be written like this:
今天日期:{% now "2006-01-02" %}
For example, if you need to display more detailed time information,2023-10-26 15:30:00:
当前时间:{% now "2006-01-02 15:04:05" %}
{% now %}The flexibility of the label lies in its ability to output any current time representation you want, based on the Golang time format string you provide.
Format an existing timestamp
In addition to displaying the current time, a more common need is to format the timestamps stored in the website content (such as articles, products). AnqiCMS will provide in document details, lists, and other tags.CreatedTimeandUpdatedTimeSuch timestamp fields. These fields are typically 10-digit Unix timestamps, so that they can be displayed in a more readable format for users, we can use{{ stampToDate(时间戳, "格式") }}This template function.
For example, on the detail page of a blog post, you may want to display its publication date and time:
发布于:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}
This willarchive.CreatedTimeThis timestamp is converted to2023年10月26日 15:30This format. Similarly, if you want to display the update time:
最后更新:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05") }}
Understand Golang time formatting rules deeply
The date and time formatting in AnqiCMS templates follows the specific rules of the Golang language. This is different from what we usually useYYYY-MM-DDThe pattern is different, and it may seem a bit special at first contact.
Golang uses a fixed reference time.2006-01-02 15:04:05.999999999 -0700 MSTDefine the time format. You need to replace the parts of this reference time with the format you want, rather than using placeholders.
Here are some commonly used reference mappings:
- Year:
2006-u003eYYYY(Four-digit year) - Month:
01-u003eMM(Two-digit month, leading zero if necessary) orJan(Abbreviation in English) orJanuary(Full name in English) - Date:
02-u003eDD(Two-digit date, leading zero if necessary) - hours:
15-u003eHH(24-hour format) or03(12-hour format) or3(12-hour format without leading zeros) - minute:
04-u003eMM(two-digit minutes, pad with zeros if necessary) - seconds:
05-u003eSS(two-digit seconds, pad with zeros if necessary) - Week:
Mon(Abbreviation in English) orMonday(Full name in English) - AM/PM:
PM(and3used with the 12-hour clock)
Once you have mastered this mapping relationship, you can very flexibly combine various time formats. For example:
"2006/01/02"-u003e2023/10/26"01-02"-u003e10-26(Display only month and day)"15:04"-u003e15:30(Display only hour and minute)"Monday, Jan 2, 2006"-u003eThursday, Oct 26, 2023(English weekdays, months)
Application scenarios in practice
- Footer copyright informationEnsure that it updates automatically every year, without manually modifying the template file.
<p>© {% now "2006" %} {{ system.SiteName }} 版权所有。</p> - Timestamp on the article or product detail pageClearly display the publishing or latest update date of the content, enhancing user experience and content timeliness.
<p>发布时间:{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}</p> {% if archive.UpdatedTime > archive.CreatedTime %} <p>最后更新:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04") }}</p> {% endif %} - Any display that requires dynamic date information.For example, display the start date of the activity, system running time, etc.
By{% now %}Tags and{{ stampToDate() }}A function combined with Golang's flexible time formatting rules makes AnqiCMS easy and powerful to display dynamic time information.No matter whether it is displaying the copyright year at every corner of the website or the precise content release time down to the second, you can easily achieve it and ensure that this information is always kept up-to-date and accurate.
Frequently Asked Questions (FAQ)
1. Why do I use{% now "YYYY" %}Unable to display the current year?
This is because AnqiCMS's time formatting follows the rules of the Golang language instead of the commonYYYY-MM-DDorYYYYPlaceholder pattern. In Golang, you need to use a fixed reference time2006-01-02 15:04:05To define your format. So, to display the current year, you should use{% now "2006" %}.
2.{{ stampToDate(item.CreatedTime, "...") }}Error, what is the reason?
It is usually possible to encounter this error,item.CreatedTimeThe variable itself is not a valid timestamp (for example, it is a null value, a string type, or another non-numeric type), or the first argument you are trying to pass is not a timestamp. Make surestampToDateThe first parameter of the function is a valid 10-digit or 13-digit Unix timestamp.
3. Can I display English months or weekdays?
Of course you can. Golang supports various English representations for time formatting. For example, to display the English abbreviation of the month, you can useJan; To display the English name of the weekday, you can useMondayorMon. For example:{{ stampToDate(archive.CreatedTime, "Monday, Jan 02, 2006") }}It will display similarThursday, Oct 26, 2023format.