In website content operation, the publication time or update time of the article is an important element in conveying information to visitors.A clear and readable date-time format can not only improve the user experience but also allow visitors to quickly understand the timeliness of the content.AnQi CMS provides a very practical template tag to help us easily achieve this goal.

Core Tool:stampToDateTag

AnQi CMS allows us to go throughstampToDateLabel, format the original timestamp stored in the article (usually a 10-digit number) into any date-time style we need. Its basic usage is very intuitive:

{{ stampToDate(时间戳变量, "格式化字符串") }}

Here时间戳变量Is the field storing the timestamp in the article, such as the creation time of the articleCreatedTime) or update time (UpdatedTime).格式化字符串Is the key that defines how you want the time to be displayed

Master the date and time formatting rules of the Go language

While usingstampToDatetags in the template,格式化字符串The syntax may be familiar to us in everyday lifeYYYY-MM-DDor%Y-%m-%dis different, as it is defined based on the unique 'reference time' of the Go language. The Go language adopts a fixed point in time:January 2, 2006 3:04:05 PMAs a reference for all datetime formatting strings.

This means that when you want to display a date and time element, you need to use the corresponding number of the reference time to construct your format string. The following are some common format comparisons:

  • Year:2006(reference year)
  • Month:01(reference month, with leading zero) or1(without leading zero)
  • Date:02(reference date, with leading zero) or2(without leading zero)
  • hours:15(24-hour format) or03(12-hour format with leading zero) or3(12-hour format without leading zero)
  • minute:04(with leading zero) or4(without leading zero)
  • seconds:05(with leading zero) or5(without leading zero)
  • Week:Mon(abbreviation of the day of the week) orMonday(full name of the day of the week)

For example, if you want to display 'Year-Month-Day Hour:Minute', the corresponding format string is"2006-01-02 15:04".

Practical application: Formatting time in article list and detail pages

In AnQi CMS, the creation time of articles is usually stored inCreatedTimefield, and the update time is stored inUpdatedTimefield. Whether it is an article list page or an article detail page, you can view similaritem.CreatedTime(When looping through the article list) orarchive.CreatedTime(When directly accessing the article detail page) to obtain the timestamp, then combinestampToDateFormat labels.

Here are some examples of formatting article time in templates:

Example one: on the article list page(archiveListin the loop)

Suppose you are usingarchiveListTag循环显示文章列表,and hope each article shows the publish date and update date.

{% archiveList articles with type="page" limit="10" %}
    {% for item in articles %}
    <article>
        <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
        <p>发布于:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}</p>
        <p>更新于:{{ stampToDate(item.UpdatedTime, "2006-01-02 15:04") }}</p>
        <p>{{ item.Description }}</p>
    </article>
    {% endfor %}
{% endarchiveList %}

Example two: On the article detail page

On the article detail page, we usually display more detailed date and time information, including even the day of the week.

<article>
    <h1>{{ archive.Title }}</h1>
    <div class="article-meta">
        <span>发布日期:{{ stampToDate(archive.CreatedTime, "2006年01月02日 星期一") }}</span>
        <span>最后更新:{{ stampToDate(archive.UpdatedTime, "2006/01/02 15:04:05") }}</span>
    </div>
    <div class="article-content">
        {{ archive.Content|safe }}
    </div>
</article>

By using these simple code snippets, you can make the article's time display more humanized.

It is worth mentioning that if you only need to display directly on the article detail pageCreatedTimeorUpdatedTime,archiveDetailThe tag itself supportsformatParameters are formatted, for example:{% archiveDetail with name="CreatedTime" format="2006-01-02" %}But this is limited to directly calling these fields, for other timestamp variables in the template,stampToDateTags are still a general and powerful choice.

Customization and flexibility

stampToDateThe strength of the tag lies in its flexibility. You can freely combine the reference time elements of the Go language according to the design style of the website and the reading habits of the user group, creating a unique date and time format.Whether it is a concise

By mastering the use ofstampToDateTags and Go language date and time formatting rules, you can make the time information on the website clearer and more professional, thereby enhancing the overall user experience.


Frequently Asked Questions (FAQ)

Why is the format string"2006-01-02"instead of"YYYY-MM-DD"?

Answer: An