As an experienced website operations expert, I know that it is crucial to efficiently and flexibly display information in daily content management.AnQiCMS (AnQiCMS) leverages the efficiency of the Go language and the template syntax of Django style, providing us with powerful content display capabilities.Today, we will focus on a very practical tip in the Anq CMS template: how to usestampToDateLabel, only extract the year from the timestamp for display.

AnQi CMS template's time processing philosophy

AnQi CMS uses a template engine syntax similar to Django, which makes template writing both intuitive and powerful.Whether it is to display article titles, content, categories, or handle time data, there are corresponding tags and variables available for us to call.In many scenarios, the time information we retrieve from the database is usually Unix timestamp, which represents the number of seconds from 00:00:00 on January 1, 1970, to the current time.Directly displaying these numbers is not user-friendly, therefore, AnQiCMS provides a name calledstampToDateThe built-in tag is specifically used to convert these original timestamps into the familiar date and time format.

Deep understandingstampToDateThe principle of operation of tags

stampToDateThe core function of the tag lies in its powerful formatting capabilities. Its usage is{{stampToDate(时间戳, "格式")}}The key here is the second parameter格式It follows the unique date and time formatting rules of the Go language. Different from what we might be accustomed toYYYY-MM-DDThe pattern is different, Go language uses a specificreference time pointto define the format, this reference time point is fixed as:

2006年01月02日 15时04分05秒

This means that when you want to display the year, you use2006; Use when you want to display the month01or1(Depending on whether you need a leading zero); Use when displaying the date02or2This is an example. The numbers themselves in this reference time point represent the corresponding date and time components.

Actual operation: Only extract the year for display.

Now, let's return to the core issue: how to extract only the year from the timestamp for display in the Anqi CMS template?

The answer is hidden in the formatting rules of the Go language. Just need tostampToDateThe format parameter is set to2006It will intelligently identify and only extract the year information from the timestamp for display.

Suppose you have a variable namedpublishStampA variable, storing a 10-digit Unix timestamp, such as1672502400(This is approximately January 1, 2023). If you want to display the year it represents, you can write it like this:

{# 假设 publishStamp 是一个Unix时间戳变量 #}
<div>文档发布年份:{{stampToDate(publishStamp, "2006")}}</div>

The output on the page will be:

文档发布年份:2023

This is very concise and intuitive. In the actual content list or detail page of Anqi CMS, documents (archive) or comments (comment) and other data will usually containCreatedTime(Creation time) orUpdatedTime(Updated time) such timestamp fields. For example, in an article list, you may want to only display the year of the article's publication:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
    <li>
        <a href="{{item.Link}}">
            <h5>{{item.Title}}</h5>
            <div>发布于:{{stampToDate(item.CreatedTime, "2006")}}</div> {# 只显示年份 #}
        </a>
    </li>
    {% endfor %}
{% endarchiveList %}

Thus, next to each article, only the year of publication will be displayed, without showing the full date, maintaining the cleanliness of the page layout.

More flexible date formatting

stampToDateThe power far exceeds this. By adjusting the format string, you can easily implement various date and time display methods. For example:

  • Display the full date (Year-Month-Day):{{stampToDate(item.CreatedTime, "2006-01-02")}}output:2023-01-01
  • Show a date with Chinese:{{stampToDate(item.CreatedTime, "2006年01月02日")}}output:2023年01月01日
  • Display the full date and time:{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}}output:2023-01-01 08:00:00(depending on the time zone)

Mastery of this reference point in Go language will allow you to navigate the Anqi CMS template development with ease, whether it's displaying a simple year or detailed date and time, it becomes effortless.

Cautionary notes and **practice

  • Timestamp accuracy:stampToDateThe label expects to receive by default10 digitsThe Unix timestamp (in seconds). If your timestamp is a 13-digit millisecond timestamp, you need to divide it by 1000 to convert it to seconds.
  • Formatting rules for the Go languageRemember:2006-01-02 15:04:05This magical string is the essence of Go language date formatting. It is not a placeholder, but a literal reference value.
  • Cache impactAfter developing or modifying a template, if the front-end page does not update immediately, don't forget to clear the cache of AnQi CMS to ensure that the latest template code is loaded correctly.

Frequently Asked Questions (FAQ)

Q1: WhystampToDateIt is used to format the year2006instead ofYYYY?

A1: This is the uniqueness of the Go language date and time formatting mechanism. Go language does not adoptYYYY-MM-DDThis common placeholder pattern, instead, uses a fixed reference point2006-01-02 15:04:05In this reference point,2006represents the year,01represents the month,02represents the date,15represents the hour (24-hour format),04represents the minute,05Represents seconds. When you provide a format string, Go will match and output the corresponding time information based on the reference time point included in the string.

Q2: How can I display the month and date besides the year?

A2: Using the reference time point of the Go language. If you want to display "year-month-day", the format string is"2006-01-02"; If you want to display "month/day", it can be"01/02"If you want to display "01月02日". For example:{{stampToDate(item.CreatedTime, "01月02日")}}Will be displayed01月01日.

Q3: My timestamp is 13 digits (millisecond level),stampToDateCan I use the label directly?

A3:stampToDateThe label expects a 10-digit Unix timestamp (second-level).If your timestamp is a 13-digit millisecond timestamp, you need to convert it to 10 digits first.Although the template syntax of Anqi CMS does not provide a direct division filter to handle this problem, it is more appropriate to handle it in the Go backend code or through other means (such as conversion during database queries).If it is necessary to handle it in the front-end template, consider processing it before outputting the timestamp, or ensure that the timestamp passed to the template is already a 10-digit second-level timestamp.