How to further process the formatted date string from `stampToDate` using other filters (such as truncation)?

Calendar 👁️ 62

In the Anqi CMS template world, we often need to convert timestamps (timestamp) into readable date and time strings.stampToDateThis label function is undoubtedly a good helper for us to achieve this goal.It can elegantly present a 10-digit timestamp according to the Go language date format, such as "2023-06-07 15:30:00".

However, the actual needs of website operation are always changing.Sometimes, we are not satisfied with just basic date formatting, and we may need to further process the already formatted date string, such as cutting a part of it, or making more detailed adjustments according to specific scenarios.For example, we may want to display only the month and date ("06/07"), or only the time ("15:30"), or even need to shorten the long date string to fit the limited display space.

At this time, the "filter" provided by the AnQiCMS template engine (filters) comes into play.

stampToDate: The initial formatting of the date

First, let's take a look backstampToDateThe basic usage.It accepts two parameters: a 10-digit timestamp and a string representing the Go language date format.

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

Assumeitem.CreatedTimehas a value of1678886400Corresponding to March 15, 2023, 00:00:00, the output of the above code will be: "2023-03-15 00:00:00".

Filter: Refines formatted date strings

Now, we have a formatted date string, but our goal is to further process it.AnQiCMS's template filter is like a pipeline, which can pass a variable (or the result of a function) through a series of processing steps, ultimately obtaining the data we want.{{ 变量 | 过滤器名称:参数 }}.

For extracting date strings, we have several very practical filters to choose from, includingsliceandtruncatecharswhich are the two most commonly used.

UsesliceFilter to accurately extract strings

sliceThe filter allows us to operate on substrings of a string like an array, based on the start and end indices.This is very effective for extracting specific parts from a formatted date string."start:end"of whichstartIs the starting index (inclusive),endIs the ending index (exclusive). If omittedstart, then extract from the beginning of the string; if omittedendTruncate to the end of the string.

Example 1: Only show the month and date.

Assuming we have already passedstampToDateGot the string "2023-03-15", now we only want to display "03-15". We can first format the date to "year-month-day", then usesliceStart from the 5th character (index 4, because indexing starts from 0) to the end of the string:

{# 假设item.CreatedTime的时间戳对应2023-03-15 #}
{% set formattedDate = stampToDate(item.CreatedTime, "2006-01-02") %}
{# formattedDate 现在是 "2023-03-15" #}
{{ formattedDate | slice:"5:" }}

The output of this code will be: “03-15”. Here is"5:"Extracting from the 5th character (index 4) to the end of the string.

Example two: only show time (hour:minute)

If we need to extract "10:30" from "2023-03-15 10:30:45", we can do it like this:

{# 假设item.CreatedTime的时间戳对应2023-03-15 10:30:45 #}
{% set formattedDateTime = stampToDate(item.CreatedTime, "2006-01-02 15:04:05") %}
{# formattedDateTime 现在是 "2023-03-15 10:30:45" #}
{{ formattedDateTime | slice:"11:16" }}

The output will be: "10:30". Here is"11:16"It means starting from the 12th character (index 11) and ending at the 16th character (index 15).

UsetruncatecharsFilter by character length

truncatecharsThe filter is more focused on truncating the string to the specified character count and automatically adding an ellipsis ("...").This is very useful when there is limited display space but you do not want to lose all the information.

Example: Shorten long date format for display

If we have a very long date string, such as “2023 March 15th Wednesday 10:30:45”, but we want to display only the first 10 characters on the front end and use an ellipsis after that, we can usetruncatechars:

{# 假设item.CreatedTime的时间戳对应2023年03月15日 星期三 10时30分45秒 #}
{% set longDateString = stampToDate(item.CreatedTime, "2006年01月02日 星期三 15时04分05秒") %}
{# longDateString 现在可能是 "2023年03月15日 星期三 10时30分45秒" #}
{{ longDateString | truncatechars:10 }}

The output will be: "2023 March..." (Note,truncatecharsthe ellipsis will also be counted in the total length).

Combine{% set %}Labels enhance readability

When we need to perform multi-step processing or want the code to be clearer, we can use{% set %}Label to define a temporary variable. This helps break down the complex process into more readable small steps.

`twig {% set rawTimestamp = item.CreatedTime %} {# Step 1: Format as a complete date and time string #} {% set fullDateTime = stampToDate(rawTimestamp, "2006-01-02 15:04") %}

{# Second step: Extract the date part from the full date and time #} {% set datePart = fullDateTime | slice: "0:11" %} {# Assuming the need for "YYYY-MM-DD" #}

{# Third step: Further process the date part, such as truncating or replacing #} {% set displayDate = datePart | replace: "year", ""}

Related articles

How to use `stampToDate` to display the publication time of each comment in the Anqi CMS comment list?

Good, as an experienced website operations expert, I know that every detail can affect user experience.In Anqi CMS, present the publication time of the comment list in a clear and understandable way, which can not only improve the user experience but also make the website content look more timely and professional.Today, let's delve into how to cleverly use the `stampToDate` tag in the Anqi CMS comment list to make the publishing time of each comment clear at a glance.

2025-11-07

What is the specific meaning of the format string "`2006-01-02 15:04:05.999999999 -0700 MST`" in the `stampToDate` tag?

## Unveiling the AnQiCMS `stampToDate` tag: The mysteries of Go language string formatting for date-time

2025-11-07

How to implement localized date format display in `stampToDate` of a multilingual website template?

In today's globalized digital world, website operators all know the importance of content localization.Especially for multilingual websites, it's not just the translation of text content, but also elements like dates and times that seem trivial need to be localized according to the cultural habits of the target users.AnQiCMS is a content management system tailored for small and medium-sized enterprises and content operation teams, deeply understanding this field, and providing powerful date format localization capabilities through its flexible template engine.

2025-11-07

Can the `stampToDate` tag convert timestamps to full Chinese date descriptions, such as 'October 1, 2023'?

## Insight AnQiCMS: Deep Analysis of `stampToDate` Tag and Full Chinese Date Descriptions In website content operation, the accuracy and diversity of time display are crucial for improving user experience and meeting specific business needs.For senior content operators, skillfully handling the template tags of a content management system (CMS) and transforming technical abilities into actual operational effects is the core of daily work.

2025-11-07

If the `archive.CreatedTime` field is empty, how will `stampToDate` handle and display?

As an experienced website operations expert, I know that the timeliness of content is crucial for user experience and SEO optimization.AnQiCMS (AnQiCMS) with its powerful content management and flexible template system, allows us to efficiently handle these requirements.In content display, handling the publication time is a common and critical link, we usually use the `stampToDate` template tag to format the timestamp data from the backend into a user-friendly date and time format.

2025-11-07

How to get and display the day of the week corresponding to the timestamp in the AnQi CMS template using `stampToDate`?

## Unlock Time Magic: Displaying the Weekday Easily in Anqi CMS Templates As an experienced website operations expert, I am well aware of the importance of content timeliness and presentation methods for user experience and information communication.In AnQiCMS (AnQiCMS) such a flexible and efficient content management system, we can not only easily manage diverse content, but also finely control every detail of content display through its powerful template engine.

2025-11-07

Are there any special considerations when using the `stampToDate` tag in the Anqi CMS multi-site environment?

As an experienced website operation expert, I have accumulated rich experience in the practical application of AnQiCMS, especially feeling deeply about the flexible use of template tags.Today, let's delve deeply into the commonly used `stampToDate` tag, and when it is called in the multi-site environment of AnQiCMS, are there any points that need special attention?AnQiCMS is an efficient and customizable enterprise-level content management system, and its multi-site management function is one of its core advantages.It allows users to manage multiple independent sites on a unified backend

2025-11-07

Can `stampToDate` format timestamps into ISO 8601 standard format (such as "YYYY-MM-DDTHH:MM:SSZ")?

As an experienced website operation expert who deeply understands the operation of AnQiCMS, I fully understand the importance of time formatting, especially standardized time formats, in content management.Today, let's delve into the `stampToDate` tag of Anqi CMS to see if it can format timestamps into ISO 8601 standard format, such as the `"YYYY-MM-DDTHH:MM:SSZ"` we often see.### AnQi CMS's `stampToDate`: Easily format timestamps to ISO

2025-11-07