stampToDateThe philosophy and practice of label usage.

KnowstampToDate: The art of time conversion.

AnQiCMS 为我们提供了一个名为stampToDateThe template tag, its core responsibility is to convert a Unix timestamp (usually 10 digits, representing the number of seconds elapsed since 00:00:00 UTC on January 1, 1970) into a date and time format that we humans are accustomed to reading. For example, when obtaining the publication time of an article, we will usually see something like{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}}This usage. Here isitem.CreatedTime就是一个时间戳,而"2006-01-02 15:04:05"则是 Go 语言特有的参考时间格式,它定义了我们希望日期时间如何呈现。

This reference time format is the convention of the Go language, not what we intuitivelyYYYY-MM-DD HH:MM:SS, but it uses2006年01月02日 15点04分05秒This string of numbers and structure serves as the 'reference object' for formatting. Understanding this is crucial for correct usagestampToDateas the foundation.

Common challenges in the transformation journey.

AlthoughstampToDateTags are powerful and convenient, but in practical application, we occasionally also encounter some minor "interruptions", leading to the date display not being as satisfactory. These challenges mainly arise from the following aspects:

First, one of the most common challenges is the inputstampToDateof the timestamp data itself may have issues. For example,item.CreatedTimethe variable may be empty (nil), or its value may be0.In some cases, it may even be a non-numeric string, or a non-expected non-10-digit timestamp (for example, a millisecond timestamp, but the label expects a second-level timestamp).stampToDateIt may return the zero value of the Go language time type, that is,0001-01-01 00:00:00 +0000 UTCThis format is obviously not what we want to display to the user.

其次,另一个常见问题出在日期格式化字符串上。正如前面提到的,Go 语言的参考时间格式并非直观。如果开发者误将其写成YYYY-MM-DDoryyyy-MM-ddSuch a common format, or if there are spelling errors, the output date may be completely off the mark, for example, it may be displayed as2006-01-02Itself, rather than the actual date after conversion.

Template level “Verification” strategy and practice

We cannot perform in the template level of AnQiCMS,stampToDateFunction execution directly “verifies” the format of its output whether it is “correct” or “valid”, because once the function is executed, it will always try to provide a string result. Therefore, the truly effective “verification” lies inPre-check judgmentandError tolerance handling.

The best method is to callstampToDatebefore, pre-check the timestamp variable passed in. For example, we can useiflogical tags to checkitem.CreatedTimeDoes it exist and make sense. If it does not exist or0, we choose to display a friendly alternative text instead of showing the user a 'Year 1 BC' date.

{% if item.CreatedTime %}
    {# 检查时间戳是否大于0,避免显示0001-01-01这种默认值 #}
    {% if item.CreatedTime > 0 %}
        <div>发布日期:{{stampToDate(item.CreatedTime, "2006年01月02日 15:04")}}</div>
    {% else %}
        <div>发布日期:暂无有效日期</div>
    {% endif %}
{% else %}
    <div>发布日期:信息缺失</div>
{% endif %}

This code segment handles missing or invalid timestamps gracefully, ensuring that users always see reasonable information in any case.If the date displayed on the page does not match the expected one, the first thing to do is to visually check the date string displayed in the browser and compare it with the reference time format in the Go language, to verify whether the formatting string in the template is accurate.For deeper issues, such as whether there is a problem with the timestamp data source, it may be necessary to use the AnQiCMS backend logs or debugging tools to investigate.

Enhance the stability of date display practices

To make the date display on your website more stable and the information more accurate, I suggest you follow the following **practices during template development:**

  1. Always check the timestamp variable:In any callstampToDate habitually use{% if 变量名 %}or{% if 变量名 > 0 %}to ensure that the timestamp data exists and is not zero.
  2. Provide clear alternative text:When the timestamp is invalid or missing, do not leave the page blank or display default zero values, but provide clear prompts such as 'No publication date available' or 'Date to be determined'.
  3. Standardized format string:Throughout the website, try to unify the display format of date and time as much as possible. This not only improves user experience but also reduces debugging problems caused by inconsistent formats. Commonly used ones such as"2006-01-02"(Year-Month-Day) or"2006年01月02日 15:04"(Full Chinese Format).
  4. Sufficient testing:Test date display in different scenarios, including new content, old content, content with missing timestamps, and performance on different browsers and devices.

Through these meticulous operations, you will be able to ensure that the date and time information in the AnQiCMS template is always presented in a professional, accurate, and user-friendly manner, adding a reliability to your website operation.


Common Questions (FAQ)

1.stampToDateanddateWhat are the differences between filters? Which one should I use?

stampToDateTags are used specifically forUnix timestampConverts an integer representing the number of seconds (usually) to a date string in a specified format. Its core function is to handle timestamps.dateThe filter is one of the general filters provided by the AnQiCMS template engine, which expects to process the original Go language.time.TimeType object. If you obtain a Unix timestamp from a database or data model, you should usestampToDate; If some advanced logic has already converted the timestamp totime.TimeObject, or a built-in variable itselftime.TimeType (This situation is less common in AnQiCMS templates, more common in Go backend code), then you can usedatefilters. In handling fromitem.CreatedTimeWhen getting the time information of the field,stampToDateis the preferred and recommended tag.

**2. Why my date