In the daily operation of Anqi CMS, the precise presentation of content is a key factor in improving user experience and website professionalism.Date and time formatting is an indispensable part of it.As an experienced website operator, I am well aware of the readers' requirements for clear and consistent information.To help everyone better utilize the AnqiCMS template functions, this article will elaborate in detailstampToDateHow to format timestamps into specified date and time in AnqiCMS template filters.

Flexible timestamp formatting:stampToDateIntroduction to filters

In the AnqiCMS template system, we often encounter scenarios where it is necessary to display content publishing time, update time, and so on.These time data are usually stored in the form of Unix timestamps, and it is difficult and unattractive for users to display a string of numbers directly. At this point,stampToDateThe filter has become a powerful tool for us to format time.It can convert the standard 10-digit timestamp into a date or time string that we preset and is easy to read, greatly enhancing the readability and user experience of the content.

stampToDateBasic usage of the filter

stampToDateThe syntax of using filters is very intuitive:

{{stampToDate(时间戳, "格式")}}

The two main parameters are:

  • timestamp: Refers to the 10-digit Unix timestamp that needs to be formatted. In AnqiCMS templates, this is usually sourced from a database field, such as the article object'sitem.CreatedTimeoritem.UpdatedTimeFor example,1609470335represented a specific moment.
  • Format: This is a string parameter used to define the date and time display mode after the timestamp is converted. The "format" here is not what we usually understand.YYYY-MM-DDsuch placeholders, but follow the Golang (Go language) specific reference time format.

Mastering the Golang reference time format is the correct way to use it.stampToDateThe key. Golang uses a fixed reference time:2006年01月02日 15时04分05秒 -0700 MSTThis serves as the 'template' for defining formats. This means that when you want to output a certain date and time format, you need to write the representation of the corresponding part in the 'format' string.

For example, if you want to display年-月-日you would write"2006-01-02"; if you want to display时:分you would write"15:04"Let us understand this through some practical examples.

Understand the reference time format of Golang in depth.

Golang's reference time is a very specific value:2006-01-02 15:04:05.999999999 -0700 MSTEach number and letter represents a specific component of the date and time.

  • 2006: Year, such as2023
  • 01: Month (number), such as03Represents March
  • JanuaryorJanuaryMonth (full name or abbreviation)
  • 02Date, such as15
  • MondayorMonDay of the week (full name or abbreviation)
  • 15Hour (24-hour format), such as09Represent 9 a.m.
  • 03: hour (12-hour format), such as09Represent 9 a.m.
  • PMorpm: AM/PM indicator
  • 04: minutes, such as30
  • 05: seconds, such as45
  • .999999999: nanoseconds
  • -0700: time zone offset
  • MST: time zone abbreviation

By combining these reference time elements, we can define various required date and time formats.

Practice examples in AnqiCMS template

Suppose we have a timestamp variablepublishStampIts value is1609470335representing January 1, 2021, 12:25:35 UTC). The following are several common formatting requirements and theirstampToDateUsage:

Display only the date:

  • Year-Month-Day (e.g., 2021-01-01)
    
    {% set publishStamp = 1609470335 %}
    <p>发布日期:{{stampToDate(publishStamp, "2006-01-02")}}</p>
    
  • Year/Month/Day (e.g., 2021/01/01)
    
    {% set publishStamp = 1609470335 %}
    <p>发布日期:{{stampToDate(publishStamp, "2006/01/02")}}</p>
    
  • Date in Chinese characters (e.g., 2021/01/01)
    
    {% set publishStamp = 1609470335 %}
    <p>发布日期:{{stampToDate(publishStamp, "2006年01月02日")}}</p>
    

Only display time:

  • Time in hours:minutes:seconds (24-hour format, e.g., 12:25:35)
    
    {% set publishStamp = 1609470335 %}
    <p>发布时间:{{stampToDate(publishStamp, "15:04:05")}}</p>
    
  • Time in hours:minutes (e.g., 12:25)
    
    {% set publishStamp = 1609470335 %}
    <p>发布时间:{{stampToDate(publishStamp, "15:04")}}</p>
    

Display date and time:

  • Year-Month-Day Time:Minute:Second (e.g., 2021-01-01 12:25:35)
    
    {% set publishStamp = 1609470335 %}
    <p>完整时间:{{stampToDate(publishStamp, "2006-01-02 15:04:05")}}</p>
    
  • Month day, year, time: minute AM/PM (e.g., Jan 01, 2021, 12:25 PM)
    
    {% set publishStamp = 1609470335 %}
    <p>英文格式:{{stampToDate(publishStamp, "Jan 02, 2006, 03:04 PM")}}</p>
    

In practical applications, we usually willstampToDateThe filter is applied to the content object'sCreatedTimeorUpdatedTimeproperty. For example, the publication time of the article is displayed on the article detail page:

{% archiveDetail article with name="CreatedTime" %}
<p>文章发布于:{{stampToDate(article, "2006年01月02日 15:04")}}</p>

or the update time of each article is displayed in the article list:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
    <p>{{item.Title}} - 最后更新:{{stampToDate(item.UpdatedTime, "2006-01-02 15:04")}}</p>
    {% endfor %}
{% endarchiveList %}

stampToDateThe value of the filter for website operation

For website operators,stampToDateFilter is not just a technical detail, but also a part of content management and user experience optimization. By flexibly using this filter, we can:

  • Enhancing user experienceConvert rigid timestamps into human-readable formats so that users can easily obtain content timeliness information.
  • Maintain content consistency: Regardless of the data source, it can uniformly display the date and time format on the front end, maintaining the overall professional image of the website.
  • Convenient for content maintenance and updateThrough clear time information, operations personnel can more easily track the publication and update cycle of content, and timely adjust operational strategies.
  • Supports multilingual and localizationThrough adjusting the format string, it can adapt to the date and time display habits of different language environments.

In short,stampToDateThe filter is a small but powerful tool in the AnqiCMS template.Mastering its use will help you better manage and present website content, providing users with a better browsing experience.


Frequently Asked Questions (FAQ)

Is the timestamp parameter supposed to be 10 digits? What should I do if my timestamp is 13 digits (millisecond level)?

stampToDateThe filter is currently designed to accept standard 10-digit Unix timestamps (second-level). If your timestamp is 13 digits in milliseconds, you need to pass it tostampToDateBefore, first divide it by 1000 to convert it to a second-level timestamp.AnqiCMS template itself may not have the function to perform mathematical operations directly, this usually needs to be handled on the backend data processing or assisted by frontend JavaScript, ensuring that the parameters passed to the filter are 10-digit integers.

Why is the date and time not displayed correctly when I set it according to the format string 'YYYY-MM-DD HH:MM'?

This is a common misconception.stampToDateThe filter uses a reference time format in Golang, not the commonYYYYorMMsuch placeholders. You must use Golang's reference time2006年01月02日 15时04分05秒The corresponding numbers are used to construct the format string. For example, to display年-月-日 时:分you should write"2006-01-02 15:04"instead of"YYYY-MM-DD HH:mm"Make sure to refer to the specific datetime numbers defined in Golang.

except for the article'sCreatedTimeandUpdatedTimeWhat are some other scenarios that can be usedstampToDateFilter?

stampToDateThe filter can be used for any date-time data stored in the form of a 10-digit Unix timestamp.For example, user registration time, comment posting time, order generation time, and so on.If the backend data returns a timestamp and you want to display it in a readable date-time format on the frontend,stampToDateThe filter can be used. You can use it on any template variable that needs to format a timestamp.