Good, I'm glad to deeply interpret AnQiCMS for you.stampToDateTags, help you easily manage the time display on the website.


Flexible time management: make use of AnQiCMS'sstampToDateLabel customization date and time format

On website content management, the way dates and times are displayed often directly affects user experience and the clarity of information transmission.An concise and readable time format allows visitors to understand the timeliness of the content more quickly.stampToDateTags, allowing you to easily format the Unix Timestamp stored in the database into various custom date and time formats.

Understanding Timestamps: Your Content's Time Source

In AnQiCMS internally, many time-related information, such as the release time of articles (CreatedTime)Update time(UpdatedTimeComments time, even the latest login time of the user, are all stored in the form of timestamps.The timestamp is usually a 10-digit number, representing the number of seconds elapsed since 00:00:00 on January 1, 1970, Greenwich Mean Time (UTC).1678886400Such numbers are very efficient for computers, but difficult for ordinary visitors to understand their exact meaning.

This brings up our need for time formatting, converting these original numbers into formats that people are accustomed to reading, such as “2023/03/15 10:00:00” or “3 days ago”.

stampToDateThe core usage of tags

stampToDateThe label usage is very intuitive:

{{ stampToDate(时间戳, "格式") }}

The key here is the second parameter - the 'format' string. It is used by many other systems.YYYY-MM-DDSuch placeholders are different, AnQiCMS internally uses the Go language time formatting standard, which has a fixed "reference time".You need to replace the date and time elements you expect to display with the corresponding parts of this reference time.

2006-01-02 15:04:05.999999999 -0700 MST[usually abbreviated as]2006-01-02 15:04:05)

This is like a 'magic number', it is not the actual year, but a placeholder used by the Go language to identify the position of time elements. You just need to remember that when you want to display the year in the result, just write2006;To display the month, write01;To display the date, write02. Continue in this manner.

Let's understand it through some examples of common formats:

  • Display only the year: {{ stampToDate(publishStamp, "2006") }}2023
  • Year-Month-Day: {{ stampToDate(publishStamp, "2006-01-02") }}2023-03-15
  • Year/Month/Day: {{ stampToDate(publishStamp, "2006/01/02") }}2023/03/15
  • Month/Day/Year (often used for internationalization): {{ stampToDate(publishStamp, "01/02/2006") }}03/15/2023
  • Hour:Minute:Second (24-hour format): {{ stampToDate(publishStamp, "15:04:05") }}10:00:00
  • Year-Month-Day Hour:Minute: {{ stampToDate(publishStamp, "2006-01-02 15:04") }}2023-03-15 10:00
  • Complete format with Chinese: {{ stampToDate(publishStamp, "2006年01月02日 15时04分05秒") }}2023年03月15日 10时00分00秒
  • Day of the week: {{ stampToDate(publishStamp, "Mon") }}Wed(Wednesday)

Please note that other characters in the format string, such as dashes, slashes, colons, spaces, and Chinese characters, will be output as is and serve as separators or descriptions.

tostampToDateApplied to your website: actual scenario examples

stampToDateTags are everywhere in the template development of AnQiCMS, especially in scenarios where content publishing or update time needs to be displayed.

1. In the document detail page, show the publish and update time

When you are browsing an AnQiCMS article, you will usually see its publish date. Assuming you arearticle/detail.html模板中,可以使用 EnglisharchiveDetail标签获取文档详情,并格式化其中的时间戳。

<article>
    <h1>{% archiveDetail with name="Title" %}</h1>
    <div class="meta-info">
        <span>发布于:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}</span>
        <span>更新于:{{ stampToDate(archive.UpdatedTime, "2006-01-02") }}</span>
        <span>浏览量:{% archiveDetail with name="Views" %}</span>
    </div>
    <div class="article-content">
        {% archiveDetail articleContent with name="Content" %}{{ articleContent|safe }}
    </div>
</article>

这段代码将把archive.CreatedTime格式化为精确到分钟的中文日期时间,而archive.UpdatedTime则只显示到日。

2. In the document list page, display the article publication date

In the article list page, you may want to display the publication date next to each article so that visitors can quickly understand the order and timeliness of the content.

<ul class="article-list">
    {% archiveList archives with type="page" limit="10" %}
        {% for item in archives %}
        <li>
            <a href="{{ item.Link }}">
                <h3>{{ item.Title }}</h3>
                <p>{{ item.Description|truncatechars:100 }}</p>
                <div class="list-meta">
                    <span>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
                    <span>分类:{% categoryDetail with name="Title" id=item.CategoryId %}</span>
                    <span>阅读量:{{ item.Views }}</span>
                </div>
            </a>
        </li>
        {% empty %}
        <li>暂无文章内容。</li>
        {% endfor %}
    {% endarchiveList %}
</ul>

Here,item.CreatedTimeFormatted in each loopYYYY-MM-DDFormat.

3. In the comments section, the comment time is displayed

If your website has a comment function, then the publication time of each comment is also a focus for users.

`twig

<h4>用户评论</h4>
{% commentList comments with archiveId=archive.Id type="list" limit="5" %}
    {% for item in comments %}
    <div class="comment-item">
        <strong>{{ item.UserName }}</strong>
        <span class="comment-time">于 {{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }} 评论</span>
        <p>{{ item.Content }}</p>
    </div>
    {% empty %}
    <p>暂无评论。</p>
    {% endfor %}
{%