In AnQi CMSCreatedTimewithstampToDateThe mystery of timestamp conversion:

As an experienced website operator and a deep user of AnQi CMS, I know how common and crucial the handling of time data is in daily content management and template development.Especially when using Go language to build high-efficiency systems like safe CMS, understanding timestamps and correct use of template tags is particularly important.Recently, I noticed that some users have beenCreatedTimeField in coordinationstampToDateA question arises when using the tag:CreatedTimeAs a 10-digit timestamp, when usingstampToDateDo you need to multiply by 1000 to convert it to milliseconds first?

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:Not requiredWhen you are handling the template in Anqi CMS,CreatedTimeand intend to use this kind of 10-digit timestamp field,stampToDateWhen formatting the tag, you can directly pass it as a parameter without any multiplication by 1000 conversion operation.

Why is this? It originates from the international standard of timestamps and the design philosophy of Anqicms.

In the world of timestamps, a 10-digit number typically represents the number of seconds that have passed since the Unix epoch (that is, 00:00:00 UTC on January 1, 1970).This "second-level timestamp" is the default standard for many systems and APIs.And a millisecond timestamp is usually a 13-digit number. Anqi CMS is an enterprise-level content management system developed based on Go language, adhering to the principles of intuition and industry conventions in timestamp processing.

We can clearly find it in the Anqicms documentation.stampToDateLabel description:

How to use:{{stampToDate(时间戳, "格式")}}Timestamp is a 10-digit time, such as1609470335, formatted in the format supported by Golang.

This description directly indicates,stampToDateThe tag expects to receive a 10-digit second timestamp. Therefore, if you take a timestamp that is already in seconds (such asCreatedTimeThen multiply it by 1000, and incorrectly convert it to milliseconds, thenstampToDateIt will be treated 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 a 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, in the above code,item.CreatedTimeis directly passed instampToDatefunction, which does not involve any multiplication operations. This also applies toCreatedTimeyou 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.stampToDate.

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

Summary

AnQi CMS follows the principle of simplicity and efficiency in handling timestamps.CreatedTimeAnd other 10 timestamp fields are naturally second-level timestamps and arestampToDateLabel is directly recognized and processed. This greatly simplifies the time handling logic in template development, avoids unnecessary conversions, and reduces the risk of time display errors due to misoperations.

In the future development and operation, please rest assured to directly use the 10-digit timestamp provided by Anqi CMS andstampToDateLabel, let your website content present time information in the most accurate and elegant way.


Frequently Asked 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 will become a 13-digit number, which is usually considered a millisecond timestamp. ButstampToDateIt is expected to be in seconds. Therefore, it will treat this 'fake millisecond' timestamp as a huge number of seconds, resulting in a final display of a distant future date, usually a few decades later, such as 2050, 2060, and so on.

  2. Does AnQi CMS have built-in tags that can display time as 'just now', '5 minutes ago', and so on in relative terms?Based on the current document information, AnQi CMS'sstampToDateThe tag is 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 requirement, 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 via custom Go backend logic to provide this feature.

  3. exceptCreatedTimeWhat are some time fields that are also 10-digit timestamps and can be used directly withstampToDateto be used together?In AnQi CMS, multiple fields related to time use a 10-digit timestamp format. As documented, common ones include:

    • UpdatedTime: Document update time.
    • LastLogin: The user's most recent login time.
    • ExpireTime: The expiration time of VIP or other services. These fields, as long as their values are in the form of 10-digit numeric timestamps, can be directly passed tostampToDateLabel formatting. When in doubt, you can check the output value of the field. If it is a 10-digit number, it is usually a timestamp in seconds.