As an experienced website operation expert, I know that it is crucial to efficiently and flexibly present information in daily content management.Safe CMS (AnQiCMS) provides powerful content display capabilities to us with its efficient features of Go language and the template syntax style of Django.stampToDateLabel, only extract the year from the timestamp for display.
The time processing philosophy of Anqi CMS template
The AnQi CMS adopts a template engine syntax similar to Django, which makes template writing both intuitive and powerful.Whether it is displaying article titles, content, categories, or handling time data, there are corresponding tags and variables available for us to call.In many scenarios, the time information we obtain from the database is usually Unix timestamp, which is a number representing the number of seconds from 00:00:00 UTC on January 1, 1970, to the current time.stampToDateThe built-in tag, specially used to convert these original timestamps into the date and time format we are familiar with.
Deep understandingstampToDateHow tags work
stampToDateThe core function of this tag lies in its powerful formatting capabilities. Its usage is{{stampToDate(时间戳, "格式")}}。这里的关键在于第二个参数——English格式。它遵循Go语言独特的日期时间格式化规则。与我们可能习惯的YYYY-MM-DDDifferent patterns, 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, when you want to display the year, you should use2006;To display the month, use01or1(Depending on whether a leading zero is needed);To display the date, use02or2This is an example. The numbers themselves in this reference time point represent the respective date and time components.
Actual operation: Only the year is extracted for display.
Now, let's get back 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. Simply need tostampToDateThe format parameter is set to2006and it will intelligently identify and only extract the year information from the timestamp for display.
Suppose you have a field namedpublishStampvariable, which stores a 10-digit Unix timestamp, for example1672502400(This is approximately January 1, 2023). If you want to display the represented year, 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 usually containCreatedTime(Creation time) orUpdatedTime(Updated time) This timestamp field. For example, in a list of articles, 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 %}
So, only the publication year of each article will be displayed next to it, without showing the full date, thus maintaining the neat layout of the page.
More flexible date formatting
stampToDateThe power goes beyond this. By adjusting the format string, you can easily achieve 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(Dependent on time zone)
Proficient in Go language at this reference point will enable you to navigate through the template development of Anqi CMS with ease, whether it's displaying a concise year or detailed date and time, it becomes effortless.
Attention Points and **Practice
- Timestamp accuracy:
stampToDateThe tag expects to receive by default10-digitThe 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. - Go language formatting rules:Remember
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 influenceIn 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.
Common Questions (FAQ)
Q1: WhystampToDateFormat year with2006InsteadYYYY?
A1: This is the uniqueness of the Go language date and time formatting mechanism. Go language does not adoptYYYY-MM-DDThis common placeholder pattern uses a fixed reference time point instead2006-01-02 15:04:05In this reference time point,2006it represents the year,01Represents the month,02represents the date,15represents the hour (24-hour clock),04represents the minutes,05Represents seconds. When you provide a format string, Go will match and output the corresponding time information based on the reference time point part included in your string.
Q2: How can I display the month and date if not the year?
A2: The same reference time point is used in Go language. If you want to display 'Year-Month-Date', the format string is"2006-01-02"; If you want to display 'Month/Date', it can be"01/02"If you want to display "month day", it can be"01月02日". For example:{{stampToDate(item.CreatedTime, "01月02日")}}Will be displayed01月01日.
Q3: My timestamp is 13 digits (millisecond level),stampToDateCan I use tags directly?
A3:stampToDateThe label expects a 10-digit Unix timestamp (second precision).If your timestamp is a 13-digit millisecond timestamp, you need to convert it to a 10-digit one.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 process in the frontend template, consider processing before outputting the timestamp, or ensuring that the timestamp passed to the template is already a 10-digit second-level timestamp.