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", ""}