In website operation, it is often necessary to dynamically display the current year or a specific date and time format at different positions on the page, whether it is for the copyright statement in the footer, the publishing and updating time of articles, or the time points of various events.AnQiCMS (AnQiCMS) provides flexible and powerful template tags, allowing you to easily meet these needs.
AnQiCMS's template system borrows the syntax of the Django template engine, which means you can use double curly braces{{变量}}to output variables, and by{% 标签 %}Call the functional tag. It is crucial to understand the core time formatting rules when handling dates and times.
Understand the date and time formatting in Go language.
AnQiCMS time formatting follows the unique conventions of the Go language, it does not use commonY-m-doryyyy-MM-ddthis kind of placeholder, but uses a specific reference time2006-01-02 15:04:05This number refers to a component of the date and time. You just need to replace the format you want to display with the corresponding number.
For example:
- year:
2006 - month:
01(with leading zero) or1(without leading zero) - Day:
02(with leading zero) or2(without leading zero) - hours:
15(24-hour format, with leading zero) or03(12-hour format, with leading zero) - minute:
04(with leading zero) - seconds:
05(with leading zero) - AM/PM:
PM(and03Use together)
Mastering this rule, you can freely combine various date and time formats.
Display the current year in the template.
Display the current year at any location on the website page, for example, in the footer for copyright statements, you can use it directly{% now %}The tag is used with Go language's year formatting characters2006.
© {% now "2006" %} 您的网站名称. All Rights Reserved.
This will automatically display the current year in your website footer, for example, without manual updates© 2023 您的网站名称. All Rights Reserved..
Display the current date and time in a specified format
If you need to display the current full date and time on the page, such as showing the access time at the top of the page or providing time-sensitive information in a specific module,{% now %}Tags can also be used. You just need to provide the complete Go language date and time format string.
For example, to display the current date and time to the second, the format is年-月-日 时:分:秒:
当前时间:{% now "2006-01-02 15:04:05" %}
It will display something similar on the page当前时间:2023-10-26 15:30:00The content.
If you need other common date and time formats, you can combine them like this:
- Only display the date (e.g., 2023-10-26):
发布日期:{% now "2006-01-02" %} - Display the month and date without leading zeros (e.g., 10/26/2023):
活动日期:{% now "1/2/2006" %} - Display time in 12-hour format (e.g., 03:30 PM):
更新时间:{% now "03:04 PM" %}
Format the timestamp in the content
In AnQi CMS, content such as articles and products, their publication time or update time is usually stored in the database in the form of a 10-digit timestamp, for exampleitem.CreatedTimeoritem.UpdatedTime. When you iterate over this content in a template and need to display the time, you need to use{{stampToDate()}}This tag is specifically used to format timestamps.
{{stampToDate()}}The tag requires two parameters:
- Timestamp variableFor example
item.CreatedTime. - Go language format string: with
{% now %}The format rules used for tags are the same.
Assuming you are looping through a list of articles and want to display the publication date and detailed update time for each article:
{% archiveList archives with type="list" limit="10" %}
{% for item in archives %}
<article>
<h2><a href="{{item.Link}}">{{item.Title}}</a></h2>
<p>发布于:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</p>
<p>最后更新:{{stampToDate(item.UpdatedTime, "2006-01-02 15:04:05")}}</p>
<p>{{item.Description}}</p>
</article>
{% endfor %}
{% endarchiveList %}
By this code, the publish date of each article will be displayed in the form2023年10月26日and the update time will be displayed in the precise format2023-10-26 15:30:00like this.
Summary
The Anqi CMS template system passed{% now %}Tags and{{stampToDate()}}The filter, combined with the concise and unique date and time formatting rules of the Go language, provides you with great flexibility, whether you need to display the current time or format timestamps stored in content, it can be easily achieved.Mastering these basic skills will help you build a website with rich information and a better user experience.
Frequently Asked Questions (FAQ)
Q1: Why did my date formatting not work, or did it display strange numbers?A1: This is likely because you did not strictly follow the reference time in string formatting in Go language2006-01-02 15:04:05Make sure that the year, month, and date are all used with the corresponding numbers in the reference time (for example, the year must be2006instead ofYYYYoryy)。Another possible reason is that you are trying to use{{stampToDate()}}The formatted variable itself is not a valid timestamp.
Q2: Can I display the current time while adding some custom text?A2: Of course you can.{% now %}The label will only output the formatted time string, you can embed it in any text content, for example:欢迎访问,今天是:{% now "2006年01月02日" %},祝您心情愉快!
Q3: How to display the year and month of the content's publication on an article or product detail page?A3: On an article or product detail page, you can usearchive.CreatedTimeGet the timestamp of the content published. Then combine{{stampToDate()}}Tags and Go language formatting strings to display, for example:{{stampToDate(archive.CreatedTime, "2006年01月")}}It can be displayed as2023年10月The content.