How to format a timestamp to a specified datetime using the `stampToDate` filter in AnqiCMS template?

Calendar 👁️ 58

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.

Related articles

How to implement pagination display and style control for document list using the `pagination` tag?

As a website operator who deeply understands the operation of AnQiCMS, I know the subtle art of content presentation lies in how to efficiently and elegantly serve users.For a document list page containing a large amount of content, a reasonable pagination mechanism not only improves user experience but also optimizes website performance.In AnQiCMS, the `pagination` tag is the core tool to achieve this goal, allowing us to flexibly control the pagination display and style of the document list.

2025-11-06

How to display all tags and document lists under tags in `tagList` and `tagDataList` tags?

As an experienced website operator who deeply understands the operation of Anqi CMS, I fully understand the core role of content organization and user experience in the success of a website.Tags are the important bridge connecting these two, they not only help us effectively classify and manage a vast amount of content, but also guide users to quickly find the information they are interested in.In Anqi CMS, the `tagList` and `tagDataList` template tags are the key tools to achieve this goal, allowing us to flexibly display tags and associated documents under the tags on the website front-end.###

2025-11-06

How to get the category list and detailed information of a single category with the `categoryList` and `categoryDetail` tags?

As a senior AnQi CMS website operation personnel, I know that the flexibility of content organization and presentation is crucial for attracting and retaining users.The site classification structure is not only the skeleton of content, but also an important path for users to explore and discover information.Today, let's delve deeply into the two core template tags in Anqi CMS, `categoryList` and `categoryDetail`, and how they help us efficiently obtain and display category lists and individual category details.## Flexible Construction of Category Navigation: `categoryList`

2025-11-06

How to get the detailed information of a specified document, including custom fields, using the `archiveDetail` tag?

In the daily operation of AnQi CMS, obtaining and displaying detailed information of website content is one of the core tasks.The `archiveDetail` tag is a powerful tool specifically designed for this purpose in the Anq CMS template system, allowing us to flexibly extract detailed data from any specified document, including standard document properties and user-defined field content.

2025-11-06

What are the core features and highlights of AnQiCMS?

As an experienced website operator, I have a deep understanding of the powerful functions of AnQiCMS and the actual value it brings to small and medium-sized enterprises, self-media operators, and multi-site managers.AnQiCMS is an enterprise-level content management system developed based on the Go language, and its design philosophy always revolves around providing an efficient, customizable, and easy-to-expand content management solution.It not only helps users simplify the content publishing process, but also helps websites stand out in the fierce market competition through a series of refined functions.

2025-11-06

How can AnQiCMS help small and medium-sized enterprises and content operation teams improve efficiency?

## AnQi CMS: A powerful tool to help small and medium-sized enterprises and content operation teams achieve efficient growth In today's digital wave, small and medium-sized enterprises and content operation teams are facing increasingly growing challenges in content management.From how to efficiently create, edit, and publish content, to how to improve the visibility of content in search engines, and how to effectively manage multi-site and multi-language content, every step tests the team's operational efficiency and market competitiveness.AnQiCMS (AnQiCMS) is an enterprise-level content management system designed to address these pain points, and it relies on its simple and efficient architecture and rich practical functions

2025-11-06

How to implement unified operations for the multi-site management function in AnQiCMS?

As a website operator who has been deeply involved with AnQiCMS for many years, I am well aware that in today's complex network environment, enterprises and content creators often need to manage multiple brand sites, sub-sites, or independent platforms established for different markets and user groups in different languages.The multi-site management function of AnQi CMS is specifically designed to meet such needs, it not only simplifies the cumbersome operational processes, but also provides strong support at the level of unified operations.### AnQiCMS

2025-11-06

How to customize the content model in AnQiCMS to meet personalized needs?

As an experienced website operator familiar with Anq CMS, I know that flexible content management is the key to improving user experience and realizing personalized operation.The powerful function of Anqi CMS in customizing content models is the core weapon that allows us to flexibly meet various business needs.Today, I will give a detailed explanation of how to customize the content model in AnQiCMS to meet your personalized needs.

2025-11-06