In AnQiCMS (AnQiCMS) daily operation and template design, we often encounter situations where we need to display time data, such as the article publication timeCreatedTimeor update timeUpdatedTime.These time data are usually stored in the database in the form of Unix timestamps, which are obviously less intuitive and readable for users than
How can you elegantly convert these timestamp data into the readable date format we are accustomed to in Anqi CMS templates? The answer lies in a powerful and easy-to-use auxiliary tag built into the system:stampToDate.
MasterstampToDateLabel: The core tool for time conversion
stampToDateIs an Anqi CMS template engine (based on Django template engine syntax in Go language) that provides a special auxiliary tag for handling timestamps.The core function is to convert a 10-digit timestamp (usually in Unix timestamp format) to a date and time string in the format specified by the user.The introduction of this tag greatly simplifies the complexity of front-end time display, allowing the time information of the content to be presented in the most humanized way.
UsestampToDateThe label is very intuitive. Its basic syntax structure is:
{{ stampToDate(时间戳数据, "格式字符串") }}
Let's take a detailed look at these two key parameters:
时间戳数据This is usually the original timestamp field you obtain from the content object, such as the article detail objectarchiveofarchive.CreatedTimeorarchive.UpdatedTimeThese fields directly store 10-digit digital timestamps, waitingstampToDatefor "pointing".格式字符串This is the key to defining the output date and time format. Anqi CMS here continues to use the unique "reference time" format in the Go language standard library. Unlike the commonY-m-d H:i:sThis description format is different, Go language uses a fixed date and time template, that is2006-01-02 15:04:05. Just replace the year, month, day, hour, minute, and second in this reference time with the display format you want, and the system will format the actual timestamp according to your "template".For example:
- If you want to display the format of "Year-Month-Day", for example:
2023-10-26then the format string is"2006-01-02". - If you want to display it as Chinese "Year-Month-Day Time:Minute", for example:
2023年10月26日 14:30then the format string is"2006年01月02日 15:04". - To be accurate to seconds, it can be:
"2006-01-02 15:04:05". - Or other internationalization formats, such as
"02/01/2006"displayed as26/10/2023.
- If you want to display the format of "Year-Month-Day", for example:
This flexibility allows you to freely adjust the time display style according to the UI design of the website and the user's habits.
Actual application example
In Anqi CMS, whether it is an article list, product detail page, or single page, as long as it involves displaying timestamp data,stampToDatetags can be used.
For example, inarchiveDetailWhen getting the details of an article from a tag, you would usually do it like thisCreatedTime:
{# 假设我们正在一个文章详情页,或者在循环中遍历一个名为 `item` 的文章对象 #}
<p>发布日期:{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}</p>
<p>更新时间:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04") }}</p>
{# 在文章列表(如使用 archiveList 标签)中,循环项可能是 `item` #}
{% archiveList archives with type="list" limit="10" %}
{% for item in archives %}
<div class="article-meta">
<span>发布于:{{ stampToDate(item.CreatedTime, "2006/01/02") }}</span>
<span>最后更新:{{ stampToDate(item.UpdatedTime, "15:04") }}</span>
</div>
{# ... 其他文章内容展示 ... #}
{% endfor %}
{% endarchiveList %}
By these simple code snippets, timestamp data is beautifully converted into the date and time format we want to see, greatly enhancing the user experience.
Avoid common 'traps':stampToDatewithdateThe difference between filters
In the template system of AnQi CMS, besidesstampToDatetags, you may also see adateFilter. At first glance, both seem to be able to be used to format dates, but there is a key difference between them, understanding this is crucial to avoid potential errors.
dateThe filter is a built-in date formatting tool supported by the Django template engine, it is expected to handle Go language nativetime.TimeType object. However, Anqi CMS directly fetches it from the database.CreatedTimeorUpdatedTimeThese fields are essentially 10-digit Unix timestamps, not.time.TimeObject.
Therefore,Directly convert the original timestamp data (such asarchive.CreatedTimePass todateFilter can cause the template to report errors or display incorrect results.stampToDateLabels are designed to solve specific problems, they can directly receive a 10-digit timestamp and perform the correct conversion.
So, when you need to handle data from database fields such asCreatedTime/UpdatedTime) When directly obtaining the numerical timestamp, it is imperative to usestampToDatetag instead ofdatefilter. This will ensure that your time data is displayed accurately.
Summary
stampToDateTags are an indispensable tool in the content operation and template development of Anqi CMS.It solves the problem of timestamp data's humanized display in a concise and efficient manner.By flexibly using the 'reference time' format in Go language, you can give richer display forms to the time information on the website, thereby enhancing the user's understanding and browsing experience.Proficiently mastering this tag will undoubtedly make your Anqi CMS website more refined and professional in detail.
Frequently Asked Questions (FAQ)
Q1: Can I directly usedateFormat a filterCreatedTimeDoes it also look like it can handle time?
A1:It is not recommended to do this. In AnqicmsCreatedTimeandUpdatedTimeThe fields that are directly obtained from the database are 10-digit digital timestamps.dateThe filter is expected to process the native Go languagetime.Timeobjects, not the raw numeric timestamp. If the timestamp is passed directly todateA filter that may cause template parsing errors or display anomalies. The correct and recommended approach is to use a filter specifically designed for timestamps.stampToDate.
Q2:stampToDateWhat timestamp formats does the label support? What if my timestamp is not 10 digits?
A2: stampToDateThe tag is mainly designed to handle 10-digit Unix timestamps, which is a common way to store date and time in databases. If your timestamp is not the standard 10 digits (for example, it is 13 digits in milliseconds),stampToDateMay not work directly as expected.In this case, you may need to preprocess it in the backend business logic layer, converting it to a 10-digit Unix timestamp, or refer to the latest documents of Anqi CMS or seek community help to see if there is built-in support or recommended extension methods for other timestamp lengths.
Q3: Where can I find more time formatting references for the Go language, so I can customizestampToDatethe format string?
A3:The time formatting in Go language is based on a fixed reference time2006-01-02 15:04:05.999999999 -0700 MST. You can find the template tag documentation in AnQi CMStag-stampToDate.mdFind rich formatting examples, which show how to achieve various display effects by combining different parts of this reference time. In addition, the official Go language document discussestimethe package'sFormatDescription of the method, also provides the most comprehensive formatting rules and examples, and is a resource for in-depth learning of Go time formatting.