In a content management system, time data is often represented as a string of numbers, which we call a Unix Timestamp. This form is highly efficient for machine processing, but it is not very user-friendly, a string of1678886400This number cannot be intuitively understood to represent a date and time. Anqi CMS knows this and therefore provides a very practical template tagstampToDateHelp us convert these cryptic timestamps into the date and time format we are accustomed to reading in our daily lives.
stampToDateThe clever use of tags
stampToDateThe core function of the tag is to convert a 10-digit or 13-digit timestamp (usually second-level or millisecond-level) into a date and time string that is easy to understand according to the format rules we set. The basic usage of this tag is very intuitive:
{{stampToDate(时间戳, "格式")}}
Here, 时间戳The number you want to convert may come from the publication time of the article (for examplearchive.CreatedTime), the creation time of the comment, or any other data stored in timestamp form. And"格式"Part is to define the appearance of time you want it to ultimately present.
Unique aspects of Go language time formatting:
With other content management systems (such as PHP or Java) through a specific letter (such asYrepresents the year,mrepresents the month,HRepresenting hours) to define different time formats, Anqi CMS is developed based on Go language, therefore its time formatting follows the unique rules of Go language.
Go language uses a fixed reference date and time as a template when formatting time:2006年01月02日 15时04分05秒More precisely, the complete reference time is:
Mon Jan 2 15:04:05 MST 2006
This means that when you want to display the year, month, day, hour, minute, and second information in the output, you need to write the corresponding numbers or abbreviations from the reference date in the format string, so that the system knows which part you want to output and in what form.
For example:
- year: Use
2006 - month: Use
01(Number Month),Jan(Abbreviated Month),January(Full Month Name) - Day: Use
02(Number Date) - hours: Use
15(24-hour format),03or3(12-hour format) - minute: Use
04 - seconds: Use
05 - AM/PM: Use
PM - Time Zone: Use
MST
The key is to ensure that the numbers and letters in your format string correspond exactly to the reference date format in the Go language. For example, if you want to display a four-digit year, you must use2006instead ofyyyy.
The actual application: make time come alive
After understanding the formatting rules of Go language, we can flexibly apply timestamps to all corners of the website.
Assuming we are building an article list and want to display the publish date of each article. In the template, we can accessarchive.CreatedTimethe creation timestamp of the article.
If you want to display a common date format, for example2023-01-15We can use it like thisstampToDate:
{{stampToDate(archive.CreatedTime, "2006-01-02")}}
If we need more detailed time, including hours, minutes, and seconds, for example2023年01月15日 14:30:45:
{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04:05")}}
Pay attention, here I have directly written the Chinese 'year', 'month', 'day', etc. in the format string, and the system will output it as is.
Sometimes, we may only need the month and date, separated by slashes, for example01/15:
{{stampToDate(archive.CreatedTime, "01/02")}}
Even just the time, for instance下午02:30:
{{stampToDate(archive.CreatedTime, "03:04PM")}}
By these flexible combinations, you can precisely control the display of time data according to the needs of the page design. For example, on a document detail page, you may want to display:
发布于:{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04")}}
And on the list page, for brevity, it may only display:
{{stampToDate(item.CreatedTime, "01-02")}}
This makes our website content more vivid and the information transmission clearer.
Summary
By flexible applicationstampToDateThe label and the Go language-specific formatting rules allow Anqi CMS users to easily convert backend timestamp data into user-friendly and diverse date-time displays.Mastering this tag not only enhances the reading experience of website content but also showcases the powerful ability of Anqi CMS in detail handling.No matter what content you have, whether it is a blog article, product release or event news,stampToDateAll of which can help you present time information in ** manner.
Frequently Asked Questions (FAQ)
Q1: Why do I useY-m-d H:i:ssuch format strings not work?A1: Anqi CMS is developed based on Go language, its time formatting follows the unique rules of Go language, rather than the common rules in PHP or Python languagesY-m-dPlaceholder. You need to use the reference date of the Go language.2006-01-02 15:04:05to construct your format string with the corresponding numbers. For example,2006represents the year,01represents the month,02represents the date,15represents the hour in 24-hour format, and so on.
Q2: What should I do if my timestamp is a 13-digit number (millisecond level)?A2:stampToDateThe tag expects to receive a 10-digit (second-level) timestamp. If your timestamp is 13 digits long in milliseconds, you need to divide it by 1000 first to convert it to a second-level timestamp.You can perform simple mathematical operations in the template, such as{{stampToDate(时间戳 / 1000, "格式")}}To handle.
Q3: Can I add the Chinese name of the day of the week or month name in the formatted time?A3: The formatting rules of Go language are based on the reference date, so you can directly insert Chinese into the format string. For example, if you want to display "Monday, January 2, 2023", you need to find the reference value of Go language to represent the day of the week (for example,Mon),then construct{{stampToDate(时间戳, "Mon, 2006年01月02日")}}Make sure your template file is encoded in UTF-8, so that Chinese characters can be displayed correctly.