The website content is updated frequently, with each article, product, or comment having its creation and update timestamp.However, these times are usually stored in the background in the form of a series of numbers, which is what we commonly refer to as timestamps.These timestamps are not user-friendly for visitors, we need to convert them into a clear date and time format.

In AnQiCMS, formatting these timestamps into the date and time format we are accustomed to is actually very simple and intuitive.A powerful template tag has been built into the system, which can help us easily achieve this.

Core Tools:stampToDatetags

AnQiCMS provides a namedstampToDateThe template tag, specifically used to convert Unix timestamps (usually 10 or 13 digits) into readable date and time strings. This tag is very flexible and requires two key pieces of information:

  1. Timestamp to be formatted:This is usually what you get from the database.CreatedTimeorUpdatedTimeand other fields.
  2. Formatted string:This is the key to define the style in which you want the date and time to be displayed.

Its basic syntax is like this:{{stampToDate(时间戳, "格式化字符串")}}

Here is a special note about "formatted strings".AnQiCMS is developed in Go language, therefore it follows the unique time formatting rules of Go language.Y-m-d H:i:sThe placeholders are different, Go language uses a specificReference timeAs a template:2006-01-02 15:04:05You only need to remember this reference time and replace the corresponding part in the reference time with the style you want according to the format you want to display.

For example:

  • If you want to display2023-10-26Then the formatted string is"2006-01-02".
  • If you want to display2023年10月26日Then the formatted string is"2006年01月02日".
  • If you want to display2023-10-26 14:30:15Then the formatted string is"2006-01-02 15:04:05".

Actual application scenarios

ThisstampToDateTags can be used anywhere in the AnQiCMS template.No matter whether you are displaying a list of articles, article details, the recent login time of users, or the comment posting time, you can use it to beautify the display whenever you encounter a timestamp field.

For example, on the article detail page (usually correspondingarchiveDetailTags get the data) display the publication time and update time of the article:

<article>
    <h1>{{ archive.Title }}</h1>
    <p>
        <span>发布于:{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04")}}</span>
        <span>更新于:{{stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05")}}</span>
    </p>
    <div>
        {{ archive.Content|safe }}
    </div>
</article>

On the article list page (usingarchiveListIn the label data, display the creation date for each article:.

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <li>
        <a href="{{item.Link}}">
            <h5>{{item.Title}}</h5>
            <p>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</p>
        </a>
    </li>
    {% empty %}
    <li>暂无文章内容。</li>
    {% endfor %}
{% endarchiveList %}

Display the posting time of comments in the comment list:.

{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
    {% for item in comments %}
    <div>
        <span>{{item.UserName}}</span> 于 <span>{{stampToDate(item.CreatedTime, "2006年01月02日 15:04")}}</span> 评论道:
        <p>{{item.Content}}</p>
    </div>
    {% endfor %}
{% endcommentList %}

More flexible format customization

The reference time format in the Go language is very powerful, you can combine its elements to meet various complex date and time display requirements:

  • Display the day of the week:UseMon[Abbreviation of Monday] orMonday[Full name of Monday)].
    • For example:"2006年01月02日 Monday"Will be displayed2023年10月26日 Thursday.
  • Show AM/PM:UsePMorpm.
    • For example:"15:04 PM"Will be displayed02:30 PM[2:30 PM)].
  • Show only the month and year:
    • For example:"2006年01月"Will be displayed2023年10月.

You can freely combine these reference elements as needed to create a unique date-time format.

Other date/time related tags and filters

ExceptstampToDateAnQiCMS also provides some auxiliary functions related to date and time:

  • Display the current system time:If you need to display the current server time on the page, you can use{% now "格式化字符串" %}tags. It also follows the formatting rules of the Go language.
    
    <p>当前时间:{% now "2006年01月02日 15:04:05" %}</p>
    
  • dateFilter:for those that are already in Go languagetime.TimeType variables, you can also usedateto format the filter. However, please note thatdatethe filter applies only totime.TimeType, using it directly on integer timestamps will cause errors. Most time data in AnQiCMS templates are provided in timestamp format, thereforestampToDateThis is the preferred method for handling timestamps.

In summary, AnQiCMS is through an intuitive interface.stampToDateLabels and the flexible reference time format of Go language make the display of website content's time simple and expressive.Master this little trick, and it will make your website content more readable and professional.


Common Questions (FAQ)

1.Why does AnQiCMS use the numeric combination '2006-01-02 15:04:05' as the format string when formatting dates?

This is a time formatting method unique to the Go language. Unlike many programming languages that use symbols (such asYRepresents the year,mrepresenting the month), Go language provides a specific reference time2006-01-02 15:04:05(That is, 3:04:05 PM on January 2, 2006) as the formatted "template".You only need to write the desired display format in the formatted string according to the meaning of each number in this reference time, and the system will automatically apply it to the actual timestamp.2006.

2. How can I set the format to display the day of the week or AM/PM?

To display the day of the week, you can use the reference time of the following:Mon(Abbreviation for Monday) orMonday(Represent the full name of Monday). For example,“"2006年01月02日 Monday"will display `2023”