How to combine the `add` filter with the `stampToDate` function to concatenate a formatted date and time string?

Calendar 👁️ 70

When managing content in AnQi CMS, we often need to display dates and times in a specific format.The system provides very convenient template tags and filters to handle these requirements.addHow to combine the filter withstampToDateCombine functions to concatenate a formatted date-time string, making our content display more flexible and diverse.

Get to knowstampToDateFunction: Format timestamp

First, let's reviewstampToDatefunction.This function is the core tool for handling timestamps in the Anqi CMS template.It receives a Unix timestamp (usually 10 digits) and a format string, and then outputs the date and time according to the specified format.

This format string is quite special, it is not the common oneY-m-dThis placeholder is based on a fixed reference time:2006-01-02 15:04:05This means you want to output which part, use the corresponding number in the reference time to represent it. For example:

  • If you want to display the year (four digits), use2006.
  • If you want to display the month (two digits), use01.
  • If you want to display the hour (24-hour format), use15.

For example, we have a timestamp stored in the creation of an articlearchive.CreatedTimeIn Chinese, we want to display in the format of "Year-Month-Date", which can be written as:

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

This will output something similar2023-10-26The result. If you want to display to the second, it can be written as:

{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04:05") }}

The output may be2023-10-26 10:30:00.

UnderstandaddFilter: Concatenate your content

Next isaddThe filter is intuitive and powerful in its function.addThe filter can be used to perform an "addition" operation on two values. For numbers, it performs arithmetic addition; while when dealing with strings, it becomes a tool for concatenating text.

For example, if you want to concatenate two strings, you can do it like this:

{{ "欢迎使用"|add:"安企CMS" }}

This will display欢迎使用安企CMS. If the first value is a string, then subsequent throughaddThe value being connected, even if it is a number, will be automatically converted to a string for concatenation.

Combined with: Concatenate formatted date and time strings

Now, let's combine these two tools.Imagine, we hope to display the publication time of an article on the page, and add a Chinese prompt like 'Publication time:'.

First, we usestampToDateThe function will format the timestamp into the date string we want, such as"2006年01月02日". The result of this function is a string. Then, we can use this formatted date string asaddThe second parameter of the filter, while taking 'Publish time:' as the first parameter.

The complete usage looks like this:

{{ "发布时间:"|add:stampToDate(archive.CreatedTime, "2006年01月02日") }}

Ifarchive.CreatedTimeThe corresponding time is2023年10月26日Then the display result of the page will be:发布时间:2023年10月26日.

We can further complicate things, for example, to display "The article was published on: YYYY-MM-DD, the specific time is: HH:mm":

{{ "文章发布于:"
   |add:stampToDate(archive.CreatedTime, "2006年01月02日")
   |add:",具体时间是:"
   |add:stampToDate(archive.CreatedTime, "15时04分") }}

This code first concatenates the string "Article published on:" with the formatted date string, then concatenates the result with ", the specific time is:" and finally concatenates it with the formatted time string.This can generate a very personalized date and time display.

This combination of flexibility can be used in many scenarios. Whether it is to add an accurate release time to news reports or to note the last modification date for product updates, throughstampToDateandaddThe clever combination of filters, you can easily achieve it.

Frequently Asked Questions (FAQ)

1.stampToDateWhy are the format strings in the function so strange? It's fixed.2006-01-02 15:04:05?

Yes,stampToDateThe function adopts a unique way of defining time formats in Go language. It does not useY-m-dthis type of placeholder, but uses a fixed reference time (2006年1月2日 15点04分05秒 -0700 MSTThe template. You need to display which part of the time information, use the corresponding number or character from the reference time to replace it. For example, to display the year, use the number in the reference time that corresponds to the year.2006This provides very fine control capabilities, but it requires some adaptation when first encountered.

2.addHow many strings can the filter concatenate?

addThe filter can be chained to perform multiple concatenation operations. You can use multiple filters in a single expression.|add:As shown in the above example, concatenate multiple string fragments one by one to build the complete text you need.

3. If my timestamp is not a 10-digit number (for example, a 13-digit millisecond timestamp in JavaScript),stampToDateCan I still use it?

stampToDateA function usually expects to receive a standard 10-digit Unix timestamp. If your timestamp is 13 digits long in milliseconds, you may need to pass instampToDateBefore, divide it by 1000 (for example, using|divide:1000A filter, if the AnQiCMS template engine supports it, converts it to a timestamp in seconds. Please adjust according to your actual data source and the filters supported by your template engine.

Related articles

How to efficiently concatenate values of different fields in the custom content model (`archiveParams`) using the `add` filter?

In AnQi CMS, we often encounter the need to combine multiple field values of a custom content model into a text that is more expressive or conforms to a specific display format.For example, you may need to concatenate the "brand" and "model" of the product into a complete product name, or connect the "area code" and "phone number" of the contact.At this time, the `add` filter provided by AnQiCMS combined with the `archiveParams` tag can help us efficiently perform these operations.###

2025-11-07

How to use the `add` filter to dynamically add custom tracking parameters to `tag` links?

In website operation, we often need to track user behavior and evaluate the effectiveness of marketing through different channels.Adding tracking parameters dynamically to website links is an effective method.AnQi CMS is an efficient and flexible content management system that provides a powerful template engine and rich filters, allowing us to easily meet this requirement. Today, let's discuss how to use the `add` filter of Anqi CMS to dynamically add custom tracking parameters to the link of the "Tag".

2025-11-07

Can the `add` filter be used to concatenate array elements processed by the `slice` or `split` filters to form a new string?

When developing templates with AnQi CMS, we often need to flexibly process and display data.This includes string splitting, slicing, and element connection.AnQi CMS provides a rich set of filters (filters) to help us complete these tasks, such as `add`, `slice`, and `split`.Sometimes, we might consider concatenating array elements obtained after processing with `slice` or `split` filters using the `add` filter to form a new string.But is this idea feasible

2025-11-07

How does the `add` filter flexibly combine fixed text with variable content when building dynamic prompt information?

In Anqi CMS template design, building dynamic prompts with real-time interactive features is the key to improving user experience.Whether it is to display product inventory, user welcome messages, or article reading volume, we hope to seamlessly combine fixed text with continuously changing variable content.At this point, the `add` filter becomes a very practical and flexible tool.The `add` filter in the Anqi CMS template is very intuitive: it can add or concatenate two values.This process has high intelligence. If the two values being operated on are of numeric type, it will perform mathematical addition.

2025-11-07

How to use the `add` filter to dynamically generate the complete path segment in the breadcrumb navigation (`breadcrumb`)?

In website operation, breadcrumb navigation is one of the key elements to improve user experience and website SEO performance.It clearly shows the user's position on the website and provides a convenient way to return to the previous level page.AnQi CMS provides a powerful and flexible template tag system, where the `breadcrumb` tag can help us easily implement breadcrumb navigation.But if we need to dynamically adjust the path text or links in the breadcrumb navigation, the `add` filter of Anqi CMS can play a unique role.`breadcrumb`

2025-11-07

Does the `add` filter cause garbled or incorrect output when concatenating Chinese and English mixed strings?

When using AnQiCMS for template development, we often need to concatenate different text content, such as dynamically generated titles, descriptions, etc.At this time, the `add` filter has become a powerful tool in our hands.However, when dealing with mixed Chinese and English strings, many friends may worry: Will such concatenation produce garbled characters or cause program errors?Today, let's discuss this issue in detail.

2025-11-07

In the AnQiCMS template, can a simple counter dynamic display be realized through the `add` filter?

AnQiCMS is an enterprise-level content management system developed based on the Go language, providing strong support for content operators with its efficient and flexible features.In daily content operation and template creation, we often encounter the need to process and dynamically display data, such as adding numbers to list items and calculating totals.Among them, the template filter is an important tool for realizing such needs.Today, let's discuss the `add` filter in the AnQiCMS template to see if it can help us achieve a simple dynamic counter display.###

2025-11-07

How to concatenate `{module}` and `{id}` variables using the `add` filter in Custom URL mode to build a link?

In AnQi CMS, the way websites build links is crucial for SEO and user experience.Although the system provides various predefined pseudo-static rules and automatically generated links, but in certain specific scenarios, we may need to control the URL structure more finely, such as in the custom URL mode, where we concatenate the content model (`{module}`) and content ID (`{id}`) variables to form a unique link.This is not a difficult task, the powerful template engine of Anqi CMS combined with its flexible filter mechanism allows us to easily achieve this goal. Today

2025-11-07