How to use the `stampToDate` filter to format a timestamp into a readable date format in AnQiCMS template?

Calendar 👁️ 66

In AnQiCMS template development, we often encounter the need to convert the timestamp data stored in the background into a clear and readable date and time format on the user interface. For example, the original timestamp, such as1609470335This has no meaning for ordinary users. Appropriately displaying time information is particularly important to enhance the user experience of the website.It's lucky that AnQiCMS provides a very practical built-in filter-stampToDateIt can help us easily achieve this goal.

Core function:stampToDateIntroduction to filters

stampToDateThe filter is a tool specifically used for timestamp formatting in the AnQiCMS template engine.Its main function is to convert a series of digital timestamps into a date and time string that is easy to understand according to the specified format.No matter whether you want to display the publication date of the article, the update time of the product, or any other information based on a timestamp,stampToDateAll of them can provide flexible and powerful support.

Master the usage method: easily format timestamps

stampToDateThe filter usage is very intuitive, it requires two key parameters:

  1. Timestamp (Timestamp)This is usually a 10-digit integer representing the number of seconds from the Unix epoch (January 1, 1970, 00:00:00 UTC) to the present. In AnQiCMS templates, this timestamp usually comes from database fields such as article objectsCreatedTimeorUpdatedTime.

  2. Format String (Format String)This is the key to determining the date and time display style. Since AnQiCMS is developed based on the Go language, the format string here follows the unique style of Go language.参考时间Rule. The reference time for the Go language is:2006-01-02 15:04:05 -0700 MST.

    To understand this format string, you need to treat Go's reference time as a template.What you want to display in the final output, use the corresponding part in the reference time to represent. For example:

    • 2006Represents the year
    • 01Represents the month
    • 02Represents the date
    • 15representing hours (24-hour system)
    • 04representing minutes
    • 05representing seconds

    By analogy, if you want to display "year-month-day", you can use:"2006-01-02"as the format string.

The basic syntax structure is:{{ stampToDate(时间戳变量, "格式字符串") }}

Let's go through an example timestamp:1609470335(Indicating2021-01-01 00:25:35) to demonstrate:

Useful Case: Convert Timestamp to Multiple Date Formats

Suppose we have a timestamp variablepublishStampIts value is1609470335.

  • Only display the date (year-month-date): If you only need to display the specific year, month, and day, you can use"2006-01-02"as the format string.

    <div>发布日期:{{ stampToDate(publishStamp, "2006-01-02") }}</div>
    {# 输出: 发布日期:2021-01-01 #}
    
  • Display Chinese Date FormatIf you want the date to be displayed in Chinese, just add Chinese characters to the format string.

    <div>发布日期:{{ stampToDate(publishStamp, "2006年01月02日") }}</div>
    {# 输出: 发布日期:2021年01月01日 #}
    
  • Display the full date and time To display minutes and even seconds, you can add reference numbers for hours, minutes, and seconds.

    <div>发布时间:{{ stampToDate(publishStamp, "2006-01-02 15:04:05") }}</div>
    {# 输出: 发布时间:2021-01-01 00:25:35 #}
    
  • Custom delimiter: You can change the separator between year, month, and day according to your needs, for example, using a slash.

    <div>日期:{{ stampToDate(publishStamp, "2006/01/02") }}</div>
    {# 输出: 日期:2021/01/01 #}
    
  • Only show the time: If you only want to display the time part, you can combine it like this.

    <div>精确到分钟:{{ stampToDate(publishStamp, "15:04") }}</div>
    {# 输出: 精确到分钟:00:25 #}
    

In the AnQiCMS template, we often obtainarchiveListorarchiveDetailthe timestamp from the article object obtained by the tag. For example, the creation time of an article can be obtained throughitem.CreatedTimeorarchive.CreatedTimeGet it.

{# 在文章列表中,显示每篇文章的创建时间 #}
{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <div>
        <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
        <p>发布于:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}</p>
    </div>
    {% endfor %}
{% endarchiveList %}

{# 在文章详情页,显示文章的更新时间 #}
{% archiveDetail archive with name="CreatedTime" %}
    <p>最近更新:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04") }}</p>
{% endarchiveDetail %}

Advanced Techniques: Origin and Application of Timestamps

Many data models in AnQiCMS include timestamp fields, such as:

  • Document (Archive):CreatedTime(creation time),UpdatedTime(Updated Time)
  • Category: Usually does not directly contain a timestamp, but can obtain the time of the latest article below.
  • Tag: Similarly, it does not directly contain a timestamp.

When you are usingarchiveList/archiveDetailWhen looping or fetching content with tags, these timestamp fields will be returned along with the data. You just need to use them asstampToDatethe first parameter.

{# 假设在一个循环中 item 代表一个文章对象 #}
<span>{{ stampToDate(item.CreatedTime, "2006/01/02") }}</span>

This ensures that regardless of where the data comes from, as long as it is a standard Unix timestamp,stampToDateit can be formatted uniformly and accurately.

Summary

stampToDateThe filter is a key tool in AnQiCMS templates for handling timestamps, converting complex numbers into user-friendly date and time expressions. By understanding the reference time template in Go language.2006-01-02 15:04:05 -0700 MSTYou can flexibly customize any required date and time format. Mastering this filter will significantly enhance the readability and professionalism of your website content.


Frequently Asked Questions (FAQ)

Ask: Why is my time formatted output0001-01-01or1970-01-01?Answer: This usually means that you have providedstampToDateThe filter timestamp is an invalid value (such as 0, null, or an empty string) or not obtained correctly. In Unix timestamp, 0 represents January 1, 1970, and in Go language,time.TimeThe zero value is January 1, 0001. Please check if the timestamp variable you passed is empty or contains the correct 1

Related articles

How to get and display the content and information of a single page (such as "About Us" or "Contact Us") in AnQiCMS template?

Manage and display single-page content in AnQi CMS, such as "About Us", "Contact Us", or "Terms of Service", which is a very common requirement in website operation.AnQi CMS provides developers with intuitive and powerful template tags, allowing you to easily retrieve and display information on the front end of the website.Understanding Single Page Content Management in AnQi CMS Firstly, we need to understand how AnQi CMS manages these 'single pages'.In the "Page Resources" module in the background, you can find the "Page Management" feature.

2025-11-07

How does AnQiCMS handle and display image resources, including automatic compression, conversion to WebP format, and watermark management?

In website content operation, images are indispensable elements to attract visitors and convey information, but their size, loading speed, and copyright protection are also focuses for operators.AnQiCMS as a content management system is well-versed in this field, therefore, it has integrated a series of intelligent image resource processing and optimization functions, aiming to help us manage website images more efficiently and effortlessly, and improve user experience and SEO performance.

2025-11-07

How to enable multilingual support for AnQiCMS website and display language switch options in the front-end template?

Under the wave of globalization, websites no longer only serve local users. How to allow visitors from different language backgrounds to access and understand the content of the website without barriers has become an important challenge for many operators.AnQiCMS (AnQiCMS) fully understands this need, providing a flexible and powerful multilingual support solution to help websites easily expand into the international market.This article will introduce how to enable multilingual functionality on the AnQiCMS website and guide you to integrate language switching options in the front-end template to enhance user experience.

2025-11-07

How AnQiCMS dynamically generates and displays page Title, Keywords, and Description (TDK) to optimize SEO?

In website operation, to make your content stand out in search engines, attract more target users, and cannot do without the careful setting of these three core elements: Title, Keywords, and Description (TDK).AnQiCMS knows the importance of TDK for SEO, and has integrated a complete dynamic TDK generation and management mechanism from the beginning of the system design, allowing you to flexibly and efficiently optimize every page of your website.

2025-11-07

How to use `truncatechars` or `truncatewords` filters to truncate long text or HTML content and add an ellipsis?

In website content management, we often encounter such situations: in order to maintain the neat and unified layout of the page, we need to truncate the long text content without destroying its semantics or layout.Whether it is an article summary, product introduction, or user comment, appropriately controlling the length of the text and adding ellipses can greatly enhance the user experience.The template engine of AnQiCMS provides us with two very practical filters: `truncatechars` and `truncatewords`, as well as their variants for handling HTML content

2025-11-07

How to convert a numeric string (such as "5.5") to a floating-point number or integer for display and calculation in AnQiCMS template?

In Anqi CMS template development, we often encounter scenarios where we need to handle numeric string.For example, the price stored in the background custom field may be a text type "5.5", or the numbers in the data obtained through the external API may exist as strings.At this point, if a direct numerical operation is performed, problems may arise.However, AnqiCMS's template engine provides very flexible and powerful functions, which can easily convert numeric strings to floating-point numbers or integers and perform subsequent display and calculation.

2025-11-07

How to use the `cut` or `removetags` filter to remove specific characters or HTML tags from strings in the AnQiCMS template?

In AnQi CMS template design, in order to better control the display of content, the system provides a variety of flexible filters (Filter).These filters can help you perform various string processing operations when outputting variables, such as removing specific characters or HTML tags, making the content neater and meeting your display requirements.Today, we will focus on the practical filters `cut` and `removetags`.

2025-11-07

How to use the `default` filter to set a default display value for possibly empty variables?

In website content management, we often encounter a situation where a certain data variable may not have a value on some pages or under certain conditions, that is, "empty".If the template directly outputs these blank variables, ugly gaps will appear on the page, which not only affects the aesthetics but may also confuse the visitor.In order to avoid such embarrassment, AnQi CMS provides a very practical tool - the `default` filter, which can help us set a graceful default display value for these variables that may be empty.

2025-11-07