How to display the publication time of articles in Anqi CMS and customize the date and time format?

Calendar 👁️ 83

When operating website content, the publication time of the article is an indispensable element.It not only helps visitors quickly understand the "freshness" of the content, but also has a positive impact on search engine optimization (SEO), as it conveys the signal that the website content is continuously updated.AnQiCMS (AnQiCMS) offers great flexibility in content management, whether it is to obtain or customize the display time of the published articles, it is very convenient.

Next, we will explore how to flexibly display the publication time of articles in Anqi CMS and customize the date and time display format according to our needs.

Get the publication time of the article

In Anqi CMS, each article is accompanied by two important timestamps:CreatedTime(creation time) andUpdatedTime(Update time). These two fields store the creation or last modification time of the article in the system, they exist in the form of timestamps, which means they are a series of numbers, for example1609470335.

We need to use the template tags provided by Anqí CMS in the template file (usually) to display these times on the article detail page or in the article list.archive/detail.htmlorarchive/list.html)

When we want to display the publish time on an article detail page, we can usearchiveDetailtags to get the current article'sCreatedTime:

<div>发布时间:{{archive.CreatedTime}}</div>

Or use:archiveDetailnamed tag style:

<div>发布时间:{% archiveDetail with name="CreatedTime" %}</div>

If you are on the article list page (for example, usingarchiveListTags loop to display articles) It is also very direct to get the publication time of each article:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
        <div>文章标题:{{item.Title}}</div>
        <div>发布时间:{{item.CreatedTime}}</div>
    {% endfor %}
{% endarchiveList %}

You will find that it outputs directly like thisCreatedTime(orUpdatedTimeYou will get a string of numbers, not the familiar date and time format.This is because they are UNIX timestamps and need to be further formatted to be presented in a human-readable way.

Custom date and time format

To convert timestamps into a readable date and time format, Anqi CMS provides a very practical template tag:stampToDateThis tag allows us to format timestamps according to the specific format rules of the Go language into any date-time string we want.

stampToDateThe usage method of the tag is{{stampToDate(时间戳, "格式")}}. The "format" is a string, it is not what we usually understand.YYYY-MM-DDSuch placeholders, but Go language has a unique 'reference time' format. Go language uses a fixed time2006-01-02 15:04:05As a formatting reference template. This means:

  • 2006Represents the year
  • 01Represents the month
  • 02Represents the date
  • 15representing hours (24-hour system)
  • 04representing minutes
  • 05representing seconds

Understood the reference rules, we can flexibly combine various date and time formats.

Here are some common formatting examples:

  • Only display the date (year-month-date): If you only want to display the publication date of the article, you can use2006-01-02as the format string.

    <div>发布日期:{{stampToDate(archive.CreatedTime, "2006-01-02")}}</div>
    <!-- 输出示例:2023-10-26 -->
    
  • Display the date and time (accurate to minutes)If you need to be accurate to minutes, you can add the hour and minute reference value after the date.

    <div>发布时间:{{stampToDate(archive.CreatedTime, "2006-01-02 15:04")}}</div>
    <!-- 输出示例:2023-10-26 10:30 -->
    
  • Show full date and time (to the second)The complete format includes year, month, day, hour, minute, and second.

    <div>完整时间:{{stampToDate(archive.CreatedTime, "2006-01-02 15:04:05")}}</div>
    <!-- 输出示例:2023-10-26 10:30:45 -->
    
  • Chinese date displayYou can even combine Chinese to display dates, such as "October 26, 2023".

    <div>中文日期:{{stampToDate(archive.CreatedTime, "2006年01月02日")}}</div>
    <!-- 输出示例:2023年10月26日 -->
    
  • Other custom combinationsFor example, only display the month and date, or only display the hour and minute.

    <div>月日:{{stampToDate(archive.CreatedTime, "01-02")}}</div>
    <!-- 输出示例:10-26 -->
    <div>时分:{{stampToDate(archive.CreatedTime, "15:04")}}</div>
    <!-- 输出示例:10:30 -->
    

Comprehensive Application Example

Now, we will apply this knowledge to actual template code, taking the article detail page and article list page as examples.

Article detail page (archive/detail.html) Example:

<article>
    <h1>{% archiveDetail with name="Title" %}</h1>
    <div class="article-meta">
        <span>分类:<a href="{% categoryDetail with name='Link' %}">{% categoryDetail with name='Title' %}</a></span>
        <span>发布于:{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04")}}</span>
        <span>阅读量:{% archiveDetail with name="Views" %}</span>
        <!-- 如果文章有更新时间,可以这样显示 -->
        {% if archive.UpdatedTime and archive.UpdatedTime != archive.CreatedTime %}
        <span>更新于:{{stampToDate(archive.UpdatedTime, "2006年01月02日 15:04")}}</span>
        {% endif %}
    </div>
    <div class="article-content">
        {%- archiveDetail articleContent with name="Content" %}
        {{articleContent|safe}}
    </div>
</article>

Article list page (archive/list.html) Example:

<div class="article-list">
    {% archiveList archives with type="page" limit="10" %}
        {% for item in archives %}
        <div class="article-item">
            <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
            <div class="item-meta">
                <span>分类:{% categoryDetail with name="Title" id=item.CategoryId %}</span>
                <span>发布于:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
                <span>浏览量:{{item.Views}}</span>
            </div>
            <p>{{item.Description}}</p>
            <a href="{{item.Link}}" class="read-more">阅读全文 &gt;&gt;</a>
        </div>
        {% empty %}
        <p>暂时没有文章发布。</p>
        {% endfor %}
    {% endarchiveList %}

    <div class="pagination-area">
        {% pagination pages with show="5" %}
            <!-- 分页链接,具体样式可根据需求调整 -->
            {% if pages.PrevPage %}<a href="{{pages.PrevPage.Link}}">上一页</a>{% endif %}
            {% for item in pages.Pages %}<a class="{% if item.IsCurrent %}active{% endif %}" href="{{item.Link}}">{{item.Name}}</a>{% endfor %}
            {% if pages.NextPage %}<a href="{{pages.NextPage.Link}}">下一页</a>{% endif %}
        {% endpagination %}
    </div>
</div>

By using the above method, you can easily display the publication time of the article in the various locations of the Anqi CMS website in the format you wish.This flexibility makes the presentation of content more professional and in line with user habits.


Frequently Asked Questions (FAQ)

1. Why output directly{{archive.CreatedTime}}Is it a string of numbers, not a date format?This is becauseCreatedTime(andUpdatedTimeat

Related articles

How to display the cover image (Logo) and thumbnail (Thumb) of an AnQi CMS article?

In content operation, the cover image (Logo) and thumbnail (Thumb) of the article play a crucial role. They not only attract readers' attention and increase click-through rates, but are also an indispensable part of search engine optimization (SEO).AnQiCMS (AnQiCMS) provides users with a flexible and convenient way to manage and display these visual elements.This article will discuss in detail how to configure and call the cover image and thumbnail of the article in AnQiCMS.## One, Configuration and generation of cover image and thumbnail In AnQiCMS

2025-11-09

How to generate the catalog navigation on the front page for the automatic extraction of article content (`ContentTitles`) feature?

In Anqi CMS, providing a clear directory navigation to help readers better understand the structure of long articles is the key to improving user experience.The Anqi CMS article content automatically extracts the title (ContentTitles) feature, which is for this purpose.It can intelligently identify the various titles in the article content and provide this structured information to the front-end template, making it easy for us to build a dynamic and practical directory navigation.###

2025-11-09

How to implement lazy loading of images in the `archiveDetail` tag of AnQi CMS?

In today's content is king era, the importance of website performance and user experience is self-evident.Especially for content pages with many images, how to avoid affecting user experience and search engine rankings due to slow image loading has become a common concern for website operators.AnQi CMS is well-versed in this, not only providing flexible and efficient content management functions, but also cleverly supporting image lazy loading (Lazy Load).

2025-11-09

How to display and truncate the Description field of AnQi CMS on the front page?

In the daily operation of AnQi CMS, the Description field plays a crucial role, not only affecting how search engines understand the page content, but also being the key copy that attracts users to click.Understand how this field displays and flexibly truncates on the front-end page, which can help us better perform content layout and SEO optimization. ### Description of field in various aspects and functions In AnQi CMS, the "description" field is not a single concept. It will show different forms and functions according to the content type and usage scenario. The most common and has the greatest impact on SEO

2025-11-09

How to display the category information of the articles on the front page of AnQi CMS, including the category name and link?

In Anqi CMS, the display of article category information on the front page is a key link in content organization and user navigation.Whether it is to help users understand the context of the article or to optimize the website's SEO structure, accurate and accessible category names and links play an important role.AnQi CMS with its flexible template engine makes this task intuitive and efficient.Understanding Anqi CMS's Classification System Before delving into the display methods, we first need to know that Anqi CMS's content management system attaches great importance to the concept of 'classification'.Each document (whether it is an article

2025-11-09

How to use the `tagList` tag to display the associated tag list on the article detail page?

In content operation, tags (Tags) are a very practical tool that can help us organize content more flexibly, making it convenient for readers to quickly find related topics of interest.Imagine that after a user reads an excellent article, if they can immediately see other popular topics or keywords related to that article, it will undoubtedly greatly enhance their browsing experience and encourage them to explore other content on the website.In Anqi CMS, implementing this feature is very intuitive and simple, we mainly rely on the powerful `tagList` tag to complete it.

2025-11-09

How to display custom fields (such as author, source) of AnQi CMS documents on the front-end page?

When managing content in Anqi CMS, we often encounter the need to add more personalized data to articles, products, or pages, such as the author of the article, the source of content, or specific parameters of products, or specific attributes of event pages, etc.This additional customization information is called "custom field".The powerful content model function of AnQi CMS makes it very convenient to create and manage custom fields, and more importantly, how to accurately and beautifully present these meticulously entered custom fields on the front page of the website

2025-11-09

How to build and display a multi-level product category navigation in AnQi CMS?

AnQi CMS is a powerful content management system that provides users with the ability to flexibly build and display multi-level product classification navigation.This not only helps website visitors easily find the products they need, but also effectively improves the website's SEO performance and optimizes the user experience.Let's explore together how to achieve this goal in Anqi CMS. ### Core Basics: Understanding the Classification System of AnQi CMS In AnQi CMS, the construction of multi-level product classification navigation is inseparable from its core "content model" and "document classification" functions. First

2025-11-09