As an experienced website operation expert, I know that in daily content management, timestamp formatting is a seemingly simple detail that often troubles many operation personnel.In AnQiCMS such an efficient and flexible content management system, mastering the use of template tags can greatly enhance the professionalism and user experience of content presentation.Today, let's delve deeply into a very practical and crucial tag in the AnQiCMS template -stampToDateLook at how it helps us easily format timestamps.

RevelationstampToDate: Master timestamp formatting in AnQiCMS easily.

In AnQiCMS template development, we often encounter scenarios where we need to display article publish time, update time, and so on.These time data are usually stored in the database in the form of a Unix Timestamp, which is the number of seconds elapsed since 00:00:00 on January 1, 1970, UTC, and is usually a 10-digit integer.However, to directly1609470335This number presented to the user is obviously unfriendly and unprofessional. At this time,stampToDateThe label comes in handy, it can convert these original timestamps into the date and time format we are accustomed to reading.

stampToDateThe design philosophy is very simple and efficient, allowing you to format timestamps directly in the template without complex backend processing.The core function is to convert a 10-digit timestamp into a beautiful and readable date and time representation according to the format string you provide.

How to usestampToDateTag?

stampToDateThe usage method of the label is very intuitive, the basic syntax structure is:

{{ stampToDate(时间戳, "格式") }}

Let's break down these two parameters in detail:

The first parameter: timestamp

This parameter accepts a 10-digit integer representing time. In AnQiCMS templates, these timestamps usually come from the content model fields, for example:

  • item.CreatedTimeThe creation time of content (such as articles, products).
  • item.UpdatedTime: Last update time of the content.
  • Or you can directly go through the template.{% set publishStamp = 1609470335 %}In this way, define a timestamp variable for testing.

When you passarchiveListorarchiveDetailGet the content item (when the tagitemis used when)item.CreatedTimeoritem.UpdatedTimeasstampToDatethe first parameter.

The second parameter: format string

This isstampToDateThe most 'unique' label, which is also a place that many new users may find confusing. AnQiCMS is developed based on the Go language, so its time formatting follows the conventions of the Go language, rather than the commonYYYY-MM-DDoryyyy-MM-ddformat.

Go language uses a specific "reference time" to define the date and time format, and this reference time is:

2006-01-02 15:04:05.999999999 -0700 MST

You do not need to memorize the meanings of all these numbers and letters, just remember the following key parts, and use them to spell out the format you want:

  • 2006: Represents the year, it can be06(two-digit year) or2006(four-digit year).
  • 01: represents the month, it can be1(one-digit month) or01(two-digit month).
  • 02: Represents a date, it can be2(One-digit date) or02(Two-digit date).
  • 15: Represents an hour (24-hour format), it can be3(One-digit hour) or15(Two-digit hour). If it is in 12-hour format, then use03or3and match withPMorAM.
  • 04: Represents minutes.
  • 05: Represents seconds.
  • MSTor-0700: Represents time zone information, usually we do not use it directly in the template.

When you want to format time, you simply replace the corresponding part of the above reference time with the date-time separator or text you want.

Practical formatting example

Let us go through several specific examples to demonstratestampToDateThe powerful functions in the AnQiCMS template:

Suppose we have a content itemitemItsCreatedTimeThe value of1675862400(i.e., at 0:00:00 on February 9, 2023).

  1. Display the full date and time (24-hour format):

    {{ stampToDate(item.CreatedTime, "2006-01-02 15:04:05") }}
    {# 输出: 2023-02-09 00:00:00 #}
    
  2. Display only the date:

    {{ stampToDate(item.CreatedTime, "2006-01-02") }}
    {# 输出: 2023-02-09 #}
    

    You can also use a slash or other characters as a separator:

    {{ stampToDate(item.CreatedTime, "2006/01/02") }}
    {# 输出: 2023/02/09 #}
    
  3. Only display time:

    {{ stampToDate(item.CreatedTime, "15:04") }}
    {# 输出: 00:00 #}
    
  4. Custom Chinese format:

    {{ stampToDate(item.CreatedTime, "2006年01月02日 15时04分") }}
    {# 输出: 2023年02月09日 00时00分 #}
    
  5. Display in the content list:

    CombinearchiveListTags, display the publication time of each article in the article list loop:

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

By these examples, you can seestampToDateThe flexibility and practicality of tags. Once you master the Go language time formatting reference, you can freely control the display of timestamps in AnQiCMS.

Summary

stampToDateThe label is an indispensable tool in the AnQiCMS template, converting complex timestamp data into user-friendly date and time display.Understand the design principles based on the Go language reference time, and flexibly apply various format strings, which can significantly improve the reading experience and professionalism of the website content.As a website operations expert, mastering these core template functions will make your content management work twice as effective.


Frequently Asked Questions (FAQ)

Q1: Why is the time format of AnQiCMS not commonly usedYYYY-MM-DDform?

A1: AnQiCMS is developed based on the Go language, its template engine follows the specific conventions of the Go language when handling time formatting. The Go language does not useYYYY-MM-DDThis symbol representation is, rather than through a fixed "reference time" (2006-01-02 15:04:05) to define the format. You just need to remember the meanings of the numbers and strings in this reference time (such as 2006represents the year,01Represent months, etc.), then combine them in the format you want. For example, if you wantYYYY-MM-DDformat, write it2006-01-02.

Q2: If the timestamp I get from the external system is 13 digits (millisecond level),stampToDateCan I still use it?

A2: AnQiCMS of thestampToDateThe label expects to receive a 10-digit Unix timestamp (second level).If your timestamp is 13 digits (millisecond level), you need to convert it to 10 digits first.The usual practice is to divide the 13-digit timestamp by 1000.In the AnQiCMS template, if you cannot perform mathematical operations directly, you may need to process in the backend logic layer before obtaining the data, or consider using AnQiCMS custom filters and other advanced features.The most reliable way is to convert the millisecond timestamp to a second-level timestamp in the Go backend code before passing it to the template.

Q3: AnQiCMS template has onedateFilter, it andstampToDateWhat is the difference?

A3: