In website operations, a clear and unified time display format is crucial for improving user experience and content professionalism.Especially the publication time of the article, a standardized "year-month-date" format makes it easy for visitors to understand.In AnQiCMS (AnQi CMS), achieving this is actually very simple and flexible.

Understand AnQiCMS's time handling method

When using AnQiCMS, you will find that articles (archive) and products and other content all have two key time fields:CreatedTime(Creation time/Publish time) andUpdatedTime(Update time). In the template, you can directly call these fields, for example{{archive.CreatedTime}}or{{item.CreatedTime}}They are usually presented in the form of Unix timestamps, such as a string of numbers1678886400. Such numbers are not intuitive for visitors and lack beauty.

AnQiCMS knows this need and therefore provides special template tags and filters to help us easily convert these timestamps into various readable date formats.

The core of time formatting:stampToDatewith the tag andformatParameter

The core of AnQiCMS handling time formatting lies in its powerfulstampToDatetags, as well as those that can be used directly in certain specific tagsformatParameter.

The time formatting rules of Go language

Before delving into how to use it, we first need to understand the Go language time formatting rules used by AnQiCMS. Unlike many other systems that useYYYY-MM-DDThis placeholder is different, Go language uses a fixed reference time to define various date and time formats, the reference time is:

2006年01月02日 15时04分05秒

You just need to build the format string you want according to the corresponding part of this reference time. For example, to achieve the format 'Year-Month-Day', we should write it as'2006年01月02日'.

1. UsearchiveDetailTag retrieval and formatting of the article detail page time

When you display the publish time of a single article on the article detail page (for exampledetail.html), you can directly in thearchiveDetailUsed in tagsformatParameters are used to specify the output format. This is the most convenient way:

<div class="article-meta">
    <span>发布日期:</span>
    {# 直接在 archiveDetail 标签中通过 format 参数指定“年-月-日”格式 #}
    <span>{% archiveDetail with name="CreatedTime" format="2006年01月02日" %}</span>
</div>

In this way, the publication time of the article will be displayed directly in the format "March 15, 2023", without additional conversion steps. Similarly,UpdatedTimeThe field can also use the sameformatParameter for formatting

2. InarchiveListFormatting the article list time in the loop

In the article list page (for examplelist.htmlor the homepageindex.htmlYou would usually go througharchiveListTag loops to display multiple articles. In this case,item.CreatedTimeIt returns an original timestamp, which is when it needs to be usedstampToDateTags as filters to process:

{% archiveList articles with type="page" limit="10" %}
    {% for item in articles %}
    <article class="article-item">
        <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
        <div class="article-info">
            <span>分类:{% categoryDetail with name="Title" id=item.CategoryId %}</span>
            {# 使用 stampToDate 标签格式化循环中的 item.CreatedTime #}
            <span>发布时间:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</span>
            <span>阅读量:{{item.Views}}</span>
        </div>
        <p>{{item.Description}}</p>
        <a href="{{item.Link}}" class="read-more">阅读更多</a>
    </article>
    {% else %}
    <p>暂时没有文章可供显示。</p>
    {% endfor %}
{% endarchiveList %}

Here, stampToDateAccept two parameters: the first is the original timestamp (item.CreatedTime), and the second is the format string you want ("2006年01月02日")

More common time format examples

In addition to the "Year-Month-Day" format, you can also combine various common date and time formats as needed:

  • Year/Month/Date:"2006/01/02"

    {{stampToDate(item.CreatedTime, "2006/01/02")}}
    

    Display effect:2023/03/15

  • Year-Month-Day Hour:Minute:Second:"2006-01-02 15:04:05"

    {{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}}
    

    Display effect:2023-03-15 10:30:00

  • Month-Day:"01-02"

    {{stampToDate(item.CreatedTime, "01-02")}}
    

    Display effect:03-15

By flexibly using the reference time formatting rules of Go language, you can easily achieve any date and time display effect you want in the AnQiCMS template.This unified and powerful formatting capability makes your website content display more professional and user-friendly.

Frequently Asked Questions (FAQ)

1. Why is the date format string "2006-01-02", not "YYYY-MM-DD"?

This is because AnQiCMS is based on the Go language, which uses a unique 'reference time' mode when formatting time. You need to follow2006年01月02日 15时04分05秒This fixed reference time to write your format string. For example,2006Represents the year, `0