In website content management, the accuracy and aesthetics of time presentation are crucial for user experience.无论是文章的发布日期、产品更新时间,还是评论提交时刻,清晰、一致的时间格式能够显著提升内容的可读性和专业性。stampToDateTags, specifically used for formatting timestamps into various custom date and time display formats.
UnderstandingstampToDatetags
stampToDateThe core function of the tag is to convert a standard 10-digit Unix timestamp (i.e., the number of seconds from 00:00:00 UTC on January 1, 1970, to the current time) into a date and time string that is easy for humans to read.This tag can be used directly in the template language of AnQi CMS without complex logic processing, greatly simplifying the display of time information.stampToDateCan fully demonstrate its capabilities, converting it to formats such as
stampToDateThe syntax structure of tags
stampToDateThe syntax structure of the label is intuitive and easy to master, its basic form is{{stampToDate(时间戳, "格式")}}.
Among them,时间戳Partially expected to be an integer of 10 digits, representing a Unix timestamp. In AnQi CMS templates, this usually comes from a database field, such as an article object ("archive), or a list item (item) ofCreatedTimeorUpdatedTimeFor example:item.CreatedTimeThis will provide a 10-digit timestamp.
格式The format is a string that defines the specific style you want the timestamp to be formatted.It is especially important to note that the Go language used by the underlying Anqi CMS adopts a unique 'reference time' mode for time formatting.YYYY-MM-DDInstead of such an abstract placeholder, a specific date and time value in Go language is used to define the format. This reference time is fixed to:2006年1月2日 15时4分5秒,specific to milliseconds and time zones, is2006-01-02 15:04:05.999999999 -0700 MST. This means that if you want to display the year, you need to use2006as a placeholder in the format string; to display the month, use01; for the date02. Continue in this manner.
stampToDateThe actual application example
Let's understand through some specific examplesstampToDateFlexible application. Suppose we have a timestamp variablepublishStampwith the value1609470335(Corresponding to January 1, 2021, 12:25:35).
To format it into a common "year-month-day
<div>{{stampToDate(publishStamp, "2006-01-02")}}</div>
If you need to display the date in Chinese format, for example “2021年01月01日”:
<div>{{stampToDate(publishStamp, "2006年01月02日")}}</div>
For the requirement of displaying only time (e.g.,
<div>{{stampToDate(publishStamp, "15:04")}}</div>
<div>{{stampToDate(publishStamp, "15:04:05")}}</div>
Combine the date and time to achieve a more comprehensive display, for example, "2021-01-01 12:25:35":
<div>{{stampToDate(publishStamp, "2006-01-02 15:04:05")}}</div>
In the actual content list, for example, when usingarchiveListLabel loops to display articles when,item.CreatedTimeanditem.UpdatedTimethe field is usually a 10-digit timestamp. At this time,