In Anqi CMS template, the 'format' parameter of `stampToDate` should follow which time formatting standard of the Go language?

Calendar 👁️ 63

In AnQiCMS templatestampToDateThe mystery of time formatting: Unveiling the Go language time standard

As an experienced website operations expert, I know that the flexible display of timestamps in content management systems is crucial for the freshness of website information and user experience.AnQiCMS (AnQiCMS) benefits from the powerful genetics of the Go language, inheriting the unique elegance and efficiency of Go language in template processing.Today, let's delve deeply into a commonly used and powerful time processing function in the Anqi CMS template——stampToDateEspecially the unique 'format' parameter followed by the Go language time formatting standard.

stampToDateThe format source: Reference time in Go language

To put it bluntly, in Anqi CMS templatestampToDateThe format parameter of the function followsThe time formatting standard built into Go languagePeriod, unlike that commonly used in other programming languagesY-m-d H:i:soryyyy-MM-dd HH:mm:ssThis symbol placeholder is different, Go language adopts a very unique and intuitive“Reference time”method to define date and time formats.

This "reference time" is a fixed date and time:2006-01-02 15:04:05. At first glance, this string of numbers may be perplexing, why exactly this time?In fact, this is not randomly selected, but carefully chosen by the Go language designer, each number corresponds to a date and time unit, and has uniqueness, which is convenient for developers to remember and use:

  • 2006Represents the year (Year)
  • 01Represents the month (Month), lowercasejanorJanAlso
  • 02Represents the day of the month (Day of month)
  • 15Represents an hour (Hour, 24-hour format), i.e., 3 PM
  • 04Represents a minute (Minute)
  • 05Represents a second (Second)

In simple terms, when you want to format a timestamp into a specific format, you just need to 'write' the expected output format using this "reference time".The internal mechanism of Go language will intelligently parse the date and time unit you want to express based on the "reference time" pattern and apply it to the actual timestamp.

Specific practice of formatting

Understood the reference time of Go language,stampToDateThe usage becomes clear. In Anqi CMS template, we usually get such as documents.CreatedTime(Creation time) orUpdatedTime(Update time) Such a Unix timestamp. These timestamps are usually 10 or 13-digit integers.stampToDateThe function expects a 10-digit timestamp and formats it.

For example, if we have a timestamp1609470335Corresponding to2021-01-01 15:05:35), we want to display it in a different format:

  • Only show the year, month, and date (for example:2021-01-01)You just need to use the2006-01-02to represent:

    {{ stampToDate(publishStamp, "2006-01-02") }}
    {# 结果可能为: 2021-01-01 #}
    
  • Display the year, month, day, and minute accuracy (for example:2021年01月01日 15:05)Combine the corresponding parts of the reference time:

    {{ stampToDate(publishStamp, "2006年01月02日 15:04") }}
    {# 结果可能为: 2021年01月01日 15:05 #}
    

    Please note that the '15:04' exactly matches the hour and minute in the reference time.

  • Display the full timestamp format (for example:)2021-01-01 15:05:35)Use the full reference time format:

    {{ stampToDate(publishStamp, "2006-01-02 15:04:05") }}
    {# 结果可能为: 2021-01-01 15:05:35 #}
    

The formatting style of Go language is considered 'magic' because it is more flexible than the traditional placeholder system.You can almost arrange these numbers in any way you want, Go will understand your intention.For example, if you want to display as01/01/21, you can use01/02/06as the format string.

In the actual application of the Anqi CMS template

In the AnQi CMS template,stampToDateFunctions are often combined with the document'sCreatedTimeandUpdatedTimefields. These fields are usually stored in the form of Unix timestamps. For example, when outputting an article list in a loop:

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

Here, item.CreatedTimeanditem.UpdatedTimeis passed instampToDateThe timestamp, followed by the string defines their display style on the page.This way allows template designers to very finely control the display of time, meeting different design needs and user preferences.

It is worth mentioning that Anqi CMS also providesdateA filter, but it is usually used to process Go language primitivestime.Timetype objects, butstampToDateis specifically used to handle Unix timestamps, which is an important distinction in template development.

Mastered the reference time format of Go language, you can handle various time display needs effortlessly in the AnQi CMS template, so that the content of your website is always presented in the most user-friendly and business logic manner.This unique and efficient mechanism is exactly the charm of Anqi CMS as a Go language CMS.


Frequently Asked Questions (FAQ)

  1. Ask: Why is the time formatting standard of the Go language not more common?Y-m-d H:i:sIn this form?Answer: One of the design philosophies of the Go language is simplicity and clarity. Traditional symbol placeholders (such asYRepresents the year,mRepresenting the month may be ambiguous or may require additional reference tables. Go language adopts the 'reference time'2006-01-02 15:04:05Each number has a unique and intuitive meaning, directly used as a template for format strings, making the definition of the format clearer and easier to remember (once you understand this set of "magic numbers").It avoids memorizing a set of new symbols but directly simulates the format you want to output.

  2. Ask: If I get a timestamp that is not 10 digits long (for example, 13 digits containing milliseconds),stampToDateCan I still use it?Answer:stampToDateThe function expects a 10-digit Unix timestamp (second-level timestamp).If your timestamp is 13 digits (millisecond level), you need to convert it to 10 digits first, that is, divide it by 1000.For example, simple mathematical operations can be performed in the template{{ stampToDate(item.CreatedTime / 1000, "2006-01-02") }}In actual development, it is more recommended to uniformly process the timestamp to 10 digits in the backend or business logic layer before passing it to the template to keep the template clean.

  3. Question: Besides,stampToDateIs there another way to format time in AnQi CMS template?Yes, AnQi CMS, which is developed in Go language, also supports native Go language types.time.TimeValuesdateFilter. However,stampToDateis specifically designed to handle common Unix timestamps. If you operate directly ontime.Timetype variables, you can use{{ myTimeVar|date:"2006-01-02" }}This syntax. But for fields like timestamps typically obtained from databases or APIs (such asCreatedTime)stampToDateIt is a more direct and recommended choice.

Related articles

How to format `CreatedTime` obtained from the `archiveDetail` tag using `stampToDate` to the "YYYY-MM-DD" format?

## Unlock the AnQi CMS Time Magic: How to beautifully format `CreatedTime` to "YYYY-MM-DD" As an experienced website operations expert, I am well aware of the importance of content timeliness and display methods for user experience.In AnQiCMS (AnQiCMS) such a powerful content management system based on Go language, we can not only efficiently manage content, but also convert technical information into user-friendly display through flexible template tags.Today, let's talk about a common demand in operation

2025-11-07

How to use the `stampToDate` tag to display the publication date of each document on the article list page?

## Enabling content timeliness: The efficient way to display publication dates on the article list page of AnQi CMS In today's fast-paced digital world, the timeliness of content is crucial for the attractiveness of a website and the user experience.Whether it is news reports, technical articles, or product updates, clearly displaying the publication date can help readers quickly judge the value of the information and enhance the professionalism of the website.

2025-11-07

In AnQi CMS, how does the `stampToDate` tag convert a 10-digit timestamp to a common date format?

## Time Magician: How to easily handle 10-digit timestamps in AnQi CMS `stampToDate` tag In the world of content operation, the way time is presented often directly affects users' understanding and perception of the content.A clear and readable date format that allows users to quickly obtain key information while browsing articles, products, or comments, enhancing the overall browsing experience.However, at the bottom level of databases and systems, time is often represented by a series of 'mysterious' numbers - a 10-digit timestamp (Unix timestamp).

2025-11-07

How to prevent whitespace issues during the development stage of AnQiCMS templates?

How to elegantly prevent whitespace issues in AnQiCMS template development?In the era of refined website operation, every detail may affect user experience and search engine performance.For users and developers of AnQiCMS (AnQi CMS) who pursue efficiency, security, and customization, the quality of the HTML code output by the template is crucial.AnQi CMS, this enterprise-level content management system based on Go language has won the favor of many users with its powerful functions and flexible template engine.

2025-11-07

How to use `stampToDate` to format the `UpdatedTime` of the document into a complete date and time that includes hours, minutes, and seconds?

As an experienced website operation expert, I am well aware that the details of content presentation are crucial for user experience and information delivery.AnQiCMS (AnQiCMS) with its flexible and powerful template engine, provides us with great freedom.Today, let's delve deeply into a common and practical need in content operation: how to use the `stampToDate` function to format the `UpdatedTime` of a document into a complete date and time that includes hours, minutes, and seconds.

2025-11-07

Can the `stampToDate` tag customize the display of the English abbreviation of the month, such as "Jan", "Feb"?

## The `stampToDate` tag of AnQi CMS: Flexible time format customization, easily display month abbreviations As an experienced website operations expert, I know that precision and beauty are equally important in content display.AnQiCMS (AnQiCMS) provides strong support for content operation with its efficient and customizable features.In everyday operations, we often need to convert database timestamps to user-friendly date formats.Aqit CMS provides a very practical template tag - `stampToDate`.

2025-11-07

How to extract only the year from the timestamp using `stampToDate` in the AnQi CMS template?

As an experienced website operations expert, I know that it is crucial to efficiently and flexibly display information in daily content management.AnQiCMS (AnQiCMS) leverages the efficient features of the Go language and the template syntax of Django style, providing us with powerful content display capabilities.Today, we will focus on a very practical little trick in the Anq CMS template: how to extract only the year from the timestamp using the `stampToDate` tag.

2025-11-07

Does the `stampToDate` tag support formatting timestamps into the international date format "MM/DD/YYYY"?

In the daily operation of AnQiCMS, the formatting of the Unix timestamp is a common requirement, especially when our website content needs to be targeted at international users, the flexibility of the date display format is particularly important.Today, let's delve deep into the `stampToDate` tag in AnQiCMS to see if it can meet our needs to format timestamps into the international date format of "MM/DD/YYYY".

2025-11-07