As an experienced CMS website operation person, I know that the details of content presentation are crucial for user experience.Accurate and clear display of time information, which can not only help users quickly understand the publication or update timeliness of the content, but also indirectly affect the professionalism and credibility of the website.In Anqi CMS, we have a high-efficiency and flexible mechanism that can convert timestamp data stored in the background into user-friendly date and time strings.
Timestamp formatting feature in Anqi CMS
In AnQi CMS, the system internally stores date and time data in the form of timestamps, which is a digital format convenient for computer processing. However, displaying such things directly to users as1609470335This timestamp is not intuitive enough. To solve this problem, Anqi CMS provides a powerful template tagstampToDateIt can convert these original timestamp data into readable date and time strings according to the format you define.
ThisstampToDateTags are the core tools we use in content creation and template design processes to ensure that time information is presented clearly and professionally.Whether displaying the publication date on the article detail page or showing the update time on the list page, it provides precise control.
Understanding Timestamps and Go Language Formatting Layout
While usingstampToDateBefore the label, we need to understand two key concepts: timestamps and Go language time formatting layout.Timestamp usually refers to the number of seconds elapsed since the Unix epoch (January 1, 1970, 00:00:00 UTC), which is typically a 10-digit integer in AnQi CMS.
The 'format' part, Anqi CMS adopts the special time formatting mechanism of the Go language. Like many that use symbols such asYYYY-MM-DD)to define the system of formats, the Go language uses a specific reference time2006-01-02 15:04:05.999999999 -0700 MST(i.e.),January 2, 3:04:05 PM, 2006, located in the Mountain Time Zone of the United States, UTC-07:00Use it 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, just write it in the format string.2006If you want to display the month, you write01.
stampToDateBasic usage of tags
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 defined in the Go language style.The basic syntax structure is as follows:
{{stampToDate(时间戳变量, "格式化字符串")}}
For example, if you have a variable nameditem.CreatedTimetimestamp variable, and you want to format it as年-月-日format, you can write it like this:
{{stampToDate(item.CreatedTime, "2006-01-02")}}
Actual case: Display of document publishing time
In our daily operations, we often usestampToDateOne of the scenarios for tags is to display the publication or update time of content in document lists and detail pages. For example, on the article list page, we might usearchiveListTag to get document data, where each document object containsCreatedTime(creation time) andUpdatedTimesuch timestamp fields like (update time).
Assuming 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 get 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 the Go language provides a rich combination to meet various complex date and time display needs. Here are some commonly used and practical formatting string examples:
- Only show the year:
"2006" - Show only the 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" - Complete date and time:
"2006-01-02 15:04:05" - Date with the week:
"Mon Jan 2, 2006" - Custom delimiter:
"2006/01/02"or"02.01.2006"
By flexibly combining the numbers and text in these reference times, we can create any date and time format that fits the brand style of the website and the habits of users.This allows us to customize the presentation of time information for different content types or different display areas.
Summary
stampToDateThe label is a simple but powerful tool in Anqi CMS, which converts the original timestamp data in the background into user-friendly date and time strings.As a website operator, mastering the usage and flexible application of Go language formatting layout can significantly improve the professionalism and user experience of the website content, making our website more accurate and efficient in conveying information.
Frequently Asked Questions (FAQ)
Q1: Why does the Go language use time formatting strings with2006-01-02 15:04:05as a reference instead ofYYYY-MM-DDsuch symbols?
A1: This is a unique feature of Go language design.It does not use the common symbol placeholders but instead directly uses a fixed reference date and time (i.e., "Go Time": January 2, 2006, 15:04:05) as the "template" for formatting.Each number you write in the formatted string (such as2006represents the year,01Representing months and so on, each corresponds to a specific part of this reference time, in this way indicating how you want the date and time to be displayed.This design may seem unnatural at first contact, but once you understand its logic, you will find it very flexible and intuitive.
Q2: If the timestamp I obtain from the external system is not the standard 10-digit second-level Unix timestamp (for example, it is a 13-digit millisecond timestamp),stampToDatecan the tags still work?
A2:stampToDateThe tag expects to receive a 10-digit second-level Unix timestamp. If your timestamp is 13 digits in milliseconds, you need to convert it before passing it tostampToDateBefore, first convert it to a second-level timestamp by dividing by 1000. For example, if your variable isitem.MillisecondTimeYou may need to handle it like this:{{stampToDate(item.MillisecondTime / 1000, "2006-01-02")}}Please note that the template engine may need to support basic arithmetic operations.
Q3: Can I customize the text in the date, such as displaying 'Monday' as 'Weekday'?
A3:stampToDateThe tag 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 replacement. If your template supports multi-language functionality (such as throughtrTranslate label), and you want to display different names for weekdays or months based on the language environment. This usually requires combining multi-language files and template logic to achieve. You can use the weekday part in the formatted date (such asMon) as a key, providing the corresponding translation value in the multilingual file.