As an experienced CMS website operation personnel in security, I fully understand the importance of content presentation details for user experience.The accurate and clear display of time information can not only help users quickly understand the timing of content release or update, but also indirectly affect the professionalism and reliability of the website.In AnQi CMS, we have a set of efficient and flexible mechanisms that can convert timestamp data stored on the backend into user-friendly date and time strings.
Timestamp formatting feature in Anqi CMS
In the Auto CMS, the system internally stores date and time data in the form of timestamps, which is a digital format convenient for computer processing. However, it is not convenient to directly display such as1609470335This timestamp is not intuitive enough. To solve this problem, Anqi CMS provides a powerful template tagstampToDateIt can convert these original timestamp data into easily readable date and time strings according to the format you define.
ThisstampToDateLabels are the core tools that ensure the clear and professional presentation of time information during content creation and template design.Whether it is displaying the publish date on the article detail page or showing the update time on the list page, it can provide precise control.
Understanding timestamp and Go language formatting layout
When usingstampToDateBefore the label, we need to understand two key concepts: timestamp and Go language time formatting layout.The timestamp usually refers to the number of seconds elapsed since the Unix epoch (00:00:00 UTC on January 1, 1970), and in the safe CMS, it is usually a 10-digit integer.
而“format” part, the AnQi CMS adopts the special time formatting mechanism of the Go language. It is different from many that use symbols (such asYYYY-MM-DD)to define the system format, the Go language uses a specific reference time2006-01-02 15:04:05.999999999 -0700 MST(i.e.,)1 January 2nd, 3:04:05 PM, 2006, located in the mountain time zone of the United States, UTC-07:00)to use as a template. You just need to replace the corresponding part of this reference time with the format you want to display. For example, if you want to display the year, write in the format string2006If you want to display the month, write01.
stampToDateBasic usage of the tag
stampToDateThe label usage is very intuitive.It requires two parameters: the first is the 10-digit timestamp you want to format, and the second is the format string in Go language style.
{{stampToDate(时间戳变量, "格式化字符串")}}
For example, if you have a variable nameditem.CreatedTimetimestamp variable and you want to format it as年-月-日You can write it like this:
{{stampToDate(item.CreatedTime, "2006-01-02")}}
Actual case: Displaying the document publishing time
In our daily operations, the most commonly usedstampToDateThe scenario of the label is one of displaying the publication or update time of the content in the document list and detail page. For example, on the article list page, we may usearchiveListLabel gets document data, where each document object containsCreatedTime(Creation time) andUpdatedTimea timestamp field like (Updated Time).
We assume that we want to display the publication date of each article in a list, formatted as “year-month-day”, the code can be organized as follows:
{% archiveList archives with type="list" limit="10" %}
{% for item in archives %}
<li>
<h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
<p>发布日期:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</p>
</li>
{% endfor %}
{% endarchiveList %}
In the article detail page, we can also usearchiveDetailtags to obtain the detailed information of the current document, and format its time field in the same way:
{% archiveDetail archiveContent with name="Content" %}
<h1>{% archiveDetail with name="Title" %}</h1>
<p>发布于:{{stampToDate(archive.CreatedTime, "2006-01-02 15:04:05")}}</p>
<div>{{archiveContent|safe}}</div>
{% endarchiveDetail %}
This display method allows users to clearly obtain key time information at a glance, enhancing the readability and user experience of the content.
Advanced formatting options
The formatting layout of Go language provides a rich combination to meet various complex date and time display needs. The following are some commonly used and practical formatting string examples:
- Only show the year:
"2006" - Show only month and date:
"01-02" - Show hours and minutes (24-hour format):
"15:04" - Show hours, minutes, and seconds (24-hour format):
"15:04:05" - Full date and time:
"2006-01-02 15:04:05" - Date with weekday:
"Mon Jan 2, 2006" - Custom delimiter:
"2006/01/02"or"02.01.2006"
By flexibly combining the numbers and text from these reference times, we can create any date and time format that conforms to the website brand style and user habits.This allows us to customize the presentation of time information for different content types or different display areas.
Summary
stampToDateLabel is an easy-to-use yet powerful tool in AnQi CMS, which converts the original timestamp data in the backend into user-friendly date and time strings.As a website operator, mastering the usage and flexibly applying the formatting layout of the Go language can significantly enhance the professionalism and user experience of the website content, making our website more accurate and efficient in information transmission.
Common Questions and Answers (FAQ)
Q1: Why does the Go language use time formatting strings2006-01-02 15:04:05as a reference instead ofYYYY-MM-DDsymbols like this?
A1: This is a unique feature of Go language design.It does not use the common placeholder symbols but directly uses a fixed reference date and time (i.e., "Go Time": January 2, 2006, 15:04:05) as the template for formatting.2006Represents the year,01Representing months, etc.), each corresponds to a specific part of this reference time, and in this way indicates how you want the date and time to be displayed.This design may seem unaccustomed at first, but once you understand its logic, you will find it very flexible and intuitive.
Q2: If the timestamp I get from the external system is not a standard 10-digit second-level Unix timestamp (for example, it is a 13-digit millisecond timestamp),stampToDateCan the label still work?
A2:stampToDateThe label expects to receive a 10-digit second-level Unix timestamp by default. If your timestamp is 13 digits in milliseconds,stampToDateBefore that, first convert it to a second-level timestamp by dividing it by 1000. For example, if your variable isitem.MillisecondTime, you may need to handle it like this:{{stampToDate(item.MillisecondTime / 1000, "2006-01-02")}}. Please note that the template engine may require support for basic arithmetic operations.
Q3: Can I customize the text in the date, such as displaying 'Monday' as '周一'?
A3:stampToDateThe label itself is responsible for converting dates and times according to the formatting rules of the Go language.It does not automatically perform language or localization text replacements.trTranslate label), and you wish to display different names of weekdays or months based on the language environment, which usually requires combining multilingual files and template logic. You can format the date and extract the weekday part (for example,MonUsing a key as a label, provide the corresponding translation value in the multilingual file.