How to format timestamps with the `stampToDate` tag in AnQiCMS template?

Calendar 👁️ 86

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:

Related articles

How to use `if` and `for` tags for logical judgment and loop traversal in AnQiCMS templates?

As an experienced website operations expert, I am well aware of the importance of a flexible and powerful template system for content management.AnQiCMS leverages its high-performance architecture based on the Go language and support for Django template engine syntax, providing us with great convenience.It is not just a tool for publishing content, but also a powerful tool for us to achieve personalized content display and improve user experience.

2025-11-07

How to deploy AnQiCMS Docker installation tutorial through 1Panel/aaPanel/Baota panel?

As an experienced website operations expert, I fully understand the importance of an efficient, secure, and easy-to-manage content system for a company.AnQiCMS (AnQiCMS) boasts a high-performance architecture based on the Go language, providing an excellent content management experience while also winning the favor of many users with its flexible deployment methods.

2025-11-07

How to install and manage multiple sites on the same server with AnQiCMS?

## AnQiCMS: Easily manage multiple sites on the same server and implement your brand matrix management As an experienced website operations expert, I know that in today's digital age, many companies and content creators may have multiple brands, product lines, or regional sites. How to efficiently and economically manage these content platforms is often a headache.The traditional method may mean deploying a separate system for each site, which consumes a lot of server resources and operation and maintenance efforts.

2025-11-07

How to customize navigation categories and link types in AnQiCMS's navigation settings?

## AnQiCMS Navigation Settings: Customize Your Website's Structure and User Experience In today's digital age, a website's navigation system is more than just a tool to guide users through the site; it is the 'skeleton' of the website architecture, directly affecting user experience, information retrieval efficiency, and even search engine optimization (SEO) performance. As an enterprise-level content management system developed based on the Go programming language, AnQiCMS understands the importance of the navigation system and therefore provides a highly flexible and easy-to-customize navigation setting function, aimed at helping businesses and content operators easily build a clear

2025-11-07

How to call the detailed information of a specific page in Anqi CMS template?

AnQi CMS, as an efficient and flexible content management system, has provided strong support for us in the field of content operation.In daily website management, single-page (also known as independent page) plays an indispensable role, whether it is to showcase corporate culture in "About Us", provide a communication bridge in "Contact Us", or introduce various service pages, they are all important windows for users to obtain core information.

2025-11-07

What is the basic syntax and necessary parameters of the `pageDetail` tag?

As an experienced website operations expert, I know that proficiently using template tags in a high-efficiency content management system like AnQiCMS is the key to improving operational efficiency and achieving personalized content display.Today, let's delve into a very practical tag - `pageDetail`, which can help us easily obtain and display the content of independent pages on websites.

2025-11-07

What are the respective roles of `variable name` and `field name` when calling the single-page detail?

As an experienced website operation expert, I know that understanding the essence of template tags in a high-efficiency content management system like AnQiCMS is the key to exerting its powerful functions and building flexible pages.Today, let's delve into a common problem encountered when calling single-page details: What are the respective roles of '`variable name`' and '`field name`'?In the AnQi CMS template system, the `pageDetail` tag is specifically used to call up detailed information for a single page.

2025-11-07

How will the content be directly output if the `variableName` of the `pageDetail` tag is not set?

## Unlock the secrets of Anqi CMS single page content output: The power of the simple `pageDetail` tag As an expert in website operations for many years, I know that efficiently and flexibly displaying content is the key to success in daily content management.AnQiCMS (AnQiCMS) is an enterprise-level content management system developed based on the Go language, and its simple and efficient template engine is very appealing to me.It provides great convenience in content display, especially for single-page content (such as "About Us", "Contact Information", etc.)

2025-11-07