How does the `stampToDate` tag format timestamps into any custom date and time format?
Master Anqi CMSstampToDate:Customize time display format easily
In the daily work of content operation, the presentation of time information is crucial.Whether it is the release date of the article, the time of product listing, or the submission time of comments, a clear and readable date and time format can greatly enhance user experience.However, we often get a series of difficult-to-understand Unix timestamps from the database, such as1609470335Such numbers. Don't worry, AnQiCMS (AnQiCMS) provides us with an elegant and powerful solution:stampToDate.
As an experienced website operations expert, I am well aware of the importance of transforming technical information into practical applications. Today, let's delve into the deep analysis.stampToDateLabel, allowing you to become a time formatting wizard in AnQi CMS, easily customizing various time display formats.
stampToDateThe core tool for timestamp formatting.
In the AnQi CMS template system,stampToDateIt is a label specifically used to format a 10-digit Unix timestamp into any custom date-time string.Its design philosophy is simple and efficient, perfectly fitting the performance and ease of use pursued by AnqiCMS based on Go language development.
The use of this tag is very intuitive:{{stampToDate(时间戳, "格式")}}
You just need to provide a 10-digit timestamp (usually obtained from the content field, such asitem.CreatedTime),as well as a string describing the expected output format. But the 'format' string here is not the one we are familiar withY-m-d H:i:sThis placeholder follows the unique time formatting rules of the Go language, which is the key we need to understand.
Unveiling the magic of time formatting in the Go language
AnQi CMS adopts Go language as its underlying development language, therefore, its time formatting mechanism also follows the unique "reference date" method of Go language. Other programming languages use placeholders such as%YRepresents the year) to define different formats, Go language uses a specific, fixed date asa reference template for formatting. This "magic" reference date is:
2006-01-02 15:04:05 MST -0700
Understanding this reference date is the core of mastering Go language's date formatting. Each number and English abbreviation represents a specific component of the date and time:
2006Represents the year (four digits)01Represents the month (two digits, such as01January02Represents the date (two digits, such as02Represents two days)15representing hours (24-hour format, such as15indicating 3 PM in the afternoon04representing minutes (two digits)05representing seconds (two digits)MSTrepresenting the time zone name-0700: represents time zone offset
If you want to include other time information in the output, such as the day of the week, morning/afternoon, you just need to replace the corresponding English abbreviation or number in the reference date with the style you want to display.
For example:
Jan: Abbreviation for month (such asJanrepresents January,Junrepresents June)Mon: Abbreviation for weekday (such asMonrepresents Monday,Satrepresents SaturdayPM: represents afternoon (with03used together, indicates 12-hour time format and AM/PM identifiers
When you need to format the time, simply combine the numbers or text corresponding to the reference date to represent the target format you want to display.The system will intelligently convert the timestamp to the time structure represented by the reference date and output it according to the format string you provide.
Practical Exercise: Common Formatting Scenarios
Now, let's combine the commonly used in Anqi CMSitem.CreatedTime(usually the creation timestamp of articles, products, etc.) field, demonstrated through actual casesstampToDateThe powerful function.
Assumeitem.CreatedTimehas a value of1678886400This corresponds to Beijing time `2023`