How to format the timestamp into the display format of 'Year-Month-Day Hour:Minute' in AnQiCMS template?

Calendar 👁️ 87

In website content management, the clear display of time information is crucial for user experience.Whether it is the release date of the article or the submission time of the comments, a format that conforms to the reading habits can make information communication more effective.AnQiCMS provides flexible template functionality, allowing us to easily format timestamps.

Core Function: AnQiCMS timestamp formatting tagstampToDate

AnQiCMS template engine built-in a namedstampToDateThe label, specifically used for formatting timestamps.This tag can convert a Unix timestamp (usually 10 digits) into the date and time string we read in daily life.

{{stampToDate(时间戳变量, "格式化字符串")}}

The key here lies in the second parameter - 'format string'. It is not what we commonly seeYYYY-MM-DD HH:mmThis type of placeholder, but follows the unique time formatting rules of the Go language.

Understand the time formatting rules of the Go language

Go language uses a very unique and powerful method for time formatting in its design.It does not use abstract symbols, but instead uses a fixed reference point as the template for the format.

2006-01-02 15:04:05

To format a timestamp into the style you want, you need to replace the corresponding numbers and separators in the reference time with the ones you want to display in the final result. For example:

  • If you want to display the year, use2006.
  • If you want to display the month, use01.
  • If you want to display the date, use02.
  • If you want to display the hour in 24-hour format, use15.
  • If you want to display minutes, use04.
  • If you want to display seconds, use05.

If a part of the reference time (such as06Representing 06 seconds) It is not needed in your target format, so just ignore it.

Actual operation: Format the timestamp as “Year-Month-Day Hour:Minute”

Now, let's solve the core issue: how to format the timestamp in the AnQiCMS template to the display format of "year-month-day hour:minute".

Suppose we are iterating over an article list in a template, and each article has aCreatedTimefield that stores a Unix timestamp.

To achieve the target format "Year-Month-Date Time:Minute", we refer to the reference time in the Go language2006-01-02 15:04:05:

  • 2006Corresponding year
  • 01Corresponding month
  • 02Corresponding date
  • 15Corresponding to the hour in 24-hour format
  • 04Corresponding to minutes

Therefore, our formatted string should be written as"2006年01月02日 15:04".

Below is an example inarchiveLista tag used in the complete example:

{% archiveList archives with type="list" limit="5" %}
    {% for item in archives %}
        <div class="article-item">
            <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
            <p class="article-meta">
                发布时间:{{ stampToDate(item.CreatedTime, "2006年01月02日 15:04") }}
                <span> - 浏览量:{{ item.Views }}</span>
            </p>
            <p>{{ item.Description }}</p>
        </div>
    {% endfor %}
{% endarchiveList %}

In this code block,item.CreatedTimeIs the creation timestamp of the article. ThroughstampToDateTags and"2006年01月02日 15:04"This formatting string will eventually display on the page in a format similar to '2023 October 27 10:30'.

Common application scenarios

This time formatting method can be applied to various scenarios in the AnQiCMS template:

  • Article and product detail page:Display the publish date, update date.
  • Comment list:Display the submission time of each comment.
  • Event page:Mark the start and end time of the event.
  • User center:Show user registration time, last login time, etc.

Wherever you need to display timestamps in a specific humanized format,stampToDatetags can provide an effective solution.

Points to note

  • The precision of Go language format:The time formatting rules for Go language are very strict, and must be followed completely.2006-01-02 15:04:05The corresponding part to construct your format string. For example, if you want to display a two-digit year, you must use06instead of2006.
  • Timestamp type: stampToDateThe label expects a Unix timestamp, which is usually a 10-digit integer (seconds). If your time data is in a different format (such asYYYY-MM-DD HH:mm:ssString), it may need to be converted first before use.
  • withdateFilter differentiation:There is also one in the AnQiCMS template.dateA filter, but it requires the value to be in Go languagetime.Timean object type, not a timestamp. When handling timestamps, be sure to usestampToDate.

BystampToDateUnderstanding the time formatting rules of Go language, you can easily implement various time display needs in the AnQiCMS template, adding a professional and readable touch to your website content.


Frequently Asked Questions (FAQ)

Q1: Why do I useYYYY-MM-DD HH:mmsuch format strings not work?

A1: AnQiCMS'stampToDateThe label uses the Go language time formatting rules, it does not useY/M/D/H/mplaceholder characters, but through a fixed reference time2006-01-02 15:04:05To build the format. You need to replace the corresponding number in this reference time with the format you want to display. For example, to display the year, use2006; to display the month, use01and so on.

Q2:stampToDateDoes the label support a 13-digit timestamp (milliseconds)?

A2:stampToDateThe label expects to receive a 10-digit Unix timestamp (in seconds). If your timestamp is a 13-digit millisecond timestamp, you need to divide it by 1000 to convert it to seconds and then pass it instampToDateLabel. For example:{{ stampToDate(item.CreatedTime / 1000, "2006年01月02日 15:04") }}.

Q3: Besides "year-month-date hour:minute", can I format it in other styles?

A3: Absolutely.If you understand the time formatting rules of the Go language, you can flexibly combine almost any date and time format you want."Mon, 02 Jan 2006 15:04:05 -0700"This reference time, or directly refer to the Go language official document regardingtimepackage formatting example.

Related articles

How to display the user nickname, comment content, and posting time in the AnQiCMS comment list?

In AnQiCMS, the comment feature is an important part to enhance the interaction of the website.How to clearly and beautifully display the valuable comments left by users on the website, which is a focus for website operators.AnQiCMS provides flexible template tags, allowing us to easily customize the display of the comment list, including user nicknames, comment content, and publishing time, etc.To implement the display of comment lists, we mainly use the AnQiCMS template tag system.This usually involves editing the specific parts of the website template file. ### 1.

2025-11-09

How does the AnQiCMS template display or hide specific content based on user group permissions?

In website operation, displaying or hiding specific content based on user identity is a common strategy, which can help us meet various business needs such as member exclusive, paid content, and internal information distribution.AnQiCMS provides flexible user group management features, combined with its powerful template engine, it can easily implement content control based on user group permissions. ### Learn about AnQiCMS user group mechanism AnQiCMS is built with a complete user group management and VIP system.In the background, we can create different user groups

2025-11-09

How to use the `pagination` tag of AnQiCMS to implement pagination navigation on the article list page?

In website content operation, the pagination navigation of the article list page is a key link to improve user experience and optimize search engine crawling efficiency.AnQiCMS as a feature-rich enterprise-level content management system, provides us with a simple and efficient `pagination` tag, making it easy to implement this feature.Next, we will discuss in detail how to use the `pagination` tag to implement page navigation on the article list page of AnQiCMS.

2025-11-09

How to process thumbnails and display images of different sizes in AnQiCMS templates?

Managing website content in AnQiCMS, image processing is undoubtedly a key factor in improving user experience and page loading speed.A website that can intelligently present thumbnails of images in appropriate sizes for different display scenarios can not only greatly improve the page response speed but also make the visual layout more coordinated and beautiful.The AnQi CMS provides a powerful and flexible image thumbnail processing mechanism, which can be easily implemented, whether through unified settings in the background or on-demand calls in the frontend template.### Back-end Content Settings

2025-11-09

How to display hreflang tags for users of different languages on the AnQiCMS website?

In the operation of multilingual websites, it is crucial to ensure that search engines can accurately understand the language and region targeted by your content, which is essential for improving user experience and search engine optimization (SEO).The `hreflang` tag is the key tool to solve this problem.It tells search engines that your site has content variants for different languages or regions, thus avoiding duplicate content issues and helping search engines to display the most relevant pages to users of different languages.

2025-11-09

How to display the introduction and Banner image of the current category in the AnQiCMS template?

How to make your category page on the website more attractive while clearly conveying information is the key to improving user experience and SEO effectiveness.AnQiCMS (AnQiCMS) with its flexible template system allows you to easily display the introduction of the current category and personalized Banner images on the category page. We will explore together how to implement this feature in the AnQiCMS template, making your website category page more vivid and professional.### 1. Deeply understand the AnQiCMS template system AnQiCMS

2025-11-09

How to dynamically generate page Title, Keywords and Description using `tdk` tag in AnQiCMS template?

In today's internet environment where content is exploding, Search Engine Optimization (SEO) is crucial for a website's visibility.And the Title (title), Keywords (keywords), and Description (description) on the website page, which we commonly call TDK, are key factors for search engines to understand page content, decide whether to display it to users, and whether users will click.In AnQiCMS, a content management system designed specifically for small and medium-sized enterprises and content operation teams, dynamically generating these TDK information is to enhance the website

2025-11-09

How to implement multi-condition filtering display on the article list page with the `archiveFilters` tag of AnQiCMS?

It is crucial to provide users with a convenient and accurate content search experience in website operation.As the content of the website becomes richer, simple classification or keyword search is often not enough to meet the personalized needs of users.At this time, the multi-condition filtering function on the article list page is particularly important.For AnQiCMS users,巧妙利用 `archiveFilters` tag can easily achieve this goal, making your article list page instantly possess powerful filtering capabilities.

2025-11-09