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), use
2006. - If you want to display the month (two digits), use
01. - If you want to display the hour (24-hour format), use
15.
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 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.