As an experienced website operation expert, I know that in daily content management, timestamp formatting is a detail that seems simple but often troubles many operation personnel.Using templates in AnQiCMS, an efficient and flexible content management system, can greatly enhance the professionalism of content presentation and the user experience.stampToDateEnglish translation: Look at how it helps us easily format timestamps.

UnveilingstampToDateEnglish translation: Easily handle timestamp formatting in AnQiCMS.

In AnQiCMS template development, we often encounter scenarios where we need to display the article publish time, update time, etc.This time data is usually stored in the database in the form of a timestamp (Unix Timestamp), which is the number of seconds elapsed since 00:00:00 on January 1, 1970, Coordinated Universal Time (UTC), and is usually an integer of 10 digits.1609470335Such numbers presented to users are obviously unfriendly and unprofessional. At this time,stampToDateLabels come in handy, they can convert these raw timestamps into the date and time format we are accustomed to reading in daily life.

stampToDateThe design concept 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 easy-to-read date and time representation according to the format string you provide.

How to usestampToDateTags?

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

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

Let's take a detailed breakdown of these two parameters:

The first parameter: timestamp

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

  • item.CreatedTime: Content creation time (such as articles, products).
  • item.UpdatedTime: The last update time of the content.
  • Or you can directly use the template.{% set publishStamp = 1609470335 %}This way to define a timestamp variable for testing.

当你通过archiveListorarchiveDetailGet content item (with tags)item) then you can use it directlyitem.CreatedTimeoritem.UpdatedTimeasstampToDatethe first parameter.

Second parameter: format string

This isstampToDateThe most 'unique' place of the label, also a place that many new users may feel confused. AnQiCMS is developed based on 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 date and time formats, 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 the date, it can be2(one-digit date) or02(two-digit date).
  • 15: Represents the hour (24-hour format), it can be3(one-digit hour) or15(Two-hour period). If it is in 12-hour format, then use03or3and match withPMorAM.
  • 04: Represents minutes.
  • 05: Represents seconds.
  • MSTor-0700: Represents timezone information, usually we don't use it directly in templates.

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

Practical formatting example

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

Assume we have a content itemitemitsCreatedTimeThe value is1675862400English translation: (即2023年2月9日0时0分0秒)。

  1. English translation: 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. 仅显示日期:

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

    You can also use slashes or other characters as separators:

    {{ 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. Show in 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 %}
    

Through these examples, you can see:stampToDateThe flexibility and practicality of tags.If you master the Go language time formatting reference, you can freely control the display method of timestamps in AnQiCMS.

Summary

stampToDateTags are an indispensable tool in AnQiCMS templates, converting complex timestamp data into user-friendly date and time display.Understand its design principles based on the reference time of the Go language 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 efficient.


Common Questions and Answers (FAQ)

Q1: Why is the time format of AnQiCMS not commonly used?YYYY-MM-DDForm?

A1: AnQiCMS is developed based on Go language, its template engine follows the specific conventions of Go language when handling time formatting. Go language does not useYYYY-MM-DDThis notation is represented by 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 as2006Represents the year,01represent months and so on), then combine them in the format you want. For example, if you wantYYYY-MM-DDformat, write2006-01-02.

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

A2: AnQiCMS'sstampToDate标签默认期望接收一个10位的 Unix 时间戳(秒级)。If your timestamp is 13 digits (millisecond level), you need to convert it to 10 digits first.The common practice is to divide the 13-digit timestamp by 1000.In AnQiCMS templates, if you cannot perform mathematical operations directly, you may need to process the logic on the backend before obtaining the data, or consider using advanced features such as AnQiCMS custom filters.The most reliable way is to convert the millisecond timestamp to second level in the Go backend code before passing it to the template.

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

A3: