In website content management, the clear display of time information is crucial for user experience.Whether it is the release date of the article or the submission time of the comments, a format that conforms to the reading habits can make information communication more effective.AnQiCMS provides flexible template functionality, allowing us to easily format timestamps.

Core Function: AnQiCMS timestamp formatting tagstampToDate

AnQiCMS template engine built-in a namedstampToDateThe label, specifically used for formatting timestamps.This tag can convert a Unix timestamp (usually 10 digits) into the date and time string we read in daily life.

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

The key here lies in the second parameter - 'format string'. It is not what we commonly seeYYYY-MM-DD HH:mmThis type of placeholder, but follows the unique time formatting rules of the Go language.

Understand the time formatting rules of the Go language

Go language uses a very unique and powerful method for time formatting in its design.It does not use abstract symbols, but instead uses a fixed reference point as the template for the format.

2006-01-02 15:04:05

To format a timestamp into the style you want, you need to replace the corresponding numbers and separators in the reference time with the ones you want to display in the final result. For example:

  • If you want to display the year, use2006.
  • If you want to display the month, use01.
  • If you want to display the date, use02.
  • If you want to display the hour in 24-hour format, use15.
  • If you want to display minutes, use04.
  • If you want to display seconds, use05.

If a part of the reference time (such as06Representing 06 seconds) It is not needed in your target format, so just ignore it.

Actual operation: Format the timestamp as “Year-Month-Day Hour:Minute”

Now, let's solve the core issue: how to format the timestamp in the AnQiCMS template to the display format of "year-month-day hour:minute".

Suppose we are iterating over an article list in a template, and each article has aCreatedTimefield that stores a Unix timestamp.

To achieve the target format "Year-Month-Date Time:Minute", we refer to the reference time in the Go language2006-01-02 15:04:05:

  • 2006Corresponding year
  • 01Corresponding month
  • 02Corresponding date
  • 15Corresponding to the hour in 24-hour format
  • 04Corresponding to minutes

Therefore, our formatted string should be written as"2006年01月02日 15:04".

Below is an example inarchiveLista tag used in the complete example:

{% archiveList archives with type="list" limit="5" %}
    {% for item in archives %}
        <div class="article-item">
            <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
            <p class="article-meta">
                发布时间:{{ stampToDate(item.CreatedTime, "2006年01月02日 15:04") }}
                <span> - 浏览量:{{ item.Views }}</span>
            </p>
            <p>{{ item.Description }}</p>
        </div>
    {% endfor %}
{% endarchiveList %}

In this code block,item.CreatedTimeIs the creation timestamp of the article. ThroughstampToDateTags and"2006年01月02日 15:04"This formatting string will eventually display on the page in a format similar to '2023 October 27 10:30'.

Common application scenarios

This time formatting method can be applied to various scenarios in the AnQiCMS template:

  • Article and product detail page:Display the publish date, update date.
  • Comment list:Display the submission time of each comment.
  • Event page:Mark the start and end time of the event.
  • User center:Show user registration time, last login time, etc.

Wherever you need to display timestamps in a specific humanized format,stampToDatetags can provide an effective solution.

Points to note

  • The precision of Go language format:The time formatting rules for Go language are very strict, and must be followed completely.2006-01-02 15:04:05The corresponding part to construct your format string. For example, if you want to display a two-digit year, you must use06instead of2006.
  • Timestamp type: stampToDateThe label expects a Unix timestamp, which is usually a 10-digit integer (seconds). If your time data is in a different format (such asYYYY-MM-DD HH:mm:ssString), it may need to be converted first before use.
  • withdateFilter differentiation:There is also one in the AnQiCMS template.dateA filter, but it requires the value to be in Go languagetime.Timean object type, not a timestamp. When handling timestamps, be sure to usestampToDate.

BystampToDateUnderstanding the time formatting rules of Go language, you can easily implement various time display needs in the AnQiCMS template, adding a professional and readable touch to your website content.


Frequently Asked Questions (FAQ)

Q1: Why do I useYYYY-MM-DD HH:mmsuch format strings not work?

A1: AnQiCMS'stampToDateThe label uses the Go language time formatting rules, it does not useY/M/D/H/mplaceholder characters, but through a fixed reference time2006-01-02 15:04:05To build the format. You need to replace the corresponding number in this reference time with the format you want to display. For example, to display the year, use2006; to display the month, use01and so on.

Q2:stampToDateDoes the label support a 13-digit timestamp (milliseconds)?

A2:stampToDateThe label expects to receive a 10-digit Unix timestamp (in seconds). If your timestamp is a 13-digit millisecond timestamp, you need to divide it by 1000 to convert it to seconds and then pass it instampToDateLabel. For example:{{ stampToDate(item.CreatedTime / 1000, "2006年01月02日 15:04") }}.

Q3: Besides "year-month-date hour:minute", can I format it in other styles?

A3: Absolutely.If you understand the time formatting rules of the Go language, you can flexibly combine almost any date and time format you want."Mon, 02 Jan 2006 15:04:05 -0700"This reference time, or directly refer to the Go language official document regardingtimepackage formatting example.