Insight into AnQiCMS:stampToDateDeep analysis of labels and full Chinese date descriptions

In website content operation, the accuracy and diversity of time display are crucial for improving user experience and meeting specific business needs.For senior content operators, skillfully handling the template tags of the content management system (CMS) and transforming technical capabilities into actual operational effects is the core of daily work.stampToDateAnd pay special attention to a frequently mentioned issue: whether it can convert a timestamp to the full Chinese numeral description 'October 1, 2023'?

stampToDateThe wonder of it: The bridge of timestamps

AnQiCMS as an enterprise-level content management system developed in Go language excels in providing efficient and customizable content management solutions.Its template system adopts the syntax of Django template engine, which is concise and easy to use.stampToDateNo doubt it is a powerful assistant for handling the display of dates and times.

The core function of this tag is very intuitive: it can convert the Unix timestamp stored in the database (usually a string of 10 or 13 digits) into a date or time format in line with our daily reading habits. Its usage method is also quite simple, usually written as{{stampToDate(时间戳, "格式")}}Here, the 'format' is crucial, as it follows the built-in Go language time formatting rules, allowing us to define various complex date and time output patterns.

For example, if you have a timestamp variable namedpublishStampyou want to display as “2023-10-01”, you can use it like this:{{stampToDate(publishStamp, "2006-01-02")}}.This "2006-01-02" is not a literal year and date, but a reference time format used in the Go language to define date formats. It represents the "year-month-day" structure.

The challenge and implementation of displaying Chinese date descriptions

Now, let's return to the topic of the article:stampToDateCan the tag convert timestamps to full Chinese number descriptions like 'October 1st, 2023'?

From the documentation of AnQiCMS and the principle of Go language time formatting,stampToDateLabels can be very convenient to format timestamps into dates with Chinese units, for example “October 01, 2023”. By setting the format to"2006年01月02日"The system will combine the Arabic numerals for year, month, and day with the familiar Chinese characters for 'year', 'month', and 'day' to form a clear and easy-to-read date.This is more than enough for the date display needs of the vast majority of Chinese websites.

However, when we further explore, hoping to convert the numbers themselves in the date (such as “2023”, “10”, “01”) directly into full Chinese numerals in uppercase descriptions like “二零二三”, “October”, “First”stampToDateThe label does not have this feature built-in. This is mainly due to the time formatting mechanism of the Go language itself. The time formatting in Go language is based on a fixed reference time (Mon Jan 2 15:04:05 MST 2006It is defined by the character ), which parses the numbers and letters in the reference time to match and replace the corresponding part of the input timestamp.This means, it can accurately process the year '2006', the month '01', etc., and also identify and output the Chinese characters 'year', 'month', 'day' in the format string, but it will not automatically convert the Arabic numerals '2' to Chinese characters '二', or '10' to '十'.This demand for converting numbers to Chinese numerals in uppercase (such as 'Zero One Two Three Four Five Six Seven Eight Nine Ten Hundred Thousand') usually belongs to a more advanced localization or number processing category, rather than a standard time formatting function.

Flexible response: When standard functions are insufficient

Even ifstampToDateThe label cannot directly provide a date description in full Chinese numerals. The flexibility and scalability of AnQiCMS also leave us with various solutions.

For content operators, if this requirement is not a core function of the website and only involves a small number of date displays, the most direct way that does not rely on backend modification isFront-end JavaScript Helper.Once the date is displayed on the page in Arabic numerals, we can take advantage of the powerful capabilities of front-end JavaScript to write a simple function that traverses the date string and replaces the numbers with their corresponding Chinese uppercase characters.For example, after processing by JS, "2023年10月01日" can be changed to "October 1st, 2023".This method is flexible and convenient, without needing to touch the backend code of AnQiCMS.

Users with backend development capabilities, or when websites have a universal and systematic need for full Chinese date descriptions, may considerIn AnQiCMS Go backend for custom expansion.This may mean writing a custom Go function outside the template tag layer, which completes the conversion from numbers to Chinese uppercase before passing the timestamp to the template, or registers a new custom function directly in the template engine.Of course, this requires a certain amount of technical reserves.

Summary

In short, AnQiCMS'sstampToDateLabels provide us with efficient and convenient timestamp formatting capabilities, making it easy to display dates with Chinese units such asAlthough it cannot directly convert Arabic numerals in dates to full Chinese uppercase numerals (such as “Er Lao San”),this is not its original design intention, and it also reflects the characteristics of Go language time formatting mechanism.We can fully handle this specific requirement flexibly through front-end JavaScript, or we can do deeper customization development on the back-end to meet it.This once again proves the powerful adaptability and scalability of AnQiCMS as an enterprise-level content management system, always providing operators with a variety of ways to solve problems.


Common Questions (FAQ)

Q1:stampToDateHow to display Chinese date '2023年10月01日' as a label?

A1:You can directly instampToDateThe format string used in the format parameter of the label contains Chinese units. For example, if you want to display as "2023年10月01日", you can use{{stampToDate(时间戳, "2006年01月02日")}}. The Go language formatting rules recognize and preserve Chinese characters such as 'year', 'month', and 'day', and fill in the corresponding numbers in the timestamp.

Q2: Besides Chinese,stampToDateWhat are some common date formats supported?

A2: stampToDateLabels support all standard Go language date formatting layouts. For example:

  • Display as “2023-10-01”{{stampToDate(时间戳, "2006-01-02")}}
  • Displayed as “2023/10/01 15:04:05”:{{stampToDate(时间戳, "2006/01/02 15:04:05")}}
  • Displayed as “Oct 01, 2023”:{{stampToDate(时间戳, "Jan 02, 2006")}}You can use the reference time format of Go language (Mon Jan 2 15:04:05 MST 2006Combine flexibly to create any you need