In website operations, date and time information such as content release time and update time is crucial for user experience and content management.AnQiCMS as a rich-featured content management system, provides flexible template tags, allowing users to easily format the timestamp data stored in the background into various easily readable date or time strings to meet different display needs.

Understand the timestamp in AnQiCMS

Most of the date and time fields, such as the article's, inside AnQiCMS,CreatedTime(Creation time) orUpdatedTimeThe update time, stored in the form of Unix timestamp.This is a 10-digit integer representing the number of seconds from 00:00:00 UTC on January 1, 1970 to the present.Although this format is convenient for system storage and processing, it is not very intuitive to display directly to users.Therefore, we need to convert it to a human-readable date or time format.

Core function:stampToDateTag

AnQiCMS provides a dedicated template tagstampToDateto solve the timestamp formatting problem. The use of this tag is very concise and intuitive, it accepts two parameters:

  1. Timestamp variable: A timestamp that needs to be formatted, typically a field you obtain from the content model, for exampleitem.CreatedTime.
  2. Format string: A string used to define the output format.

The basic syntax structure is:{{stampToDate(时间戳变量, "格式字符串")}}.

The mystery of format strings: Reference time in Go language

Pay attention to the format string writing.AnQiCMS is developed based on the Go language, therefore it follows the conventions of Go language in date and time formatting.Y-m-d H:i:sSuch placeholders are replaced with a special 'reference time' to define the desired output style. This reference time is:2006-01-02 15:04:05.

You can imagine this reference time as a template, you can write down how you want the date and time to be displayed using this reference time. For example:

  • If you want to display the year (2006),will format the string within2006reserve.
  • If you want to display the month(01),will format the string within01reserve.
  • etc.,02represents the day,15represents the hour (24-hour format),04represents the minute,05represents seconds.

Here are some common formatting examples:

  • Show only the date (Year-Month-Day)If you wish the date to be2023-01-01The format string is written as"2006-01-02".
  • Display the full date and timeIf needed2023-01-01 12:30:00Such an output, the format string is"2006-01-02 15:04:05".
  • Custom delimiter: Want2023年01月01日Such Chinese format, can be written as"2006年01月02日"If you want it to be01/01/2023Then it is written as"01/02/2006".
  • Only show the timeFor example12:30:00The format string is"15:04:05".
  • Display short year and monthIf only01-02(Month-Day), it can be written as"01-02".

Actual application in template

In the AnQiCMS template, you can usearchiveList/archiveDetail/commentListThe tag retrieves data items containing timestamps. Then, pass the timestamp field of these data items tostampToDateFormat labels.

For example, on a list page of articles, we want to display the publication date of each article:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <li>
        <a href="{{item.Link}}">
            <h5>{{item.Title}}</h5>
            <div>
                <span>发布时间:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</span>
                <span>阅读量:{{item.Views}}</span>
            </div>
        </a>
    </li>
    {% endfor %}
{% endarchiveList %}

On the article detail page, we may need to display more detailed update time:

{% archiveDetail archiveDetailData %}
    <h1>{{archiveDetailData.Title}}</h1>
    <p>
        <span>发布于:{{stampToDate(archiveDetailData.CreatedTime, "2006-01-02 15:04")}}</span>
        {% if archiveDetailData.CreatedTime != archiveDetailData.UpdatedTime %}
        <span>更新于:{{stampToDate(archiveDetailData.UpdatedTime, "2006-01-02 15:04:05")}}</span>
        {% endif %}
    </p>
    <div>
        {{archiveDetailData.Content|safe}}
    </div>
{% endarchiveDetail %}

Tip:time.TimeType anddateFilter

AnQiCMS template indeed exists adatefilter, but it requires the variable to be in Go languagetime.TimeAn object, not the raw Unix timestamp. If we pass the timestamp directly todateThe filter, the system usually fails or does not work as expected. Therefore, when processing the original Unix timestamp obtained from the database or content model, be sure to usestampToDateLabel, it is specially designed for this kind of scenario.

If you just want to get and format the current server time without handling specific content timestamps, you can conveniently use{% now "格式字符串" %}Label, for example{% now "2006-01-02 15:04" %}Will directly output the current date and time.

BystampToDateLabel, AnQiCMS makes timestamp formatting output simple and powerful, meeting various refined needs for website content display.


Frequently Asked Questions (FAQ)