How to use a filter in AnQiCMS template to convert a timestamp to a readable date format?

Calendar 👁️ 64

In website operation, we often need to display various time information on the front-end page, such as the publication date of articles, content update time, or user comment time.AnQiCMS as an enterprise-level content management system usually uses a highly efficient and concise format such as Unix timestamp for data storage.However, for users visiting the website, a string of numerical timestamps is clearly not intuitive and user-friendly.How can I convert these timestamps into a readable date format in AnQiCMS templates?

AnQiCMS provides a very practical built-in tag and filter, making this conversion process simple and quick, even if you are not well-versed in the underlying details of the Go language, you can easily achieve it.

Understand the timestamp in AnQiCMS templates

Inside AnQiCMS, such as theCreatedTime(creation time) andUpdatedTime(Update time), comments of theCreatedTime, user login logs of theLastLoginThe value is usually stored in the form of a 10-digit or more Unix timestamp.This is an integer representing the number of seconds elapsed since 00:00:00 UTC on January 1, 1970.This storage method is convenient for the system to sort, compare, and handle time zones, but when displaying to users, we would rather see the format such as 'October 27, 2023' or '10:30 AM'.

Core Tool:stampToDateTags/Filter

AnQiCMS provided for usstampToDateThis powerful tag can directly convert timestamps in the template to the readable date format we need.

Its basic usage syntax is very intuitive:{{ stampToDate(您的时间戳变量, "目标日期格式字符串") }}

There are two key parts:

  1. Your timestamp variable: This is usually fromarchiveList/archiveDetailThe field containing the timestamp, for exampleitem.CreatedTimeorarchive.UpdatedTime.
  2. Target date format stringThis is the string that determines the final display style of the date. Unlike some other programming languages, the date formatting string in the Go language (the underlying development language of AnQiCMS) is defined based on a fixed 'reference time', rather than using likeY-m-d H:i:sSuch a placeholder. This神奇 reference time is:2006-01-02 15:04:05.

This means:

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

You just need to replace the numbers in this reference time with the format symbols you want. For example:

  • to displayYear-Month-Date:"2006-01-02"
  • to displayYear/Month/Date:"2006/01/02"
  • to displayYear Month Day (Chinese):"2006年01月02日"
  • to displayHour:Minute:Second:"15:04:05"
  • to displayComplete date and time:"2006-01-02 15:04:05"
  • to displayShort month and day:"01-02"
  • to displayAM/PM hour:"03:04 PM"(Note, here the03Represents the hour in 12-hour format,PMIndicates AM/PM)

Actual application in AnQiCMS template

Let's look at a specific example of how to use it in the article list and article detail pagestampToDate.

Assuming you have a list page of articles and you want to display the publish time and update time of each article, as well as the detailed publish time on the article detail page.

Article list template example ({模型table}/list.htmlor the homepageindex.htmlpart):

<div class="articles-list">
    {% archiveList archives with type="page" limit="10" %}
        {% for item in archives %}
        <article class="article-item">
            <h2><a href="{{item.Link}}">{{item.Title}}</a></h2>
            <div class="article-meta">
                <span>发布于:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}</span>
                <span>更新于:{{ stampToDate(item.UpdatedTime, "2006/01/02 15:04") }}</span>
                <span>阅读量:{{item.Views}}</span>
            </div>
            <p class="article-description">{{item.Description}}</p>
            <a href="{{item.Link}}" class="read-more">阅读更多</a>
        </article>
        {% empty %}
        <p>暂时没有内容。</p>
        {% endfor %}
    {% endarchiveList %}

    {# 如果是分页列表,别忘了加上分页导航 #}
    <div class="pagination-controls">
        {% pagination pages with show="5" %}
            {# 分页代码略,参考AnQiCMS分页标签文档 #}
        {% endpagination %}
    </div>
</div>

In this example:

  • item.CreatedTimeFormatted as "xxxx xx month xx day".
  • item.UpdatedTimeFormatted as "xxxx/xx/xx xx:xx".

Example article detail template ({模型table}/detail.html):

`twig

<h1>{{archive.Title}}</h1>
<div class="article-info">
    <span>发布时间:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15时04分05秒") }}</span>
    <span>更新时间:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05") }}</span>
    <span>阅读量:{{archive.Views}}</span>
</div>
<div class="article-content">
    {{archive.Content|safe}}
</div>

<div class="article-tags">
    {% tagList tags with itemId=archive.Id limit="5" %}
        {% for tag in tags %}
        <a href="{{tag.Link}}">{{tag.Title}}</a>
        {% endfor %}
    {% endtagList %}
</div>

<div class="navigation-links">
    {% prevArchive prev %}
        {% if prev %}<a href="{{prev.Link}}">上一篇:{{prev.Title}}</a>{% else %}<span>没有上一篇了</span>{% endif %}
    {% endprevArchive %}
    {% nextArchive next %}

Related articles

How does AnQiCMS automatically extract the first image of the article content as a thumbnail?

In content operation, matching a suitable thumbnail for each article can not only enhance the visual appeal of the website, but also quickly attract users' attention in the information stream.However, manually uploading and adjusting thumbnails for each article, especially when the volume of content is large, is undoubtedly a time-consuming and labor-intensive task.AnQi CMS knows this pain point and provides an intelligent and efficient thumbnail automatic extraction mechanism, making content publishing more hassle-free.

2025-11-08

How to retrieve and display custom parameters of an article on the article detail page (such as author, source)?

Manage and display content in Anqi CMS, the article detail page is undoubtedly the core position for users to obtain information.In addition to basic elements such as article titles and text, we often need to display some personalized information on the detail page, such as the author of the article, source of content, product specifications, and release location, etc.This information is called "Custom Parameters" in Anqi CMS, which can greatly enrich the dimensions of content and meet the personalized needs of different business scenarios.The AnQi CMS provides a very flexible way to define and display these custom parameters, allowing website operators to meet their actual needs

2025-11-08

How does AnQiCMS manage image resources and display them by category for easy front-end calling?

In website content operation, images are indispensable elements to attract users and convey information.Efficiently manage these image resources and be able to call them flexibly on the front-end page, which is crucial for improving the user experience and operational efficiency of the website.AnQiCMS provides a comprehensive and flexible solution in this regard, making the management and invocation of image resources simple and orderly. ### Centralized Management and Categorization of Image Resources AnQiCMS provides a centralized management platform for website image resources, where you can easily upload, store, and organize all visual content.In the background management interface

2025-11-08

How to display the thumbnail of each article in the article list?

In content operation, the thumbnail of the article list page plays a crucial role.They can quickly attract visitors' attention, increase click-through rate, and make the page content clear at a glance, greatly improving the user experience.AnQiCMS (AnQiCMS) is a comprehensive content management system that provides a very flexible and easy-to-operate way to display thumbnails of each article in the article list.Next, let's discuss in detail how to implement this function.Why are thumbnails so important? Imagine you are browsing a news website

2025-11-08

How to implement the filtering function of the article list in AnQiCMS, displaying different results according to custom parameters?

How to make it easier for users to find the content they need on the website is the key to improving user experience and conversion rates.The filtering function of the article list is an important means to achieve this goal.AnQiCMS provides a flexible and powerful mechanism that allows us to easily filter and display different list results of articles based on custom parameters, which is very helpful for building personalized and efficient content platforms.### The Foundation of Building a Filtering Function: Custom Content Model To implement an article list filter based on custom parameters

2025-11-08

How to safely parse a URL string into a clickable a tag in AnQiCMS template?

In website content operation, we often need to display some website URLs, email addresses, and other information in articles, introductions, or custom fields.If this information is just plain text, users cannot click to jump directly, which not only affects the user experience but may also reduce the interactivity of the content and the SEO friendliness of the website.Convert these URL strings securely and intelligently into clickable `<a>` tags, which is a very practical feature in the AnQiCMS template.AnQiCMS as an enterprise-level content management system has fully considered the flexibility and security of content display from the beginning of its design

2025-11-08

How to enable anti-crawling interference code and image watermark in AnQiCMS to protect the display of content?

In today's era of increasingly rich digital content, protecting original content on websites is particularly important.AnQiCMS (AnQiCMS) understands the pain points of content creators and corporate users, and has specifically built anti-crawling interference codes and image watermarking functions to help users effectively prevent their content from being maliciously scraped and stolen, while also strengthening brand identity.### Protect content security: Enable anti-capture interference code The value of original content is self-evident, but information collection tools on the Internet may copy your articles in large quantities without permission, which not only harms the rights and interests of the original creators but may also divert website traffic

2025-11-08

How to implement pagination display and like function for comment content in AnQiCMS?

In website operation, user comments and interaction are the key to enhancing the vitality of content.AnQiCMS (AnQiCMS) provides powerful content management capabilities, among which the comment feature is an important bridge for user interaction with content.This article will discuss in detail how to implement pagination for comment lists in Anqi CMS, as well as add a like function for comment content, enhancing the interactive experience of your website to a higher level.### CMS Review Function Overview The CMS has built-in article comment and comment management functions from the early version, which provides native user interaction support for the website

2025-11-08