In website operation, we often need to display some dynamic time information, such as the copyright year at the bottom of the website (such as “© 2023-Current Year”), the publication or update time of articles, or the deadline for specific activities, etc.These dynamic dates not only maintain the timeliness of the website information, but also improve the user experience, and in some cases, it also has a positive effect on SEO.
AnQi CMS provides a series of flexible template tags that allow you to easily display the current year or a specified date dynamically at any location on the website.We will delve into how to make use of these features in detail.
1. Dynamically display the current year of the website
The copyright information at the footer of the website usually needs to be automatically updated every year. If modified manually, it is麻烦 and easy to miss. Anqi CMS provides a very convenient tag for this:{% now "2006" %}.
This tag can directly obtain and display the current year of the website. It includes the"2006"It is not a specific year, but a special usage of date formatting in Go language, which represents the reference time2006年01月02日15时04分05秒The part of the year. Just place it in the template where the year needs to be displayed.
Example: Display the current year in the footer.
Assume you want to display copyright information like “© 2023 [Your company name] All rights reserved” in the footer of your website, and the year is automatically updated, you can do this in the template file (usuallyfooter.htmlOr include the footer code in the common template) as follows:
<p>© {% now "2006" %} {{ system.SiteName }} 版权所有。</p>
When the year 2024 comes, this label will automatically update to display “© 2024 [Your Company Name] All Rights Reserved”, saving you the trouble of manual modification.
Format the display of a specified date or timestamp
In addition to the current year, we also need to display the publication time, update time of the articles on the website, as well as the dates of specific events, etc. Anqi CMS provides{{ stampToDate(时间戳, "格式") }}This powerful tag to meet these needs.
Here时间戳It usually refers to a Unix timestamp (a number representing the number of seconds from 1970-01-01 00:00:00 to a specific time). The article publishing time in AnQi CMS (CreatedTime)、Update Time(UpdatedTime)are stored in the form of timestamps.格式Part determines the way dates are displayed.
Similarly,格式The parameter also follows the date formatting rules of the Go language, with2006-01-02 15:04:05This reference time is used as a template for matching. This means that if you want to display:
- Year, use
2006 - Month, use
01 - Date, use
02 - Hour (24-hour format), use
15 - minutes, use
04 - seconds, use
05
You can combine these elements as needed and add separators (such as year, month, day, slash, colon, etc.).
Example: Display the publication and update time of the article
In the article detail page (for examplearchive/detail.htmlor the corresponding detail template of the model), you can usearchiveDetailtags to get the article details, and then combinestampToDateto format the displayCreatedTimeandUpdatedTime:
{% archiveDetail archive with name="Title" %}
<h1>{{ archive.Title }}</h1>
<p>
发布时间:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}
{% if archive.UpdatedTime != archive.CreatedTime %} {# 如果更新时间与创建时间不同才显示 #}
<span>(更新于:{{ stampToDate(archive.UpdatedTime, "2006-01-02") }})</span>
{% endif %}
</p>
<div>
{{ archive.Content|safe }}
</div>
In this example:
"2006年01月02日 15:04"It will format the timestamp to the form 'October 26, 2023 10:30 AM'."2006-01-02"The timestamp will be formatted into a pure date form like '2023-10-26'.
Example: Display dates in the article list
If you want to display the article list page (for examplearchive/list.htmlOr the list template corresponding to the model) usually displays the publish date of each articlearchiveListLoop with tags:
{% archiveList archives with type="page" 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>{{ item.Description }}</p>
<a href="{{ item.Link }}">阅读更多</a>
</article>
{% endfor %}
{% endarchiveList %}
here,item.CreatedTimeIs the publish timestamp of each article in the loop, throughstampToDateLabel, we format it as年-月-日in the form of.
Chapter 3: Some application scenarios and tips
- Multilingual date display:If your website supports multilingual, you can consider adjusting the language templates according to the habits of the target language.
stampToDatelabel's格式Parameters, to adapt to the reading habits of users in different countries and regions. - Combine custom field:If you add a custom date field (such as "Event Start Date" or "Product Expiry Date") to an article or product model, as long as these fields store timestamps, they can also be used.
stampToDateLabel formatting is displayed. For example, if you have a custom field namedevent_start_timeyou can call it like this in the template:{{ stampToDate(archive.event_start_time, "2006年1月2日") }}. - Dynamic year range:If you want to display a year range like '2020-current year', you can use
CreatedTimeand the years of{% now "2006" %}:<p>© {{ stampToDate(archive.CreatedTime, "2006") }}-{% now "2006" %} 版权所有。</p>
The template tags of AnQi CMS provide great flexibility, making the management of website time information efficient and simple.Master the usage of these tags, and you will be able to better control the presentation of website content, providing visitors with more accurate and timely information.
Frequently Asked Questions (FAQ)
1. Why does my date formatting come out differently than expected, instead showing a number combination like '2006-01-02'?
This is because Go language (the development language of AnQi CMS) uses a fixed 'reference time' as a template for date formatting, rather than like some other languagesY-m-dor%Y-%m-%dAn equal sign. This reference time is2006年01月02日15时04分05秒. You must use the corresponding number in this reference time to represent the year, month, day, hour, minute, and second you want. For example, if you want to display the year, you should use"2006"If you want to display the month, use"01"Just build the format string according to the numbers in the reference time to get the expected result.
How to display a year range similar to '2020-current year'?
You can usestampToDateGet the starting year of the tag and combine it with{% now "2006" %}Label to get the current year. For example, if the content is published in 2020 and you want to display '2020-Current Year':
<p>版权信息:© {{ stampToDate(archive.CreatedTime, "2006") }}-{% now "2006" %}</p>
This will be extracted firstarchive.CreatedTimeThe year (e.g., 2020), then connect a hyphen, and finally display the current year (e.g., 2024).
3. Can I dynamically display the date in other locations on the website besides the footer?
Of course you can.The AnqiCMS template tags can be used at any position in the website template.
- Website Header:For example, display the latest dynamic release date of the website.
- Sidebar:Display the time of the latest comments or the date of an upcoming event.
- Custom content block:Add an independent date information module on the homepage or other pages. *