Besides `stampToDate`, which filters can format time values into a specified date format?

Calendar 👁️ 61

In AnQi CMS template development, we often need to display time values stored in the database, such as the publication time and update time of articles, in the date and time format we expect.stampToDateThe filter is undoubtedly one of the most commonly used and powerful tools, which can flexibly convert Unix timestamps to various date formats.But in addition to this '万能药', Anqi CMS also provides other several highly efficient and practical time formatting methods, which have their own advantages in different scenarios and can help us handle the display of time data more finely and conveniently.


stampToDateTimestamp conversion is the foundation and versatile choice

First, let's confirm againstampToDatefunction, as it is the core tool for handling the common 10-digit Unix timestamp in databases. No matter where you arearchiveList/archiveDetailorcommentListObtained from the tagCreatedTimeorUpdatedTimeThis timestamp fieldstampToDateCan be converted into a readable date-time format.

Its usage is very intuitive, accepting two parameters: the timestamp value to be formatted, and a string defining the output format.

{# 假设 publishStamp 是一个10位的时间戳,例如 1609470335 #}
{% set publishStamp = 1609470335 %}

{# 格式化为 2021年06月30日 #}
<div>发布日期:{{stampToDate(publishStamp, "2006年01月02日")}}</div>
{# 格式化为 2021-06-30 15:04:05 #}
<div>发布时间:{{stampToDate(publishStamp, "2006-01-02 15:04:05")}}</div>

This filter is the preferred choice for processing database return timestamps, with the flexibility of customizing any output format.


Define the format directly within the tag: a quick and convenient option

For time fields like article publish time (CreatedTimeAnd update time(UpdatedTime) which are often displayed inarchiveDetailorarchiveListtags, Anqi CMS provides a more convenient formatting method: directly within the tag throughformatparameter.

This method eliminates the extra callstampToDatesteps, making the template code more concise and easy to read.

{# 在文档详情页,直接获取并格式化文章创建时间 #}
<div>
    文章发布于:{% archiveDetail with name="CreatedTime" format="2006年01月02日 15:04" %}
</div>

{# 在文档列表中,同样可以为每个文章项格式化时间 #}
{% archiveList archives with type="list" limit="5" %}
    {% for item in archives %}
        <div>
            {{item.Title}} - 发布于:{{item.CreatedTime|format("2006-01-02")}}
        </div>
    {% endfor %}
{% endarchiveList %}

As can be seen, when you accessarchiveListorarchiveDetaildirectlyitem.CreatedTimeoritem.UpdatedTimeif used in conjunction withformatA filter that automatically formats, with the same effect asstampToDateThis can improve development efficiency in many scenarios.


{% now %}Label: A powerful tool for getting the current time

Sometimes, what we need is not a specific time from the database, but the current server time when the web page is rendered, such as displaying the current year or the time to the second in the footer. At this time,{% now %}the tag comes into play.

It does not depend on any external variables, it is responsible for obtaining and formatting the server's real-time time.

{# 在页脚显示当前年份 #}
<div>&copy; {% now "2006" %} All Rights Reserved.</div>

{# 显示完整的当前日期和时间 #}
<div>当前服务器时间:{% now "2006-01-02 15:04:05" %}</div>

This tag is very useful for scenarios that require dynamic display of the current time.


|date(or)|time) Filter: for specific data types

In addition to the aforementioned methods, Anqi CMS also provides|date(or its alias|time)Filter. This filter is related tostampToDateslightly different, it requires the filtered object itself to be a Go languagetime.Timetype object, rather than a pure Unix timestamp.

If you are operating on a template that has already been parsed into Go languagetime.TimeA type variable (for example, some custom Go structures or time objects converted in other ways), then|datea filter is very applicable.

{# 假设 myGoTimeVar 是一个已知的Go time.Time类型变量 #}
<div>自定义时间:{{ myGoTimeVar|date:"2006年01月02日 星期一" }}</div>

{# myGoTimeVar|time 和 myGoTimeVar|date 效果一致 #}
<div>自定义时间:{{ myGoTimeVar|time:"2006/01/02 15:04" }}</div>

Although in most scenarios where timestamps are directly retrieved from databases,stampToDatemore common, but understanding|dateand|timeThe existence can help you choose more precise tools when dealing with pre-processed Go time objects.


The mystery of string formatting: Time layout in Go language

Whether it isstampToDateFilter, tag withinformatParameter, or{% now %}Tags and|dateFilters, they all follow the same time formatting rule: Go language reference time format.

Go language does not use format specifiers like "Y-m-d" as PHP does or "yyyy-MM-dd" as Java does, but uses a specific reference date2006-01-02 15:04:05.999999999 -0700 MST(usually abbreviated as2006-01-02 15:04:05) as the formatting template.

This means, you need to write the corresponding part of the reference date/time in the format string according to the date/time part you want to display:

  • Year:2006
  • Month:01(with leading zeros),1(without leading zero,)Jan(abbreviation,)January(full,)
  • Date:02(with)}

Related articles

How to combine custom parameters (such as "area", "house type") for multi-condition filtering on the article list page?

In AnQi CMS, to implement multi-condition filtering on the article list page, such as filtering based on custom parameters such as "area" and "house type", it requires us to cleverly combine the system-provided "content model" feature with the "document parameter filtering tag" ("archiveFilters") and "document list tag" ("archiveList") of the front-end template.The entire process can be divided into several core steps, let's take a detailed look together.### Core Function: Customize Content Model and Parameters Basic Implementation of Multi-Condition Filtering

2025-11-08

How to implement search through URL parameters (such as `q=keyword`) on the article list page and display the results?

It is crucial to provide users with a convenient and accurate content search experience in website content operation.When the number of articles on the website continues to grow, a practical search function can greatly enhance user satisfaction and the efficiency of content access.AnqiCMS as an efficient content management system provides a flexible way to implement the URL parameter search function for the article list page, allowing visitors to directly search and view related content by adding parameters such as `q=keywords` to the URL.

2025-11-08

How to dynamically add a website name suffix or custom delimiter in the page title (Title)?

The page title (Title) is the 'front door' of a website on the search engine results page. It not only can intuitively tell users the content of the page, but it is also an important indicator for search engines to evaluate the relevance and authority of the page.An optimized page title that can significantly improve click-through rates and the website's search engine ranking.In AnQiCMS, you can flexibly control the way page titles are generated, especially by dynamically adding the website name suffix and custom delimiters to achieve a unified brand image and better SEO effect.### AnQiCMS in TDK

2025-11-08

How to display a list of articles with pagination and customize the style and number of pagination navigation pages?

Today, with the increasing richness of website content, how to efficiently display a large number of articles while ensuring a smooth browsing experience is a problem that every website operator needs to consider.AnQiCMS (AnQiCMS) provides a powerful and flexible article list pagination feature, which not only helps to optimize the content presentation of the website, but also allows for better integration into the overall website design through custom styles and page number settings, enhancing the user interaction experience.Next, we will delve into how to implement pagination of the article list in Anqi CMS

2025-11-08

How to use a filter to truncate the specified character length of an article description or abstract and automatically add an ellipsis?

In website operation, the presentation of article description or abstract is crucial for user experience and Search Engine Optimization (SEO).A well-balanced, concise summary that not only attracts visitors to click but also helps search engines better understand the page content.However, manually controlling the length of each article summary is time-consuming and prone to errors.Fortunately, AnqiCMS provides powerful template filter functions that can help us automate this process, ensuring the uniformity and beauty of website content display.Why is it necessary to truncate article descriptions or summaries

2025-11-08

How to perform addition or other arithmetic operations on numbers or strings in the template?

In website template development, we often need to perform some basic arithmetic operations, such as calculating the sum, adjusting values, or comparing values based on specific conditions.AnQiCMS (AnQiCMS) boasts an efficient architecture based on the Go language and a flexible template engine inspired by the Django style, providing users with an intuitive and powerful way to perform arithmetic operations such as addition of numbers or strings in templates.

2025-11-08

How to set a default display value for a possibly empty variable, string, or object?

In website content management, the integrity and consistency of data are crucial.However, in actual operation, we often encounter situations where certain variables, strings, or objects may be empty.If the template does not handle these null values properly, the front-end page may appear blank, disordered, or even report errors, severely affecting user experience.AnQiCMS provides various flexible and powerful template tags and filters, helping us elegantly handle potential null values to ensure the stability and beauty of website content.The AnQi CMS template system borrows the syntax from Django

2025-11-08

How to use a filter to remove specific characters (such as spaces) from the beginning, end, or any position of a string?

String cleaning practical guide in AnQiCMS: Efficiently remove filters for specific characters During the operation of a website, we often encounter situations where we need to clean and format string data.Whether it's the extra spaces typed by users or redundant characters carried in when importing content from external sources, these subtle details may affect the neatness of website content and the user experience.AnQiCMS (AnQiCMS) provides a multifunctional and easy-to-use template filter that can help us easily deal with these string cleaning needs.

2025-11-08