What are the similarities and differences between the `stampToDate` and `date` filters in handling time formatting and their applicable scenarios?

Calendar 👁️ 68

In AnQi CMS template development, we often need to display time data in a user-friendly format. The system provides two very practical tools for handling time:stampToDatefunctions anddateFilter. Although they all help us format time, there are some key similarities and differences as well as applicable scenarios. Understanding these can make our template development more efficient and accurate.

stampToDate: A master of handling timestamps

In Anqi CMS, when you retrieve content from the database (such as the publication time, update time) of articles, these time fields are usually stored in the form of Unix timestamps (a number representing the seconds or milliseconds since January 1, 1970, 00:00:00 UTC). For example, when you are usingarchiveListorarchiveDetailtags in the template,item.CreatedTimeoritem.UpdatedTimeIt returns a 10-digit integer timestamp for the field.

stampToDateThe function is specifically designed to handle such timestamps. Its usage is very intuitive, simply pass the timestamp as the first parameter, and the expected format string as the second parameter.

Usage scenario: stampToDateIt is what you process in the template.The time field directly obtained from the database.The preferred. For example, you need to display the publication date of the article:

{# 假设 item.CreatedTime 是一个10位时间戳,例如 1678886400 #}
<div>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</div>
{# 显示结果可能是:发布日期:2023-03-15 #}

<div>更新时间:{{stampToDate(item.UpdatedTime, "2006年01月02日 15:04:05")}}</div>
{# 显示结果可能是:更新时间:2023年03月15日 10:30:00 #}

stampToDateThe strength lies in its direct reception of timestamps, without any preprocessing, it can be converted to any date or time format you want.

dateFilter: for Go languagetime.Time精细控制的对象

withstampToDatedifferent,dateFilter requires its input to be a native Go languagetime.Timetype object. If you try to pass a pure integer timestamp directly todateFilter, an error will occur during template parsing due to type mismatch.

Usage scenario: dateThe filter is applicable toAre you sure the variable is alreadytime.TimeTypeThis usually means that the data has already been processed and transformed into a specific format before being passed into the template in the backend Go language code.time.TimeAn object. Or, certain special system built-in variables may be provided in the form oftime.Time. In the normal template development of Anqi CMS, filters are used directly todatefilter.item.CreatedTimeSuch a database timestamp field is not common, as these fields are normally exposed to the template in the form of integer timestamps.

{# 假设 customTimeVar 是一个后端传递过来的 Go time.Time 对象 #}
<div>自定义事件:{{ customTimeVar|date:"2006/01/02 15:04" }}</div>
{# 如果 customTimeVar 是 2023年3月15日10点30分,显示结果可能是:自定义事件:2023/03/15 10:30 #}

It is worth noting that,dateThe filter has an aliastimeBoth have the same usage method and effect.

Common formatting standard: Go language time layout

whether it isstampToDateOrdateFilters that follow the Go language's time formatting rules. This rule may look particularly unusual at first glance, as it is not common.Y-m-d H:i:sInstead, it uses aspecific reference time:Mon Jan 2 15:04:05 MST 2006. You need to use the corresponding number or word from this reference time to represent the date/time part you want.

For example:

  • Year:2006
  • Month:01(number) orJan(abbreviation) orJanuary(full name)
  • Date:02
  • Hour:15(24-hour format) or03(12-hour format)
  • Minute:04
  • Seconds:05

This means, if you want to display "year-month-date", the format string should be"2006-01-02"; If you want to display "year/month/date time:minute", the format string is"2006/01/02 15:04".

Which filter do you want to choose?

In short:

  • When you are dealing with timestamp fields directly from the database (such asitem.CreatedTime),Please usestampToDate.This is the most common and direct way to handle time in your AnQi CMS template.
  • When you are sure that a variable is of Go languagetime.Timetype object, please usedatefilter.This usually happens in some advanced cases where the backend has already preprocessed the data. If you are not sure about the variable type, it is best to usestampToDatetry, to avoid unnecessary template rendering errors.

Remember this principle, and you will be more at ease in handling various time display needs in Anqi CMS.


Frequently Asked Questions (FAQ)

1. Why do I usedateFiltering processitem.CreatedTimewill it report an error?

item.CreatedTime(as well asUpdatedTimeIn an Anqi CMS template, it usually exists in the form of an integer timestamp instead of Go languagetime.Timetype object.dateThe filter strictly requires that the input must betime.TimeType. Therefore, when you pass a timestamp directly todatethe filter, an error will occur due to type mismatch. The correct approach is to usestampToDateA function that is specifically designed to handle timestamps.

2. Why does Go language use time formatting2006-01-02 15:04:05such numbers instead ofY-m-d?

This is a feature of the Go language design. It does not use symbols (such asYrepresenting the year) to define the format, but instead provides a specific reference time:Mon Jan 2 15:04:05 MST 2006. You need to mimic the style of the corresponding part of this reference time to build your format string. For example, if you want to display a four-digit year, write2006; If you want to display a two-digit month, you write01This approach may seem unique at first glance, but once familiar, you will find it very flexible and not easily confused.

3. If my timestamp is not a standard 10-digit integer (second level),stampToDateCan it be handled?

Of Security CMSstampToDateThe function is designed to handle 10-digit timestamps (second-level) as described in the documentation. If your timestamp is 13 digits (millisecond-level), you may need to process it on the backend first or perform some mathematical operations (such as dividing by 1000) to convert it to a second-level timestamp before passing it tostampToDateHowever, the best practice is to ensure that the time data is stored and passed to the template in a consistent second-level timestamp format to avoid unnecessary complexity.

Related articles

How to format a Unix timestamp into a readable date and time string?

In website content management, the way time is presented is crucial to user experience.Although the system may prefer a unified and efficient Unix timestamp format for background data processing, for visitors, a string of random numbers is obviously not as intuitive and easy to understand as AnQi CMS knows this and provides a simple and powerful tool to solve this problem.### Unix timestamp: The 'time language' in the database Unix timestamp, in short

2025-11-08

Can the `divisibleby` filter be used to implement alternating row colors or other conditional styles in a loop?

In the daily operation of website content, how to make the list data more readable and visually attractive is a key factor in improving user experience.AnQiCMS (AnQiCMS) offers a flexible template engine, providing rich possibilities for content display.Today, let's talk about a very practical template filter——`divisibleby`, and see how it helps us achieve alternate row coloring or other conditional styles in loops.## Get to know the `divisibleby` filter AnQi CMS template system

2025-11-08

How to determine if a number can be evenly divided by another number in AnQiCMS templates?

In web content display, we often encounter situations where we need to adjust the layout or display logic of content based on the characteristics of some numbers.For example, we may need to insert an advertisement every few articles, or make even and odd rows of the table display different background colors, or perform special operations when the list loops to a specific position.In the AnQiCMS template system, based on the Django template engine syntax, it provides a very practical filter that can easily meet this requirement.This filter is `divisibleby`

2025-11-08

What will the `get_digit` filter return when processing non-numeric strings?

In AnQiCMS template development, we often use various filters to process data, where the `get_digit` filter is a convenient tool for extracting specific digits from numbers.However, when we turn our attention to a more common but potentially misunderstood scenario - that is, when the `get_digit` filter encounters a **non-numeric string**, its behavior becomes less obvious.Let's review the performance of the `get_digit` filter when dealing with pure numbers.When the data is of a standard numeric type

2025-11-08

How to truncate a long string and automatically add an ellipsis (...)?

In website operation, we often encounter situations where we need to display a piece of text, but we cannot let it be too long to avoid affecting the page layout or reading experience.Whether it is the title, abstract, or product description of an article, if the content exceeds the expected length, the usual practice is to truncate a part of it and add an ellipsis at the end to indicate that the content has not yet ended.For AnQiCMS users, achieving such an effect is not complicated, thanks to its flexible and powerful template engine, we have a variety of built-in filters (Filters) that can easily handle it

2025-11-08

How does the `truncatechars_html` filter safely truncate HTML content without breaking the tag structure?

In website operation, we often need to display an abstract of a large amount of content on a page, such as the article list on the homepage, a brief introduction on the product details page, or recommended content for a module.These summaries must be able to attract readers to click and maintain the neat and beautiful layout of the page.However, when the content itself contains rich HTML formatting (such as bold, italic, images, links, etc.), simply truncating the character length often leads to a headache: the HTML tag structure is destroyed, causing the page to display incorrectly and even affecting the overall style.Imagine

2025-11-08

How to convert the first letter or the first letter of each word in an English string to uppercase in AnQiCMS?

In daily website content management, we often need to finely control the display format of English strings, such as capitalizing the first letter of the article title or making each word of the product name start with uppercase to enhance the professionalism and unity of the content.AnQiCMS (AnQiCMS) fully understands the importance of these subtle details to the website's image, and therefore provides convenient and powerful string processing functions in template design, allowing you to easily meet these formatting needs.The Anqi CMS uses a template engine syntax similar to Django

2025-11-08

What are the limitations of the `lower` and `upper` filters when dealing with case conversion (such as Chinese)?

In AnQiCMS template development, the `lower` and `upper` filters are commonly used tools for handling text case conversion.They are designed to help us quickly standardize the display of text, such as converting the irregular content entered by users to lowercase or uppercase to maintain consistent page style or meet certain data processing requirements.However, when using these convenient filters, we may encounter some "edge" cases that they cannot handle, especially when it comes to non-English characters, such as Chinese.### `lower` and `upper`

2025-11-08