Unlock the AnQi CMS time magic:CreatedTimeHow to elegantly format as "YYYY-MM-DD"

As an experienced website operation expert, I am well aware that the timeliness of content and the way it is presented is crucial for user experience.In AnQiCMS (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.Today, let's talk about a common operational requirement: how toarchiveDetailTags retrieved from comments.CreatedTimeFormat it into a concise and clear "YYYY-MM-DD" date format.

The template philosophy of AnQiCMS and the challenge of timestamps

The Anqi CMS template system is designed to be very flexible and developer-friendly, adopting syntax similar to the Django template engine, allowing developers to control the display of page content in a straightforward manner. In the template file (usually with.htmlas suffix, stored in/templateIn the directory) we use double curly braces{{变量}}to output variables, using{% 标签 %}To control logic and call functions

When we usearchiveDetailtags to get the details of a document, such as the publication time of the article, itsCreatedTimeThe field returns a standard Unix timestamp. This timestamp is usually a sequence of 10 digits (such as1609470335), although it is easy for computers to handle, it is not intuitive and lacks aesthetic appeal for visitors.To enhance the user experience, we naturally hope to convert it into a clear and easy-to-read date format like "2023-10-26".

MasterstampToDate: The time formatting tool of AnQi CMS

AnQi CMS understands the importance of this formatting requirement and therefore built a very practical auxiliary tag -stampToDate. This tag is specifically used to convert time stamps (stamp) into (To) For the specified date format (Date)

stampToDateThe usage of the tag is very concise and clear, it requires two core parameters:

  1. Timestamp (timestamp)This is the original 10-digit Unix timestamp you need to convert, for example, we start fromarchiveDetailObtainedCreatedTime.

  2. formatted string (format)This is a string that defines the output date format. But there is a unique 'magic number' convention in Anq CMS (and Go language itself) that is not what we usually understand as 'YYYY-MM-DD', but a specific reference time in Go language——2006-01-02 15:04:05Each number and symbol in this date represents a different part of the time format.

    • 2006Represents the yearYYYY
    • 01Represents the monthMM
    • 02Represents the dateDD
    • 15Represents hoursHH(24-hour clock format)
    • 04representing minutesmm
    • 05representing secondsss

Understanding this set of "magic numbers", we can freely combine to create any desired date format. For example, if we want to format a 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 document detail page template (for example, intemplate/你的模板名称/模型名称/detail.html), and the page has already used context variablesarchiveGained all the details of the current document.

  1. ObtainCreatedTime: In the document details page,archive.CreatedTimewill directly provide the creation timestamp of the current document.

  2. **Application