In the daily operation of AnQi CMS, we often need to handle various types of data, among which time information is undoubtedly the most common and most important category.Whether it is the publication date of the article, the update time, or the specific moment of user comments, these time data are usually stored in the form of timestamps.However, the original timestamp is not intuitive for ordinary users, it is more like a string of meaningless numbers.This is particularly important to convert these timestamps into a date or time format that is easy to understand.

The template system of Anqi CMS, adopting syntax similar to Django template engine, provides us with powerful flexibility to control content display. The built-in tool for handling timestamps is thestampToDateTags, it can help us easily convert the 10-digit timestamps obtained from the background into various readable date and time formats.

Understanding timestamps and time data in the Anqi CMS

In AnQi CMS, you may encounter timestamps in multiple places. For example, when you usearchiveDetailtags to get article details, orarchiveListtags to get a list of articles,CreatedTime(Creation time) andUpdatedTimeThe "Update Time" field always returns a 10-digit timestamp. Similarly,commentListto get comments from theCreatedTime, or throughuserDetailTag retrieval of usersLastLogin(Last Login Time) andExpireTimeAt the time of VIP expiration, timestamps will also be received.

These timestamps are usually the number of seconds elapsed since the Unix epoch (00:00:00 UTC on January 1, 1970). Although they are convenient for machines to read, they are not easy for the human eye.1675910400This number is far less than2023年02月09日or今天上午10:30intuitive.

stampToDateLabel: Magic of time formatting

In order to convert these timestamps into the familiar format, Anqi CMS provides{{stampToDate(时间戳, "格式")}}Such usage.The key here is the second parameter—the 'format' string.Y-m-d H:i:sinstead of using2006-01-02 15:04:05This fixed reference time is used to define the format.

It looks a bit peculiar, but once you grasp the pattern, you will find it very intuitive:

  • 2006Represents the year (YYYY)
  • 01Represents the month (MM)
  • 02Represents the day (DD)
  • 15Represents the hour in 24-hour format (HH)
  • 04Represents the minutes (MM)
  • 05Represents the seconds (SS)
  • MonAbbreviation for days of the week (e.g., Mon, Tue)
  • MondayFull name for days of the week
  • JanAbbreviation for month (e.g., Jan, Feb)
  • JanuaryFull name of the month
  • MSTor-0700Abbreviation for time zone information

You just need to replace the date and time format you want to display2006-01-02 15:04:05with the number or letter at the corresponding position.

For example, if you want to format the timestamp as “2023 February 09”:“2023年02月09日”:“2023 February 09”{{stampToDate(时间戳, "2006年01月02日")}}

If you need to display it as “2023/02/09 10:30”:“2023/02/09 10:30”:“2023/02/09 10:30”{{stampToDate(时间戳, "2006/01/02 15:04")}}

or a more detailed “2023-02-09 Thursday 10:30:00”:{{stampToDate(时间戳, "2006-01-02 星期一 15:04:05")}}

Remember that each number and letter in the format string has a specific meaning, and it must be strictly followed2006-01-02 15:04:05to construct this 'magic number'.

Apply in the template.

Now, let's see how to apply it in an actual security CMS templatestampToDateLabel.

Suppose you are creating a list of articles page and you want to display the publication date of each article:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <article>
        <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
        <p>
            发布于:<span>{{ stampToDate(item.CreatedTime, "2006年01月02日") }}</span>
            阅读量:<span>{{ item.Views }}</span>
        </p>
        <p>{{ item.Description|truncatechars:100 }}</p>
    </article>
    {% endfor %}
{% endarchiveList %}

In this example,item.CreatedTimeIt will output the original timestamp, instead.stampToDate(item.CreatedTime, "2006年01月02日")It will then convert it into a friendly format like “2023年02月09日”.

For example, on the article detail page, you may need to display more precise publishing and update times:

<article>
    <h1>{{ archive.Title }}</h1>
    <div class="meta-info">
        <span>发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04:05") }}</span>
        {% if archive.CreatedTime != archive.UpdatedTime %}
            <span>最后更新:{{ stampToDate(archive.UpdatedTime, "2006年01月02日 星期一 15点04分") }}</span>
        {% endif %}
        <span>浏览量:{{ archive.Views }}</span>
    </div>
    <div class="content">
        {{ archive.Content|safe }}
    </div>
</article>

Here, we not only formatted the publishing time but also made a judgment through it.CreatedTimeandUpdatedTimeWhether different decides whether to display the update time and format it into a Chinese style that includes the weekday information.

Tips for formatting timestamps.

  • Maintain consistency:The time display format of the entire website should be consistent to provide a good user experience.
  • Consider the target audience:Choose the date and time format that best suits your primary website user group. For example, websites aimed at international users may require a more standardized format.YYYY-MM-DDFormat, while Chinese users are more accustomed toYYYY年MM月DD日.
  • Testing is crucial:Test your time display on different browsers and devices, ensuring they can display correctly under any circumstances.
  • Check the official Go language documentation:If you need more complex or less common formats, you can check the official Go language'stimeDocument the package, understand all supported formatting strings, and customize the display to meet your needs.

MasteredstampToDateLabel and Go language's time formatting rules, you can easily convert timestamp data in the Anqi CMS into dates and times that users prefer and are easy to read, thereby greatly enhancing the professionalism and user-friendliness of the website content.


Common Questions (FAQ)

Q1: I used{{stampToDate(item.CreatedTime, "YYYY-MM-DD")}}, why does the result show as incorrect? A1:This is because the time formatting of Anqi CMS follows the rules of the Go language, and it does not useYYYY-MM-DDThis common format is instead used2006-01-02As a reference. You should modify the format string to"2006-01-02"To display the year-month-date correctly. Please refer to it carefully2006-01-02 15:04:05This fixed value is used to construct your format string.

Q2: Besides the creation/update time of the article, where else do you need to usestampToDateto format timestamps? A2:the comments list that you are incommentListTagsCreatedTime)、User Details(userDetailTagsLastLogin/ExpireTime)etc. obtained time data is also timestamp format. These places can also usestampToDatetags for formatting to provide a more friendly display.

Q3: How do I write the template code if I only want to display the current year? A3:The Anqi CMS provides a{% now "2006" %}Tags can directly obtain the current year. If you want to format an existing timestamp to only display the year, you can use{{stampToDate(时间戳, "2006")}}For example,{{stampToDate(item.CreatedTime, "2006")}}It will display the year of the article's publication.