As an experienced website operations expert, I am well aware that every detail of website content management is related to user experience and brand image.AnQiCMS (AnQiCMS) relies on its efficient architecture based on the Go language and flexible template system, providing strong support for content management.However, even the most excellent system may encounter some small challenges in the customization process.Today, let's delve deeply into a problem that often confuses developers and operators in the development of Anqi CMS templates: how to avoid due tostampToDateFormat string error caused by page display exception?

In the content of the website, the display of time is everywhere, whether it is the publication date of the article, the update time of the product, or the submission moment of the comments, it is crucial to present these time information accurately and clearly. The template system of AnQi CMS providesstampToDateThis powerful tag is designed to convert the 10-digit timestamp (Unix timestamp) stored in the database to the date and time format we are accustomed to.It allows us to display time in a friendly format like “October 26, 2023, 14:30” instead of a string of hard-to-understand numbers.

However, when usingstampToDateAt times, some developers may encounter page display exceptions, such as date display being empty, showing "0000-00-00", or even causing template parsing errors. This is often notstampToDateThe issue is not with the label itself, but with the second parameter—the setting of the format string.

Deep understanding of the time formatting rules in Go language.

AnQi CMS is developed based on Go language, which means its template engine follows the unique rules of Go language when formatting time. This is different from the PHP we may be more familiar with, such asY-m-d H:i:s) Python (such as%Y-%m-%d %H:%M:%S) or Java and other languages have quite different formatting methods.

The time formatting in Go language is not abstracted to represent year, month, day, hour, minute, second, etc., by letter symbols, but by a singlefixed reference dateTo define the format layout. This reference date is:

2006年1月2日 15点04分05秒 -0700 MST

Or more simply written as:Mon Jan 2 15:04:05 MST 2006.

While usingstampToDateAt that time, the "format string" we provide is actually a template, which should be precise.Copy this part of the reference dateTo represent the expected output format. For example:

  • If you want to display2023-10-26Then the format string should be written as2006-01-02. Here,2006representing a four-digit year,01representing a two-digit month,02Represents two-digit dates.
  • If you want to display14:30:05(24-hour format), then the format string is15:04:05. Here,15Represents hours in a 24-hour clock system,04represents the minute,05represents seconds.
  • If you want to display上午09:30, then the format string can be written as下午03:04, or a more generalPM03:04.

Once you understand this core rule, you will find that formatting time becomes very intuitive and powerful.The issue of the page displaying an error is usually because the format string did not follow the unique convention of the Go language and incorrectly used format symbols from other languages.

Practical tips to avoid format string errors

To ensurestampToDateLabels can work normally and display time as expected, we can follow several practical tips:

  1. Memorize the core reference date:to2006-01-02 15:04:05(Or)Mon Jan 2 15:04:05 MST 2006This reference date should be remembered in mind. Whenever you need any time format, just find the corresponding number in the reference date and use it as the format string.For example, to display the "year", use2006To display 'month', use01.
  2. Check the official AnQi CMS document:Especially the template tag document of AnQi CMS,tag-stampToDate.mdIt provides detailed examples. It not only shows common formats but also lists various complex formatting results supported by the Go language. This is the most authoritative and direct reference material.
  3. Common Format Example Lookup:
    • Complete Date and Time (24-hour format): {{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}}2023-10-26 14:30:05
    • Complete Date and Time (12-hour format with AM/PM): {{stampToDate(item.CreatedTime, "2006-01-02 03:04:05 PM")}}2023-10-26 02:30:05 PM
    • Year-Month-Day: {{stampToDate(item.CreatedTime, "2006年01月02日")}}2023年10月26日
    • Month-Day: {{stampToDate(item.CreatedTime, "01-02")}}10-26
    • Time and hour: {{stampToDate(item.CreatedTime, "15:04")}}14:30
    • Pure year: {{stampToDate(item.CreatedTime, "2006")}}2023
  4. Using built-in formatting parameters (if available):In Anqi CMS, some document or category detail tags (such asarchiveDetail/archiveListofCreatedTimeandUpdatedTimefields) already supportformatParameter. This means that in these specific scenarios, you can format directly inside the tag without needing to call it separatelystampToDate. For example:
    
    {% archiveDetail with name="CreatedTime" format="2006-01-02 15:04" %}
    
    This way is more concise and reduces the possibility of errors. When writing templates, give priority to this built-in formatting method.
  5. Thoroughly test in the development environment:Before deploying the template to the production environment, it is necessary to preview and test all pages that contain time formatting in the local or test environment.Check if each date and time field is displayed as expected.

By understanding the time formatting mechanism of the Go language and combining the above practical skills, we can effectively avoidstampToDateA format string error causes the page display exception, ensure the accurate presentation of website time information, and provide users with a smooth, professional browsing experience.


Frequently Asked Questions (FAQ)

1. I used in the template.stampToDateBut why is it blank or "0000-00-00" displayed on the page?This is usually caused by an incorrect format string setting. Please check the one you enteredstampToDateThe second parameter should ensure that it follows the Go language date formatting rules (i.e., using2006-01-02 15:04:05as a reference template), rather than other programming languages (such as PHP'sY-m-dformat