In website content operation, the time of content release and update is a key factor in conveying the timeliness of information and establishing user trust.For search engine optimization (SEO), clear time information also helps improve the freshness rating of content.AnQi CMS is proficient in this field, providing flexible and diverse tags and methods for users, helping you accurately and beautifully display these time information on the front end of the website.

Next, we will explore how Safe CMS helps us manage and display the content publishing and update time.

Core time tags for content publishing and updating

Auto CMS in content management, for each document (whether it is an article, product, or other custom model content), has built-in two important time attributes: the content'spublication timeandUpdate time.

  1. 发布时间(English)CreatedTime)Every time we publish a document on the Anqi CMS backend, the system will automatically record the exact time when this operation occurs.This is the initial creation time, which is the time of content release, representing the moment when the content first appears.CreatedTime这一字段来获取它。

    例如,在文档详情页或文档列表页,您可以通过以下方式调用发布时间:

    {# 在文档详情页直接获取当前文档的发布时间 #}
    <div>发布日期:{{ archive.CreatedTime }}</div>
    
    
    {# 在文档列表循环中获取每篇文章的发布时间 #}
    {% archiveList archives with type="list" limit="10" %}
        {% for item in archives %}
            <div>文章标题:{{ item.Title }},发布于:{{ item.CreatedTime }}</div>
        {% endfor %}
    {% endarchiveList %}
    

    您会注意到,直接输出{{ item.CreatedTime }}or{{ archive.CreatedTime }}It may receive a series of numbers, which is actually a Unix timestamp and it needs to be further formatted to become the date and time we commonly read.

  2. Update time (UpdatedTime)With the continuous maintenance and improvement of the website content, we often need to modify and update the published content. The Anqi CMS also records every important modification of the content and providesUpdatedTimeLabel to display the latest update time of the content. This is very important for users to understand the timeliness of the content, as well as for search engines to judge whether the content is active and fresh.

    The method of calling the update time is similar to the release time:

    {# 在文档详情页获取当前文档的更新时间 #}
    <div>最后更新:{{ archive.UpdatedTime }}</div>
    
    
    {# 在文档列表循环中获取每篇文章的更新时间 #}
    {% archiveList archives with type="list" limit="10" %}
        {% for item in archives %}
            <div>文章标题:{{ item.Title }},更新于:{{ item.UpdatedTime }}</div>
        {% endfor %}
    {% endarchiveList %}
    

    Similarly,UpdatedTimeThe default output is also a timestamp, which requires us to perform formatting.

A flexible time formatting tool:stampToDatetags

Due toCreatedTimeandUpdatedTimePresented in the form of timestamps by default, to make these time information more readable, Anqi CMS provides a powerful template functionstampToDateThis tag can convert timestamps into various custom date-time formats.

stampToDateThe usage is very intuitive:{{stampToDate(时间戳, "格式")}}

Here, 'timestamp' refers to the one we use throughCreatedTimeorUpdatedTimeThe number obtained, the "format" part follows the time formatting standard of the Go language. Understanding the time formatting of Go language may seem a bit strange at first, but it is very flexible, through a specific reference time2006-01-02 15:04:05Define the output format. Just replace the year, month, day, hour, minute, and second in this reference time with the display style you want.

Some commonly used formatting examples:

  • Display year, month, and day: {{stampToDate(item.CreatedTime, "2006-01-02")}}it will be displayed as2024-03-15
  • Display Chinese year, month, and day: {{stampToDate(item.CreatedTime, "2006年01月02日")}}it will be displayed as2024年03月15日
  • Display the full date and time: {{stampToDate(item.UpdatedTime, "2006-01-02 15:04:05")}}it will be displayed as2024-03-15 10:30:45
  • Display concise month and day: {{stampToDate(item.CreatedTime, "01-02")}}it will be displayed as03-15
  • Display hour and minute: {{stampToDate(item.UpdatedTime, "15:04")}}it will be displayed as10:30

CombinestampToDateTags, we can display the publish and update time like this:

{# 在文档详情页显示格式化后的发布和更新时间 #}
<div>发布日期:{{stampToDate(archive.CreatedTime, "2006年01月02日")}}</div>
<div>最后更新:{{stampToDate(archive.UpdatedTime, "2006-01-02 15:04")}}</div>

{# 在文档列表页显示格式化后的发布时间 #}
{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
        <div>
            <h3>{{ item.Title }}</h3>
            <p>发布于:{{stampToDate(item.CreatedTime, "2006-01-02")}}</p>
            <p>更新于:{{stampToDate(item.UpdatedTime, "2006-01-02")}}</p>
        </div>
    {% endfor %}
{% endarchiveList %}

Other time-related auxiliary tags and filters

In addition to the above main tags, Anqi CMS also provides some other time-related auxiliary functions:

  • Display the current server time:{% now "格式" %}If your page needs to display the current server time, for example, displaying “Copyright © 2024” in the footer, you can use{% now "2006" %}to get the current year.

    <div>版权所有 © {% now "2006" %} 安企CMS</div>
    
  • dateFilterEnglish CMS also providesdatea filter whose function is similar tostampToDateIt can format time. But it is worth noting that,datethe expected input of the filter is a Go languagetime.TimeThe variable is of type, not a timestamp. In actual use, due toCreatedTimeandUpdatedTimeall being timestamps, sostampToDateit is usually more convenient to handle these fields.

Value in actual application

Reasonably display the publishing and updating time of content, not only a reflection of information transparency, but also a double enhancement of website SEO and user experience.Users can intuitively judge the "freshness" of the content, especially in scenarios that require high timeliness, such as news, technical tutorials, or product reviews.At the same time, search engines will also consider the update frequency of the content and the latest release time as important ranking reference factors, which helps the website content achieve better visibility and click-through rate in search results.

In summary, Anqi CMS provides meticulous support in displaying time information, from basic time data acquisition to flexible formatting tools, all of which help us better manage and present the timeliness of website content.


Common Questions (FAQ)

1. Why do I output directly in the template{{archive.CreatedTime}}Is the result a sequence of numbers, not a date format?The string of numbers you see is a Unix timestamp, which represents the number of seconds from 00:00:00 on January 1, 1970 (UTC) to the present. To convert it to a readable date and time format, you need to use the AnQi CMS provided.stampToDateTemplate functions are used for formatting, for example{{stampToDate(archive.CreatedTime, "2006-01-02 15:04:05")}}.

2. I want to display the publish time on both the article list and detail pages, do I need to write two different tags?No need. Anqi CMS has designed a unified tag calling method. Whether it isarchiveListIn the loopitema variable, orarchiveDetailofarchivea variable, they all containCreatedTimeandUpdatedTimefields. You can use the samestampToDateFunctions and formatting parameters are used to display them, ensuring the conciseness and consistency of template code.

3. If I only want to display the release date and not the specific time, how should I set it up?stampToDateThe format?If you only want to display the date, you can omit the hour, minute, and second parts of the Go language time format reference. For example, using"2006-01-02"It can only display the year, month, and date, for example2024-03-15. If you need to display it in Chinese format, you can use"2006年01月02日".