Unlock the CMS time magic:CreatedTimeHow to elegantly format as “YYYY-MM-DD”
As an experienced website operations expert, I know that the timeliness of content and the way it is presented is crucial to user experience.In AnQiCMS such a powerful content management system based on Go language, we can not only efficiently manage content, but also convert technical information into user-friendly display through flexible template tags.archiveDetailthe tags you getCreatedTimeFormatted into a concise and clear “YYYY-MM-DD” date format.
The template philosophy of Anqi CMS and the challenge of timestamps
The template system of AnQi CMS is designed to be very flexible and developer-friendly, adopting syntax similar to Django template engine, allowing developers to control the display of page content in a straightforward manner. In the template file (usually with.htmlauto/template目录下)中,我们使用双花括号{{变量}}来输出变量,用{% 标签 %}来控制逻辑和调用功能。
When we usearchiveDetail标签来获取某篇文档的详细信息时,例如文章的发布时间,其CreatedTimeField defaults to returning a standard Unix timestamp. This timestamp is typically a string of 10 digits (like1609470335Although it is easy for computers to handle, it is not intuitive and lacks aesthetic appeal to display such numbers directly to visitors.To enhance the user experience, naturally we hope to convert it into a clear and easy-to-read date format like "2023-10-26".
MasterstampToDate: AnQi CMS's time formatting tool
autostampToDateautostampautoTo)of the specified date format(Date).
stampToDateThe usage of tags is very concise and clear, it requires two core parameters:
Timestamp(
timestamp)This is the original 10-digit Unix timestamp you need to convert, for example, we have fromarchiveDetailThe one obtainedCreatedTime.formatted string (
format)This is a string defining the output date format. But here is a unique 'magic number' convention of an Antu CMS (and Go language itself): it is not the 'YYYY-MM-DD' as we usually understand, but the specific reference time of the Go language.2006-01-02 15:04:05This date includes each number and symbol, representing different parts of the time format.2006Represents the yearYYYY01represents the monthMM02represents the dateDD15Represents hoursHH(24-hour clock)04Represents minutesmm05Represents secondsss
Understood this set of 'magic numbers', we can freely combine to create any desired date format. For example, if we want to format the timestamp as 'YYYY-MM-DD', then the corresponding format string is"2006-01-02".
Practice Session: Step-by-Step FormattingCreatedTime
Suppose we are editing a template file for a document detail page (fortemplate/你的模板名称/模型名称/detail.htmlexample), and the page already uses context variablesarchiveObtained all details of the current document.
get
CreatedTimeIn the document details page,archive.CreatedTimeit will directly provide the timestamp of the current document's creation.**Application