Core Tools:stampToDatetags

Anqi CMS allows us tostampToDateLabel, stores the original timestamp of the article (usually a 10-digit number) formatted to any date-time style we need. Its basic usage is very intuitive:

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

Here are the时间戳变量Is the field stored in the article to store timestamps, such as the creation time of the article (CreatedTime) or update time (UpdatedTime).格式化字符串which is the key to define how you want the time to be displayed.

Master the date and time formatting rules of the Go language

When usingstampToDatethen,格式化字符串are written in a way that may be familiar to us in daily lifeYYYY-MM-DDor%Y-%m-%dIt is defined differently, based on the unique "reference time" in Go language. Go language uses a fixed point in time:2006年01月02日 15点04分05秒As a reference for all date-time formatting strings.

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

  • Year:2006(Reference Year)
  • Month:01(Reference Month, with leading zero) Or1(without leading zeros)
  • Date:02(Reference Date, with leading zero) Or2(without leading zeros)
  • Hour:15(24-hour format) or03(12-hour format, with leading zero) Or3(12-hour format, without leading zero) Or
  • minutes:04(with leading zeros) or4(without leading zeros)
  • seconds:05(with leading zeros) or5(without leading zeros)
  • Weekday: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".

Practice Application: Formatting Time in Article List and Detail Page

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 use similaritem.CreatedTime(while looping through the article list) orarchive.CreatedTime(In the detail page directly accessed) to obtain the timestamp, then combinestampToDateLabel formatting.

Here are some examples of formatting article time in templates:

Example 1: In the article list pagearchiveListLooping)

Assuming you are usingarchiveListTag looping displays the article list, and it is desired that 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 2: On the article detail page

In the article detail page, we usually show 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>

Through these simple code snippets, you can make the article's time display more user-friendly.

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

Customization and Flexibility

stampToDateThe power 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 to create a unique date and time format.Whether it is a concise "month/day" or a detailed "year-month-day Monday hour:minute:second", it can be achieved by adjusting the format string.

By skillfully applyingstampToDateTags and date-time formatting rules in Go language, which can make the time information on your website clearer and more professional, thus enhancing the overall user experience.


Common Questions (FAQ)

问:Why is the format string"2006-01-02"Instead"YYYY-MM-DD"?

答:An