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. This may be used for footer copyright statements, the publication and update time of articles, or time points for various events.AutoCMS (AutoCMS) provides flexible and powerful template tags, allowing you to easily meet these needs.
AnQiCMS's template system adopts the syntax of Django template engine, which means you can use double curly braces{{变量}}to output variables, and by{% 标签 %}Invoke functional tags. It is crucial to understand the core time formatting rules when dealing with dates and times.
Understand the date and time formatting in the Go language.
AnQiCMS 的时间格式化遵循Go语言的独特约定,它不使用常见的 English,而是使用一个特定的参照时间Y-m-doryyyy-MM-dd这类占位符,而是使用一个特定的参照时间 English2006-01-02 15:04:05This number represents a component of the date and time in this reference time. You just need to replace the format you want to display with the corresponding number.
For example:
- Year:
2006 - Month:
01(with leading zeros) or1(without leading zeros) - Day:
02(with leading zeros) or2(without leading zeros) - Hour:
15(24-hour format, with leading zero) Or03(12-hour format, with leading zero) - minutes:
04(with leading zero) - seconds:
05(with leading zero) - AM/PM:
PM(With03to be used together)
Grasped this rule, you can freely combine various date and time formats.
Display the current year in the template
To display the current year at any location on the website page, for example in the footer for copyright statements, you can directly use{% now %}tags along with the year formatting characters in the Go language2006.
© {% now "2006" %} 您的网站名称. All Rights Reserved.
So, your website footer will automatically display the current year, without manual updates, for example© 2023 您的网站名称. All Rights Reserved..
Display the current date and time in the 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 specific modules,{% now %}Labels can also handle it. You just need to provide the full Go language date-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" %}
The page will display something similar当前时间: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 (for example: 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 timestamps in the content
In the 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.UpdatedTimeWhen you iterate over these contents in a template and need to display their time, you need to use{{stampToDate()}}this tag specifically used for formatting timestamps.
{{stampToDate()}}The tag requires two parameters:
- Timestamp variable: For example
item.CreatedTime. - Go language format string: with
{% now %}The format rules used for labels are the same.
Assuming you are iterating through an article list and want to display the publication date and detailed update time of 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 %}
Through this code, the publication date of each article will be displayed in2023年10月26日format, while the update time will be displayed in2023-10-26 15:30:00this precise format.
Summary
The template system of AnQi CMS provides{% now %}Tags and{{stampToDate()}}Filter, along 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.Master these basic skills, and it will help you build a website with rich information and better user experience.
Common Questions (FAQ)
Q1: Why does my date formatting not work, or show strange numbers?A1: This is very likely because you did not strictly follow the reference time in string formatting in the Go language2006-01-02 15:04:05Please ensure that the format string uses the corresponding numbers in the reference time (for example, the year must be2006InsteadYYYYoryy)。Another possible reason is that, you tried 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 %}Labels will output formatted time strings, which you can embed 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 canarchive.CreatedTimeObtained the timestamp of the content. Then combine{{stampToDate()}}Tags and Go language formatting strings to display, such as:{{stampToDate(archive.CreatedTime, "2006年01月")}}It can be displayed as2023年10月The content.