How to use the `stampToDate` tag to format a timestamp into a custom date and time format?

Calendar 👁️ 64

Good, I am very happy to delve into the AnQiCMS for you.stampToDateTags, help you easily handle the time display on the website.


Flexible time handling: Make use of AnQiCMS's.stampToDateLabel customization date and time format

In website content management, the way dates and times are displayed often directly affects user experience and the clarity of information communication.A concise, readable date format can help visitors quickly understand the timeliness of the content.AnQiCMS as an efficient content management system, knows this well and provides powerful and easy-to-usestampToDateLabel, allowing you to easily format the Unix Timestamp stored in the database into various custom date and time formats.

Understanding Timestamps: The Source of Your Content's Time

Within AnQiCMS, there are many time-related pieces of information, such as the publication time of articles (CreatedTime)、Update Time(UpdatedTime), comment time, even the latest login time of the user, are all stored in the form of timestamps.Timestamp is typically a 10-digit number representing the number of seconds elapsed since 00:00:00 on January 1, 1970, Greenwich Mean Time (UTC). For example,1678886400Such numbers are very efficient for computers, but it is difficult for ordinary visitors to understand their exact meaning.

This brings up our need for time formatting, converting these original numbers into a format that people are accustomed to reading, such as "2023-03-15 10:00:00" or "3 days ago."}

stampToDateThe core usage of tags

stampToDateThe usage of tags is very intuitive:

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

The key lies in the second parameter - the format string. Many other systems adoptYYYY-MM-DDThis placeholder is different, AnQiCMS internally uses the Go language time formatting standard, which has a fixed 'reference time'.You need to replace the date and time elements you expect to display with the corresponding part of this reference time.This reference time is:

2006-01-02 15:04:05.999999999 -0700 MST(usually abbreviated as2006-01-02 15:04:05)

This is like a 'magic number', which is not an actual year but a placeholder used by the Go language to identify the position of time elements. Just remember that when you want to display the year in the result, just write2006; If you want to display the month, you write01; To display the date, write02and so on.

Let's understand it through some examples of common formats:

  • Only display the year: {{ stampToDate(publishStamp, "2006") }}2023
  • Year-Month-Day: {{ stampToDate(publishStamp, "2006-01-02") }}2023-03-15
  • Year/Month/Day: {{ stampToDate(publishStamp, "2006/01/02") }}2023/03/15
  • Month/Day/Year (commonly used for internationalization): {{ stampToDate(publishStamp, "01/02/2006") }}03/15/2023
  • Hour:Minute:Second (24-hour format): {{ stampToDate(publishStamp, "15:04:05") }}10:00:00
  • Year-Month-Day Hour:Minute: {{ stampToDate(publishStamp, "2006-01-02 15:04") }}2023-03-15 10:00
  • Complete format with Chinese: {{ stampToDate(publishStamp, "2006年01月02日 15时04分05秒") }}2023年03月15日 10时00分00秒
  • When is the day: {{ stampToDate(publishStamp, "Mon") }}Wed(Wednesday)

Please note that other characters in the format string, such as dashes, slashes, colons, spaces, and Chinese characters, will be output as is, serving as separators or descriptions.

tostampToDateApply to your website: actual scenario examples

stampToDateTags are everywhere in AnQiCMS template development, especially in scenarios where content publishing or update time needs to be displayed.

1. Display the publication and update time on the document detail page

When you are browsing an AnQiCMS article, you will usually see its publication date. Assume you arearticle/detail.htmlIn the template, you can usearchiveDetailLabel to get document details and format the timestamps within.

<article>
    <h1>{% archiveDetail with name="Title" %}</h1>
    <div class="meta-info">
        <span>发布于:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}</span>
        <span>更新于:{{ stampToDate(archive.UpdatedTime, "2006-01-02") }}</span>
        <span>浏览量:{% archiveDetail with name="Views" %}</span>
    </div>
    <div class="article-content">
        {% archiveDetail articleContent with name="Content" %}{{ articleContent|safe }}
    </div>
</article>

This code will takearchive.CreatedTimeFormat to an accurate Chinese date and time to the minute, butarchive.UpdatedTimeonly show to the day.

2. The article publication date is displayed on the document list page

On the article list page, you may want to display the publication date next to each article so that visitors can quickly understand the order and timeliness of the content.

<ul class="article-list">
    {% archiveList archives with type="page" limit="10" %}
        {% for item in archives %}
        <li>
            <a href="{{ item.Link }}">
                <h3>{{ item.Title }}</h3>
                <p>{{ item.Description|truncatechars:100 }}</p>
                <div class="list-meta">
                    <span>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
                    <span>分类:{% categoryDetail with name="Title" id=item.CategoryId %}</span>
                    <span>阅读量:{{ item.Views }}</span>
                </div>
            </a>
        </li>
        {% empty %}
        <li>暂无文章内容。</li>
        {% endfor %}
    {% endarchiveList %}
</ul>

here,item.CreatedTimeIs formatted in each loop.YYYY-MM-DDForm.

3. Comment time displayed in comments

If your website has a comment function, then the posting time of each comment is also a focus for users.

`twig

<h4>用户评论</h4>
{% commentList comments with archiveId=archive.Id type="list" limit="5" %}
    {% for item in comments %}
    <div class="comment-item">
        <strong>{{ item.UserName }}</strong>
        <span class="comment-time">于 {{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }} 评论</span>
        <p>{{ item.Content }}</p>
    </div>
    {% empty %}
    <p>暂无评论。</p>
    {% endfor %}
{%

Related articles

How to correctly configure the `pagination` tag to generate a functional pagination navigation on the list page?

In website content management, configuring a fully functional pagination navigation for the list page is a key factor in improving user experience and optimizing the website structure.AnqiCMS (AnqiCMS) provides a simple and powerful `pagination` tag, allowing developers to easily meet this need.To make the `pagination` tag generate the pagination navigation correctly on the list page, it is first necessary to understand its mechanism and the collaborative relationship with content list tags (such as `archiveList`).

2025-11-07

How to get and paginate the display of all relevant documents under a specific `tagDataList` tag?

When managing content in Anqi CMS, tags (Tag) are an important tool for organizing and categorizing information.It can not only help users quickly find relevant content, but also significantly improve the internal link structure and SEO performance of the website.When it is necessary to display all relevant documents under a specific tag and to manage pagination, the `tagDataList` tag becomes an indispensable core function.

2025-11-07

How to use the `prevArchive` and `nextArchive` tags to navigate to adjacent documents in the article detail page?

In today's content-driven world, the user experience of the website article detail page is particularly important.When a reader is immersed in an excellent content, if they can navigate smoothly to related or adjacent articles, not only can it extend their stay on the website and increase the page views, but it can also indirectly optimize the search engine crawling and ranking through the internal link structure.AnQi CMS is an efficient and SEO-friendly content management system that fully considers these details and provides simple and intuitive template tags to help us easily achieve this function. Today

2025-11-07

How to implement hierarchical display and nested calling of the `categoryList` tag?

Building a clear and organized website navigation in AnQiCMS is crucial for improving user experience and website accessibility, especially when dealing with multi-level categories.The `categoryList` tag was created for this purpose, providing powerful features to help us flexibly display the hierarchical structure of categories and make fine-grained nested calls.

2025-11-07

How to dynamically generate page title, keywords, and description with the `tdk` tag and flexibly control whether to include the website name?

In website operation, the page title (Title), keywords (Keywords), and description (Description), which we commonly refer to as TDK, are the foundation of search engine optimization.They can not only help search engines understand the page content, but are also key factors to attract users to click.AnQiCMS (AnQiCMS) fully understands the importance of TDK, and through its powerful template tag system, especially the versatile `tdk` tag, makes dynamic generation and flexible control of page TDK easy and efficient.

2025-11-07

How to construct a website navigation with multi-level submenus and associated content using the `navList` tag?

When building a website, a clear and easy-to-use navigation system is crucial, as it directly affects whether users can quickly find the information they need and also influences how search engines understand the structure of the website.AnQiCMS provides a powerful `navList` tag, allowing us to flexibly build a navigation system with multi-level submenus and closely associate navigation items with website content.Next, we will delve into how to use the `navList` tag to create a set of multi-level navigation that is both beautiful and practical for your website.### Website Navigation Basics

2025-11-07

How to create a dynamic breadcrumb navigation using the `breadcrumb` tag and customize the home page name and title display?

In website operation, a clear navigation path is crucial for user experience and search engine optimization (SEO).Breadcrumb Navigation is like a small map of a website, allowing visitors to always know their location and easily return to the previous page.Today, let's delve deeper into how to use the powerful `breadcrumb` tag in Anqi CMS to create dynamic breadcrumb navigation and customize the home page name and title display according to our needs.

2025-11-07

How to perform content filtering, batch replacement, and recycle bin operations in the document management feature of the AnQiCMS backend?

The content management system is the core of website operation, especially for sites that update content frequently, efficient document management functions are crucial.AnQiCMS as a powerful content management system, provides a rich and practical background document management tool, helping operators easily handle daily content maintenance work.This article will delve into the functionality of AnQiCMS backend in content filtering, batch replacement, and recycle bin operations, helping you manage website content more efficiently.### Efficient Filtering: Quickly Locate Target Content Quickly locate the required information in the vast amount of website content

2025-11-07