How to format Unix timestamp to 'YYYY-MM-DD HH:MM' and other localized date strings using the `stampToDate` filter in AnQi CMS?

Calendar 👁️ 68

In website content management, the display of date and time information is ubiquitous.No matter the publication date of the article, the listing date of the product, or the submission time of the comments, a clear and readable date format is crucial for enhancing user experience.AnQiCMS (AnQiCMS) understands this need and provides powerful template tags and filters, among whichstampToDateThe filter is a tool that converts the original Unix timestamp into a localized date string that we are familiar with.

This article will delve deeper intostampToDateThe usage of the filter, which helps you easily implement various custom date and time formats in Anqin CMS templates.

Understanding Unix timestamps in Anqin CMS

In Anqi CMS, many date and time information stored in the database, such as the creation time of documentsCreatedTimeor update timeUpdatedTimeIt usually exists in the form of a Unix timestamp. A Unix timestamp is an integer representing the number of seconds elapsed since 00:00:00 UTC on January 1, 1970, also known as the Unix epoch.

Display such timestamps directly in the template, for example1609470335It is difficult for ordinary users to understand. In order to provide a better reading experience, we need to format it into a localized string such as "2021-01-01 12:25".

Core Tool:stampToDateFilter

The Anqi CMS template engine has borrowed the syntax of Django and providedstampToDateThis convenient filter is specifically used to format Unix timestamps into readable date and time strings.

Basic syntax:

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

Among them:

  • 时间戳: is typically what you get from a data object likeitem.CreatedTime)Retrieved 10-digit Unix timestamp.
  • "格式"This is a string that defines the specific format you want the date and time to be displayed in. It is especially important to note that this "format" is not the common one.Y-m-d H:i:sThis pattern, instead, adopts the unique "reference time" mechanism of the Go language.

Master the GoLang time format secret.

Go language uses a fixed reference point to define time formats, representing each time unit with this reference time point:2006年01月02日 15点04分05秒.

This means, when you want to display the year, you should use2006; When you want to display the month, you should use01(representing January); When you want to display the date, you should use02 Representing the second, and so on. The following are common correspondences:

  • 2006-> Year (YYYY)
  • 01or1-> Month (MM/M)
  • 02or2-> Day (DD/D)
  • 15or3-> Hour (24-hour format HH/H) or (12-hour format hh/h)
  • 04-> Minute (MM)
  • 05-> Second (SS)
  • Mon-> Day of the week (e.g., Mon, Tue, Wed)
  • Monday-> Full day of the week (e.g., Monday, Tuesday)
  • Jan-> Abbreviated English month names (e.g., Jan, Feb)
  • January-> Full English months (e.g., January, February)

By combining these reference time elements, you can almost define any desired date and time format.

Common formatting examples and applications

Suppose we have a Unix timestamp1672502400It corresponds to2023年01月01日 00:00:00. Let's see how to usestampToDateThe filter formats it into various common localized strings.

  1. “YYYY-MM-DD HH:MM”(such as: January 1, 2023 00:00)

    {{ stampToDate(1672502400, "2006年01月02日 15:04") }}
    

    output:2023年01月01日 00:00

  2. “YYYY-MM-DD”(such as: 2023-01-01)

    {{ stampToDate(1672502400, "2006-01-02") }}
    

    output:2023-01-01

  3. “MM/DD HH:MM”(like: 01/01 00:00)

    {{ stampToDate(1672502400, "01/02 15:04") }}
    

    output:01/01 00:00

  4. “HH:MM:SS”(like: 00:00:00)

    {{ stampToDate(1672502400, "15:04:05") }}
    

    output:00:00:00

  5. “YYYY/MM/DD Monday”(such as: 2023/01/01 Sunday)

    {{ stampToDate(1672502400, "2006/01/02 星期Monday") }}
    

    output:2023/01/01 星期Sunday(note the Go language's)MondayThe complete day of the week is output here, it will be Sunday because 1672502400 is Sunday

Examples of actual application scenarios

In the template design of AnQi CMS,stampToDateFilters are often usedarchiveList(Document list),archiveDetail(Document details) andcommentListin tags such as (comment list) and others.

For example, display the publication time of each article in a document list:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <div class="article-item">
        <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
        <p>发布时间:
            <time datetime="{{ stampToDate(item.CreatedTime, "2006-01-02T15:04:05") }}">
                {{ stampToDate(item.CreatedTime, "2006年01月02日 15:04") }}
            </time>
        </p>
        <p>{{ item.Description }}</p>
    </div>
    {% empty %}
    <p>暂时没有文章。</p>
    {% endfor %}
{% endarchiveList %}

In this example,item.CreatedTimeThe obtained is a Unix timestamp, bystampToDateFilter, we have formatted it into a user-friendly "year-month-date hour:minute" format, and also<time>label'sdatetimeThe property outputs machine-readable ISO 8601 format, balancing user experience and SEO.

Related articles

In addition to `stampToDate`, how can the `date` filter of Anqi CMS implement custom date and time formatting when handling `time.Time` type?

In Anqi CMS, the display of dates and times often needs to be adjusted flexibly according to the actual needs of the website.We all know that the `stampToDate` filter is very convenient for handling Unix timestamps, as it can easily convert a string of numeric timestamps into the date format we need.But sometimes, the date value we handle in the template may already be a `time.Time` type object in Go language, rather than the original timestamp.In this case, Anqi CMS provides another powerful tool, that is `date`

2025-11-08

How to use the `divisibleby` filter in AnQi CMS template to achieve alternate row coloring or group output every N elements?

In website operation, a well-designed page layout and content display can significantly improve user experience and readability.Especially in scenarios with a long list of items, if all entries are presented in the same style, it is easy to cause visual fatigue.At this time, alternating line colors or grouping by a specific quantity can well solve these problems.

2025-11-08

How to use the `length_is` filter in an Anqie CMS template to validate the length of user input or data lists and return a boolean value for conditional rendering?

In Anqi CMS template development, flexibly controlling the display method of content is the key to improving website user experience.Among them, the `length_is` filter is a very practical tool that can help us easily check the length of user input or data lists in templates and conditionally render based on the check results.The core function of the `length_is` filter is to determine whether the length of a variable (whether it is a string, array, or mapping) is equal to a predefined value.It does not directly return the specific length value

2025-11-08

The `length` filter calculates the length of Chinese string in an AnQi CMS template, is it counted by bytes or characters? How does it affect the output?

In AnQi CMS template development, dealing with string length is a common requirement.When dealing with multilingual content, especially when it includes Chinese characters, how a string's length is calculated in bytes or characters can directly affect the accuracy of template output and the presentation of the content.The `length` filter used in the Anqi CMS template is designed to solve this problem.It counts the length of the string by characters rather than by bytes.This means, whether it is an English letter

2025-11-08

How to safely use the `truncatechars_html` filter in Anqi CMS template to truncate HTML rich text content and automatically close tags?

In website content operation, we often need to display the partial content of articles, products, or single pages on list pages, abstract areas, or specific blocks.This content is often rich text that includes HTML tags. Simply truncating by character count may cause the HTML tags to be cut off, thereby破坏页面布局和显示效果.For example, a `<p>This is a paragraph<strong id=“test”>bold</span>` content that is abruptly truncated may cause the page to display unclosed tags.

2025-11-08

What are the differences in the truncation logic of the `truncatewords` and `truncatechars` filters when truncating the abstract of an AnQi CMS article?

In Anqi CMS, in order to display the article summary on the list page or preview area, we often need to truncate the article content.At this time, the `truncatewords` and `truncatechars` filters come into play.They all can help us to shorten long content, but there are significant differences in the truncation logic between them, especially in handling Chinese and English characters and words, where their performance is even more disparate.Understanding these differences can help us better control the presentation of the summary.

2025-11-08

How to use `upper`, `lower`, `capfirst`, and `title` filters to unify the English title case format of the CMS frontend page?

In website operation, the professionalism and consistency of content display are crucial for improving user experience and brand image.Especially when dealing with English titles, consistent capitalization not only makes the page look neater, but also indirectly affects the readability of the content.AnQiCMS (AnQiCMS) relies on the powerful features of the Django template engine and provides several very practical filters to help us easily format the case of English titles on the front-end page.

2025-11-08

How to efficiently remove specific punctuation marks or spaces from a string using the `cut` filter in the Anqi CMS template to clean up the output content?

In the daily operation of AnQi CMS, we often encounter situations where we need to refine the content output by templates.To make the page display cleaner, improve the user reading experience, or generate URLs that are more beneficial for SEO, removing unnecessary punctuation or extra spaces from strings is a very practical skill.The powerful template engine of Anqi CMS provides a rich set of filters to help us achieve these goals, among which, the `cut` filter is a simple yet extremely efficient tool.### `cut` filter

2025-11-08