In AnQi CMSCreatedTimeWithstampToDate:The Mystery of Timestamp Conversion

As an experienced website operator and a deep user of CMS, I know that handling time data is common and crucial in daily content management and template development.Especially when using high-efficiency systems like security CMS built with Go language, understanding and correct use of template tags are particularly important.CreatedTimeField in cooperationstampToDateA question arises when using the tag:“CreatedTimeAs a 10-digit timestamp, do you need to multiply it by 1000 to convert it to milliseconds?stampToDate?

Today, let's delve deeply into this issue and unveil the true face of timestamp conversion in AnQi CMS.

Core revelation: No need to multiply by 1000.stampToDateDirectly supports 10-digit timestamps.

The answer is very clear:It is not necessary. When you are handling the template in Anqi CMSCreatedTimethese 10-digit timestamp fields and intend to usestampToDateThe label can be passed directly as a parameter when formatting, no need for any multiplication by 1000.

Why is this? It is due to the international standard agreement of timestamps and the design philosophy of AnQi CMS.

We can clearly find it in the documentation of AnQi CMS.stampToDateLabel description:

“English method of use:{{stampToDate(时间戳, "格式")}}. Timestamp is a 10-digit time, such as1609470335, in a format supported by Golang.

this description directly indicates,stampToDateThe tag is expected to receive a 10-digit timestamp. Therefore, if you have a timestamp that is already in seconds (such asCreatedTime)Multiplying by 1000, converting it incorrectly to milliseconds,stampToDateWhen processing, it will be considered as a timestamp far beyond the current time, resulting in a date that is completely inconsistent with expectations, usually several decades in the future.

Practice makes perfect: Direct application in templates

One of the design philosophies of Anqi CMS templates is intuitiveness and practicality, which is fully reflected in the handling of timestamps. When you need to display the creation time of the document, you can directly callitem.CreatedTimeThat's it, for example:

{# 假设item是循环中的文档对象 #}
<div>文档添加时间:{{stampToDate(item.CreatedTime, "2006-01-02")}}</div>
{# 如果需要更精确到时分秒 #}
<div>文档添加时间:{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}}</div>
{# 甚至可以自定义中文格式 #}
<div>文档添加时间:{{stampToDate(item.CreatedTime, "2006年01月02日 15时04分")}}</div>

Please note that, in the above code,item.CreatedTimeis directly passed in,stampToDatefunction, without any multiplication operations. This is not only applicable to,CreatedTimeyou may encounter in the AnQiCMS template,UpdatedTime/LastLogin/ExpireTimeFields related to time, as long as they are displayed as a 10-digit timestamp, they can be passed directly in the same way.stampToDateLabel.

stampToDateThe second parameter is the Go language-specific date and time formatting string, for example"2006-01-02"/"15:04:05"The placeholders for year, month, day, hour, minute, and second, respectively.This flexible formatting method allows you to easily convert timestamps into various easily readable date and time formats according to your specific needs.

Summary

AnQi CMS follows the principle of simplicity and efficiency when handling timestamps.CreatedTimeIts and other 10 timestamp fields are naturally second-level timestamps and have beenstampToDateLabel recognition and processing directly. This greatly simplifies the time processing logic in template development, avoids unnecessary conversions, and reduces the risk of incorrect time display due to misoperation.

In the future development and operation, please feel free to use the 10-digit timestamp provided by Anqi CMS directly.stampToDateLabels make your website content display time information in the most accurate and elegant way possible.


Common Questions (FAQ)

  1. If I mistakenly multiply a 10-digit timestamp by 1000 before passing it tostampToDateWhat will happen?If you mistakenly multiply a 10-digit (second-level) timestamp by 1000, it becomes a 13-digit number, which is usually considered a millisecond timestamp.stampToDateThe expectation is for seconds.Therefore, it will treat this "pseudo-millisecond" timestamp as a huge number of seconds, resulting in the final display of a distant future date, usually several decades later, such as 2050, 2060, and so on.

  2. Does the Anqi CMS have a built-in tag that can display time as 'just now', '5 minutes ago', and the like?According to the current document information, the Anqi CMS'sstampToDateLabels are mainly used to format timestamps into specific date and time strings (such as “2023-10-26 10:30:00”).The document does not directly mention providing built-in tags for relative time displays such as "X minutes ago.If you have such a need, you would usually consider using a JavaScript library (such as Moment.js or date-fns) to handle it on the front-end, or implementing a new tag or filter with custom Go backend logic to provide this functionality.

  3. ExceptCreatedTimeWhat other time fields are also 10-digit timestamps that can be used directly withstampToDatecoincidence?In the AnQi CMS, multiple fields related to time all use a 10-digit timestamp format. According to the document, common ones include:

    • UpdatedTime: Document update time.
    • LastLogin: User's recent login time.
    • ExpireTime: Expiration time of VIP or other services. These fields, as long as their values are in the form of 10-digit timestamp numbers, can be passed directly tostampToDateLabel formatting. When in doubt, you can check the field's output value; if it is a 10-digit number, it is usually a second-level timestamp.