As an experienced website operations expert, I am well aware that the accuracy and readability of time in content display are crucial for user experience and Search Engine Optimization (SEO).AnQiCMS as an efficient and flexible content management system provides a very convenient way to control the display format of the article's publication and update time.This article will focus on how AnQiCMS formats and displays the publication and update time of documents, and will explain the secrets and practical skills in detail.


AnQi CMS: Flexibly control the sequence of articles, easily format the publication and update time

In today's increasingly rich digital content, the time of article publication and update is not only an important indicator of the life cycle of the content, but also a key element in conveying the timeliness and professionalism of the information to readers.A clear and standardized time display that can effectively improve user experience and help search engines better understand and index your content.AnQi CMS is well-versed in this, providing powerful and flexible template tags that allow us to easily format the publication and update times of documents.

Understand the time storage mechanism of AnQi CMS

Before delving into formatting methods, we first need to understand how AnQiCMS stores time. To ensure data processing efficiency and cross-platform compatibility, AnQiCMS usually stores the publication time of articles (CreatedTimeAnd update time(UpdatedTimeStored as(Unix timestamp.This is the number of seconds from 00:00:00 UTC on January 1, 1970 to the current time, existing in pure digital form, which is friendly to computers but difficult for humans to understand intuitively.Therefore, we need a conversion tool to convert these timestamps into the date and time format we are familiar with.

Core tool:stampToDateFormatted timestamp label

AnQiCMS provides a namedstampToDateThe template tag that is the key to solving the timestamp formatting problem.This tag can receive a Unix timestamp and convert it into a readable date and time string according to the format you specify.

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

Here is the "timestamp" you need to convertCreatedTimeorUpdatedTimeVariable, and the "format" is the key to defining the final display effect

Mastering the Golang time format

stampToDateThe "format" parameter follows the unique time formatting rules of Golang (Go language). Unlike PHP or Java, it uses letter symbols such asY/m/dThe year, month, and day are represented differently, Golang uses oneFixed reference timeAs the formatting template, namely:

2006-01-02 15:04:05

This seemingly ordinary date and time string has a special meaning in Go:

  • 2006Represents the year (four digits)
  • 01Represents the month (two digits, with leading zeros if necessary)
  • 02Represent the date (two digits, leading zero if necessary)
  • 15Represent the hour (24-hour format, two digits, leading zero if necessary)
  • 04Represent the minutes (two digits, leading zero if necessary)
  • 05Represents seconds (two digits, leading zeros if necessary)

In addition, the Go language supports other formatting elements, such as:

  • MonRepresents the abbreviation of the day of the week (e.g., Mon, Tue)
  • MondayRepresent the full name of the week (e.g., Monday, Tuesday)
  • JanRepresent the abbreviation of the month (e.g., Jan, Feb)
  • JanuaryRepresent the full name of the month (such as: January, February)
  • PMorpmRepresent AM/PM identifier
  • MSTor-0700Represent time zone information

Understood this reference time, you can then combine any date and time format you want, like stacking blocks.

Some common formatting examples:

The format you want to display Go language format string Sample output
Complete date (Year-Month-Day) "2006-01-02" 2023-10-26
Complete date (Year/Month/Day) "2006/01/02" 2023/10/26
Chinese date "2006年01月02日" 2023年10月26日
Complete Date and Time (24-hour format) "2006-01-02 15:04:05" 2023-10-26 10:30:45
Complete Date and Time (12-hour format) "2006-01-02 03:04:05 PM" 2023-10-26 10:30:45 AM
Month and Date "01-02" 10-26
Day of the Week and Date "Mon, 02 Jan" Thu, 26 Oct

Formatted Time on Document Detail Page

In the document detail page (usually correspondingarchiveDetailto the data obtained from the tag), you can directly access the current document'sCreatedTimeandUpdatedTimefield.

Assuming your document variable is namedarchive:

{# 格式化显示文档发布时间 #}
<p>发布时间:{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04")}}</p>

{# 格式化显示文档更新时间 #}
<p>最后更新:{{stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05")}}</p>

{# 如果只显示日期,不显示时间 #}
<p>发表于:{{stampToDate(archive.CreatedTime, "2006-01-02")}}</p>

format the time in the document list page

When you use on the article list pagearchiveListWhen tags cycle through multiple articles, you can use loop variables (for exampleitem) to access the time field of each article.

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
        <article>
            <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
            <div class="meta">
                <span>发布:{{stampToDate(item.CreatedTime, "2006/01/02")}}</span>
                {% if item.CreatedTime != item.UpdatedTime %} {# 判断发布时间与更新时间是否一致,不一致才显示更新时间 #}
                    <span>更新:{{stampToDate(item.UpdatedTime, "2006/01/02 15:04")}}</span>
                {% endif %}
            </div>
            <p>{{item.Description}}</p>
        </article>
    {% endfor %}
{% endarchiveList %}

In this way, you can easily display the publication and update time of each article in the article list and choose different formats as needed.

Why is it important to display time accurately?

  1. Enhancing user experience: Readers can quickly tell the age of the article, which is crucial for news, technical articles, or any time-sensitive content.
  2. Enhance content trustworthiness: Clear update time indicates that the content provider is maintaining and updating information, which increases the reliability of the content.
  3. Optimize search engine rankings: Search engines consider the publication and update time when evaluating the relevance and freshness of web pages.Reasonably displaying this information helps search engines better understand your content, thereby potentially achieving better search rankings.

Summary

Anqi CMS passedstampToDateThe tag combines the time formatting rules of the Go language, providing website operators with powerful and flexible time display control capabilities.Whether it is the precise publication time down to the second or a concise update date, you can easily achieve it according to your design and operational needs.Master these skills and make your website content more timely, professional, and ultimately transform it into a better user experience and superior SEO performance.


Frequently Asked Questions (FAQ)

  1. Ask: The AnQiCMS document'sCreatedTimeandUpdatedTimeCan it be stored in a format other than Unix timestamp? Answer:Not allowed. AnQiCMS internally uses Unix timestamps to storeCreatedTimeandUpdatedTimeThis is for the efficiency of data processing and the uniformity of the system's internal storage. You don't need to care about its internal storage format, just throughstampToDateLabel it in the front-end template to display it in any format you need.

  2. **Question