In website content operation, the timing of content publication 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 the content.AnQi CMS is well-versed in this, providing users with flexible and diverse tags and methods to accurately and beautifully display these time information on the website front-end.

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

The core time tags for content publication and update.

AnQi CMS in content management provides two important time attributes for each document (whether it is an article, product, or other custom model content): the content'sRelease TimeandUpdate time.

  1. Publish time (CreatedTime)Every time we publish a document on the Anqie CMS backend, the system will automatically record the exact time the operation occurred.This is the initial creation time, which is the time of content release, representing the moment when the content first appeared.In the template, we can go throughCreatedTimeThis field is used to get it.

    For example, on the document detail page or document list page, you can call the publication time in the following way:

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

    You will notice that it outputs directly.{{ item.CreatedTime }}or{{ archive.CreatedTime }}You might get a string of numbers, which is actually a Unix timestamp that needs to be further formatted to become the date and time we read in everyday life.

  2. Update time (UpdatedTime)With the continuous maintenance and improvement of the website content, we often need to modify and update the published content. AnQi CMS also records each important modification of the content and providesUpdatedTimeThe label shows 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 needs to be formatted.

A flexible time formatting tool:stampToDateTag

due toCreatedTimeandUpdatedTimePresented in the form of timestamps by default, to make this time information more readable, AnQi CMS provides a powerful template functionstampToDate. This tag can convert timestamps into various custom date and time formats.

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

Here, the 'timestamp' is what we get throughCreatedTimeorUpdatedTimeThe number obtained, the "format" part follows the Go language time formatting standard. Understanding the Go language time formatting may sound foreign 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:

  • Show Date and Month {{stampToDate(item.CreatedTime, "2006-01-02")}}It will be displayed as2024-03-15
  • Show Chinese Date and Month {{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
  • Show Brief Date and Month {{stampToDate(item.CreatedTime, "01-02")}}It will be displayed as03-15
  • Show 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 auxiliary functions related to time:

  • 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>
    
  • dateFilterSecurity CMS also providesdatea filter whose function is similar tostampToDateIt can format time. However, it is worth noting thatdateThe filter expects input to be in Go languagetime.TimeType variables, not timestamps. In practice, due toCreatedTimeandUpdatedTimeall being timestamps, sostampToDateit is usually more convenient to handle these fields.

Value in practical application

Reasonably display the publication and update time of content, not just a reflection of information transparency, but also a double enhancement for website SEO and user experience.Users can intuitively judge the freshness of 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 and the latest release time of the content 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 detailed support for displaying time information, from basic time data acquisition to flexible formatting tools, all of which help us better manage and present the timeliness of web content.


Frequently Asked Questions (FAQ)

1. Why do I output directly in the template{{archive.CreatedTime}}Is it a string of numbers, not a date format?The number you see is a Unix timestamp, which represents the number of seconds from January 1, 1970, 0:0:0 (UTC) to the present. To convert it to a readable date and time format, you need to use the Anqi CMS providedstampToDateThe template function is 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 the detail page, do I need to write two different tags?Do not need. Anqi CMS has designed a unified tag call method. Whether it isarchiveListin the loopitemvariables, orarchiveDetailofarchivevariables, 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 publish date and not the specific time, how should I set it?stampToDateWhat is the 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, use"2006-01-02"You 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日".