How to format a timestamp and display it as a readable date and time in AnQiCMS?

Calendar 👁️ 77

In website content operations, time information plays a vital role.Whether it is the release time of an article, the listing date of a product, or the submission time of a comment, a clear and easy-to-read date and time format can greatly enhance user experience.AnQi CMS as an efficient content management system, fully considers this point, and provides a flexible way to format and display these timestamp data.

Understanding the timestamp in Anqicms

In the AnQi CMS backend, when we publish articles, products, or perform other content management operations, the system usually records the time of these events as a "timestamp".In simple terms, a timestamp is a series of numbers representing the seconds from a specific moment (usually the Unix epoch, January 1, 1970, 00:00:00 UTC) to the occurrence of the event.Although it is convenient for system storage and calculation, this string of numbers is difficult for visitors to understand.

Therefore, we need to convert these timestamps into a familiar date and time format on the front-end template, such as

Core Tool:stampToDateTag

The AnQi CMS provides a very practical built-in tag in the template, calledstampToDateThis is used to format a 10-digit timestamp into a readable date and time string.Its usage is intuitive and simple, only two parameters are needed: the timestamp you want to format, and the date-time format you expect.

The basic syntax is as follows: {{stampToDate(您的时间戳, "您想要的格式")}}

The 'Your timestamp' here is usually the one you get fromarchiveList/archiveDetail/commentListThe content obtained from the tagsCreatedTimeorUpdatedTimefields.

Master the time formatting rules of Go language

UnderstandingstampToDateThe key is to understand the "Format You Want" parameter.AnQi CMS is developed in Go language, therefore it follows the special time formatting rules of Go language.different from commonY-m-d H:i:sThis format is directly represented, Go language uses a unique reference time to define the date and time format.

This reference time is fixed:2006年01月02日15点04分05秒 -0700 MST.

You do not need to remember the meaning behind all these numbers, just remember this date, and then replace the corresponding numbers or words in the reference time according to the display effect you want, with the format delimiter you want.

For example:

  • If you want to display the year, write2006.
  • If you want to display the month (with leading zero), write01.
  • If you want to display the date (with leading zero), write02.
  • If you want to display the hour in 24-hour format (with a leading zero), write15.
  • If you want to display the minutes (with a leading zero), write04.
  • If you want to display the seconds (with a leading zero), write05.
  • If you want to display the full name of the day of the week, writeMonday; If you only want to display the abbreviation, writeMon.

Some common format examples:

The format you want to display Go language formatting string Sample output
Year-Month-Date 2006-01-02 2023-10-27
Year/Month/Day (Chinese) 2006年01月02日 2023年10月27日
Year/Month/Day Time:Minute 2006/01/02 15:04 2023/10/27 14:30
Month/Day Time:Second 01/02 15:04:05 10/27 14:30:00
Day of the Week, Year Month Day Mon, 02 Jan 2006 Fri, 27 Oct 2023
AM/PM Time:Minute 03:04PMor3:04 PM 02:30PM
Accurate to milliseconds (or more) 2006-01-02 15:04:05.000 2023-10-27 14:30:00.123

Actual application: Formatting time in templates

When you need to display the publication time or update time of an article, it is usually inarchiveListorarchiveDetailthe loop body of the tagstampToDate.

For example, display the publish and update time on the document detail page:

<article>
    <h1>{{archive.Title}}</h1>
    <div>
        发布时间:<span>{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04:05")}}</span>
        {% if archive.CreatedTime != archive.UpdatedTime %}
        <span>更新时间:{{stampToDate(archive.UpdatedTime, "2006年01月02日 15:04:05")}}</span>
        {% endif %}
    </div>
    <div>
        {%- archiveDetail articleContent with name="Content" %}
        {{articleContent|safe}}
    </div>
</article>

Or, display the publish date of each article in the article list:

<div>
{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <li>
        <a href="{{item.Link}}">
            <h5>{{item.Title}}</h5>
            <div>{{item.Description}}</div>
            <div>
                <span>发布于:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
                <span>阅读量:{{item.Views}}</span>
            </div>
        </a>
    </li>
    {% empty %}
    <li>
        当前没有任何内容
    </li>
    {% endfor %}
{% endarchiveList %}
</div>

By flexible applicationstampToDateTags and Go language-specific time formatting rules allow you to easily convert timestamps on websites into user-friendly date and time displays, making your content more vivid and practical.

Other time-related features

exceptstampToDateTags, AnQi CMS also providesnowTags, If you need to display the current server time on the page, you can use it directly, and its formatting rules are the same asstampToDatethe same:{% now "2006年01月02日 15:04:05" %}

Frequently Asked Questions (FAQ)

1. Why does the time formatting string of AnQi CMS look so strange (for example2006-01-02)?This is because Anqi CMS is developed based on Go language, and Go language uses a fixed reference date and time as a template for time formatting. This reference time is2006年01月02日15点04分05秒 -0700 MST. You just need to remember this "template", and then replace the part you want to display with the corresponding number or letter in the reference time. For example,2006represents the year,01represents the month,02Represents a date. This approach, while initially may seem unusual, once you understand its principles, you will find it very flexible and precise.

2. I want to display relative time such as 'today', 'yesterday', or 'a few minutes ago',stampToDateCan it be done? stampToDateTags are mainly used to format timestamps asabsolutedate and time string (for example, "2023 October 27 14:30").It does not have the function to calculate relative time. If you need to display "N minutes ago", "M hours ago" such relative time, you may need to combine with custom Go language functions or front-end JavaScript logic to implement.At present, AnQi CMS template tags mainly focus on absolute time display, and future versions may consider adding more rich relative time display features.

3. Why is the scheduled publication time of my article not displayed on the front page?When editing articles on the Anqi CMS backend, you can set the "publish time".If this time is set to a future moment, then this article will not be displayed on the front page before reaching the set time.This is the AnQi CMS 'Time Factor - Scheduled Release Feature' designed to improve operational flexibility.Please check if the publication time you set has arrived. Once the time arrives, the article will be automatically published and displayed.

Related articles

How to get and display the list of friendship links configured in the AnQiCMS background?

In website operation, friendship links play an indispensable role. They not only bring valuable external traffic to the website but also help improve search engine optimization (SEO) effects, enhance the authority and credibility of the website.For users using AnQiCMS, managing and displaying friend links is a simple and efficient process.This article will introduce in detail how to configure friend links in the AnQiCMS background, as well as how to elegantly present them in your website front-end template.In AnQiCMS admin manage friend links First

2025-11-08

How to generate and display the website's message form in AnQiCMS templates?

In website operation, an efficient and convenient feedback form is an important bridge for user interaction with the website.It not only collects user feedback, but also serves as the entry point for potential customers to obtain information.AnQiCMS as an enterprise-level content management system provides powerful and flexible functions in this aspect, allowing us to easily generate and manage comment forms in templates.

2025-11-08

How to display the comment list of a document in AnQiCMS and implement pagination?

In Anqi CMS, adding a comment list to the document page and implementing pagination is a key step to enhancing user interaction experience.This can not only let visitors participate in discussions and share opinions, but also brings more vitality to the website content.AnQi CMS provides intuitive and powerful template tags, allowing us to easily implement these features. ### Integrate the comment feature into your document detail page Comment lists and comment forms are typically integrated into the detail page of the document.

2025-11-08

How to retrieve and display the list of documents under a specific Tag label in AnQiCMS?

In Anqi CMS, managing website content, the Tag tag is undoubtedly a very flexible and practical tool that helps us classify different categories and even different model content according to themes, greatly enriching the organization of content and the browsing experience of users.So when we want to display a list of all documents under a specific Tag on a dedicated page, Anqi CMS provides a very intuitive and powerful template tag to help us achieve this goal.### Understanding the role of Tag tags in Anqi CMS Before delving into the code

2025-11-08

How to implement conditional judgment in AnQiCMS template to control the display of content?

In AnQi CMS template design, flexibly controlling the display of content is the key to building dynamic, responsive websites.Whether it is to display different information based on page type, data status, or specific conditions, conditional judgment is an indispensable tool.The AnQiCMS template engine provides an intuitive and powerful conditional judgment mechanism, allowing you to easily implement these complex logic.### Core Grammar: The Basics of Conditionals The condition judgment in AnQiCMS templates is similar to many mainstream template engines, using the `{% if ... %}` tag structure

2025-11-08

How to use a for loop to traverse data and display it in the AnQiCMS template?

AnQiCMS with its flexible and powerful template system makes content display efficient and expressive.For website operators and developers, mastering how to traverse and display data in templates is a key step to unlocking their powerful features and bringing the website content to life.Today, let's delve into how to use the `for` loop in AnQiCMS templates to easily present your dynamic data.AnQiCMS's template engine adopts syntax similar to Django, which allows users familiar with other mainstream template languages to quickly get started.

2025-11-08

How to use AnQiCMS filters to truncate or convert text to uppercase and lowercase?

AnQiCMS provides powerful flexibility in content display, its template engine is built-in with a rich set of filters, helping users to easily process text without modifying the original content, including truncation and case conversion.These filters make the presentation of front-end content more accurate and beautiful, meeting the display needs of different scenarios.In AnQiCMS template syntax, the use of filters is very intuitive.You can apply a filter by adding a pipe symbol (`|`) after the variable name, if the filter requires parameters, then use a colon (`:`) after the filter name

2025-11-08

How to display the mobile URL of the website in AnQiCMS template?

When using AnQiCMS to build and manage websites, many friends may encounter such a need: If the website has an independent mobile version, how can you conveniently obtain and display the URL of this mobile website in the template?This makes it convenient for users to switch between different devices, and it also has a positive impact on search engine optimization (SEO).Don't worry, AnQiCMS provides a very intuitive way to achieve this.Why do we need to display the mobile URL in the template?First, let's talk about why there is such a need

2025-11-08