In a content management system, time data often exists in the form of a series of numbers, which we call a timestamp (Unix Timestamp). This form is very efficient for machine processing, but it is not very user-friendly.1678886400Such numbers are obviously not easy to intuitively understand the date and time they represent. Anqi CMS is well aware of this and therefore provides a highly practical template tagstampToDateHelp us convert these cryptic timestamps into the date and time format we are accustomed to reading in our daily lives.

stampToDateThe magic use of tags

stampToDateThe core function of the label 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 label is very intuitive:

{{stampToDate(时间戳, "格式")}}

Here,时间戳It is the number you want to convert, which may come from the publication time of the article (for examplearchive.CreatedTime), the creation time of the comments, or any other data stored in the form of a timestamp."格式"[en] Part of it is defining the appearance you want the time to ultimately present.

Go Language Time Formatting: Unique Features

Representing content management systems (such as PHP or Java) through specific letters (such asYRepresents the year,mRepresents the month,HThe definition of time format using (hour) differs, and the Anqi CMS is developed based on Go language, therefore its time formatting follows the unique rules of the Go language.

The Go language uses a fixed reference date and time as a template for time formatting: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 information such as year, month, day, hour, minute, and second in the output, you need to write these numbers or abbreviations in the format string corresponding to the reference date. This tells the system which part you want to output and in what form.

For example:

  • Year: Use2006
  • Month: Use01(Number of the month),Jan(Abbreviated English month),January(Full English Month)
  • Day: Use02(Number Date)
  • Hour: Use15(24-hour format),03or3[12-hour format]
  • minutes: Use04
  • seconds: Use05
  • AM/PM: UsePM
  • time zone: UseMST

The key is that you need 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,2006,instead ofyyyy.

[en]Actual Application: Make Time Alive

After understanding the formatting rules of the Go language, we can flexibly apply timestamps to all corners of the website.

Assume we are building a list of articles and we want to display the publication date of each article. In the template, we can get the creation timestamp of the article.archive.CreatedTimeby obtaining the creation timestamp of the article.

If you want to display a common date format, such as2023-01-15You can use it like thisstampToDate:

{{stampToDate(archive.CreatedTime, "2006-01-02")}}

If we need a more detailed time, including hours, minutes, and seconds, such as2023年01月15日 14:30:45:

{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04:05")}}

[en] Note that I have directly written Chinese characters such as "year

Sometimes, we may only need the month and date, separated by a slash, for example01/15:

{{stampToDate(archive.CreatedTime, "01/02")}}

Even just the time can be displayed, for example下午02:30:

{{stampToDate(archive.CreatedTime, "03:04PM")}}

Through 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 below the title:

发布于:{{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 information transmission clearer.

Summary

By using flexibilitystampToDateTags and their Go-specific formatting rules allow users of the Anqi CMS to easily convert backend timestamp data into user-friendly and diverse date-time displays.Mastering this tag not only enhances the reading experience of the website content but also demonstrates the powerful capability of AnQi CMS in detail handling.stampToDateAll of which can help you present time information in a** manner.


Common Questions (FAQ)

Q1: Why do I useY-m-d H:i:sthis format string not work?A1: The AnQi CMS is developed in Go language, and its time formatting follows the unique rules of the Go language, rather than the common rules in languages like PHP or Python.Y-m-dPlaceholder. You need to use the reference date in Go language.2006-01-02 15:04:05The corresponding number in it to construct your format string. 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:stampToDateTags are expected to receive a 10-digit (second-level) timestamp by default.If your timestamp is 13 digits in milliseconds, you need to divide it by 1000 first to convert it to a second-level timestamp.{{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 the Go language are based on a reference date, so you can directly insert Chinese characters into the format string. For example, if you want to display 'Monday, January 02, 2023', you need to find the reference value represented by the Go language for the day of the week (for exampleMon),then construct{{stampToDate(时间戳, "Mon, 2006年01月02日")}}Ensure your template file is encoded in UTF-8 so that Chinese characters can be displayed correctly.