What are the main differences between AnQiCMS's `date` and `stampToDate` filters when handling date data?

Calendar 👁️ 76

In AnQiCMS template development, the display of time data is an indispensable part, whether it is the publication time of articles, the update date of products, or the time records of user behavior, they all need to be presented to the user in a clear and readable manner. To meet different time processing needs, AnQiCMS provides two core filters:dateandstampToDateAlthough they can all help us format time, there is a fundamental difference in the data types they handle at the bottom level. Understanding these differences is crucial for avoiding template errors and efficient development.

AnQiCMS time data overview

In the AnQiCMS content management system, time data usually exists in two main forms: one is stored directly as a standard date and time objecttime.TimeType, this is the data structure used to represent time in Go language), another is stored in the form of Unix timestamp (Timestamp), which is usually an integer representing the number of seconds since 00:00:00 UTC on January 1, 1970.Understand the type of your data is the first step in choosing the correct filter.

dateFilter:time.TimeThe elegant presentation of objects

dateThe filter is used to process those that have been parsed as Go language primitivestime.TimeType of time data. Its main function is to output the structured date and time object in a specified format for easy understanding.

While usingdateWhen filtering, you just need totime.Timepass the variable of the type through the pipe|Connected todateApply the filter and provide a time formatting string that conforms to the Go language standard.

For example, suppose you have a variable namedcreateTimevariable, which is atime.TimeObject, if you want to format it as 'Year-Month-Day', you can use it like this:

{{ createTime|date:"2006-01-02" }}

The key point is:dateFilterStrict requirementsThe input data must betime.TimeType. If a timestamp is passed as an integer, it will not be recognized and may cause template rendering errors. In AnQiCMS,datethere is also an aliastime, and the function is withdatebeing exactly the same.

stampToDateFilter: Professional Timestamp Converter

withdateThe filter is different,stampToDateThe filter is specifically designed to handle Unix timestamps. Many built-in time fields in AnQiCMS, such as the document'sCreatedTime(creation time) andUpdatedTime(Update time), the user'sLastLogin(Last login time) etc., are stored in the form of 10-digit (or more) integer timestamps.

stampToDateThe usage of the filter is a bit different, it is more like a function call, you need to pass the timestamp variable as the first parameter, and the formatted string as the second parameter.

For example, if you want to list the articlesitem.CreatedTimeTo format the timestamp as “Year-Month-Day”, you need to do this:

{{ stampToDate(item.CreatedTime, "2006-01-02") }}

This filter will first convert the integer timestamp totime.TimeAn object is then formatted, so that it can perfectly handle the display of most built-in time fields in AnQiCMS.

Guide to Core Differences and Selection

Now, let's clarify.dateandstampToDateThe core difference, so you can make the correct choice during development:

  1. Input data type:

    • dateFilter: Expected to receiveGo languagetime.Timeobject.
    • stampToDateFilter: Expected to receiveUnix timestamp in integer form.
  2. Applicable scenarios:

    • UsedateFilterWhen your template variable is explicitly atime.Timeobject (for example, you have already processed the time data in your Go backend code intotime.Timeand passed it to the template).
    • UsestampToDateFilterWhen your template variable is an integer timestamp, this is very common in AnQiCMS, such asitem.CreatedTime/archive.UpdatedTimeetc.

In simple terms, if you are unsure but the variable name suggests that this is a "timestamp" (for example, with "Stamp", "Time", etc.), thenstampToDateIt is often a safer and more correct choice.

The mystery of Golang time formatting strings.

whether it isdateOrstampToDateThey follow the Go language's unique time formatting rules. Go language does not useY-m-d H:i:sthis common placeholder, but uses a fixed reference time2006-01-02 15:04:05.999999999 -0700 MSTDefine the format. You need to remember this magical 'reference time' and use it to build the format you want.

For example:

  • "2006-01-02"Indicates年-月-日
  • "15:04:05"Indicates时:分:秒
  • "2006年01月02日 15时04分"Indicates2006年01月02日 15时04分

Mastery of this rule allows you to control the time display format at will.

**Practice and Precautions

  • **Specify the data source clearly.

Related articles

How to format a 10-digit timestamp retrieved from the database into a custom date-time format in AnQiCMS template?

In website content display, we often encounter time data in a long string of numbers from the database, such as `1678886400`. This string of numbers, which is what we commonly call a timestamp, is very convenient for computers, but it seems cold and difficult to understand for users visiting websites.Fortunately, AnQi CMS provides us with a very convenient feature that can easily convert these original 10-digit timestamps into the date and time format we are accustomed to reading.

2025-11-07

How to set a default friendly display value for a possibly empty variable in AnQiCMS template?

When building website templates, we often encounter situations where a variable may be empty in the template due to unentered data, optional fields left blank, or specific business logic.If these empty variables are not processed, the front-end page of the website may appear blank areas, display unfriendly placeholders, and even cause layout errors, thereby affecting user experience and the professionalism of the website.

2025-11-07

What is the correct syntax for using the `{{ obj|filter__name:param }}` filter in AnQiCMS templates?

AnQiCMS (AnQiCMS) provides strong support for content operations with its efficient and flexible features.In daily content display, we often need to process and format data in a fine-grained manner to ensure that information is presented to visitors in a **state**.At this moment, the 'filter' in the template has become an indispensable tool for us. Today, let's delve into the correct syntax for using filters in Anqi CMS templates: `{{ obj|filter_name:param }}`.Understand and master it

2025-11-07

How to quickly remove spaces or specific characters from both ends, left or right of a string in AnQiCMS template?

In content management, we often encounter situations where there are excessive spaces or unwanted specific characters at both ends of strings, which not only affects the display aesthetics of the content, but also sometimes causes unnecessary interference to data processing or search engine optimization (SEO).AnQiCMS uses a template engine syntax similar to Django, providing us with concise and efficient filters (Filters) to easily solve these problems.This article will introduce how to quickly remove spaces or specific characters from both ends, left, or right of a string in the AnQiCMS template.

2025-11-07

How to always display product prices (floating point numbers) with two decimal places in AnQiCMS templates?

When operating a product display website, you may often encounter situations where you need to display product prices accurately.A professional and user-friendly website often requires a unified display format for product prices, such as always retaining two decimal places, even for integer prices, which are automatically filled with `.00`. AnQiCMS with its flexible template system makes these details handling very convenient.

2025-11-07

How does AnQiCMS automatically identify URLs or email addresses in text and convert them into clickable hyperlinks?

In the daily operation of websites, we often need to add various links in article content, such as URLs pointing to external resources, or email addresses for easy reader contact.Manually adding hyperlinks one by one is not only inefficient but also prone to errors.Fortunately, AnQiCMS provides very practical features that can intelligently identify URLs and email addresses in text and automatically convert them into clickable hyperlinks, greatly enhancing the efficiency and user experience of content editing.

2025-11-07

How to control the truncation length of the hyperlink text when using the AnQiCMS `urlizetrunc` filter?

In website content operation, we often need to display various hyperlinks in articles, comments, or list pages.These links may be pointing to other content within the site, external resources, or the contact email of the user.However, some excessively long links may not only destroy the page layout, affect the aesthetics, but may also reduce the user's reading experience.Especially in limited display space, long URLs can make content look disorganized.

2025-11-07

Why does the AnQiCMS template default to escaping HTML code? How can HTML content be safely output?

When using AnQiCMS for template development, we may notice an interesting phenomenon: sometimes, the HTML code directly output in the template, such as a `<div>` tag, is not parsed by the browser into a visible area as expected, but is displayed exactly as it is, showing `&lt;div &gt; `such characters. This may be confusing, why does AnQiCMS default to escaping HTML code?How can we safely output the HTML content we want?--- ### One

2025-11-07