How to convert a timestamp to a specified date format and display it in the AnQiCMS template?

Calendar 👁️ 68

In Anqi CMS template design, we often encounter situations where we need to display timestamp data in a human-readable date format.Whether it is the publication time of the article, the update date of the content, or the record of user activities, this data is usually stored in the form of timestamps in the database.It is particularly important to master how to convert timestamps to specified date formats in templates so that website visitors can clearly and intuitively understand this information.

Where do the timestamps in AnQiCMS come from?

In AnQiCMS, you will find that many data fields related to time exist in the form of timestamps, such as article detail tags (archiveDetail) and article list tags (archiveList) in theCreatedTime(creation time) andUpdatedTime(Update time), as well as the comment list label(commentList) in theCreatedTime, as well as the user details label(userDetail) in theLastLogin(Last login time) andExpireTime(VIP expiration time) and so on. These timestamps are usually a 10-digit integer representing the number of seconds elapsed since the Unix epoch (January 1, 1970, 00:00:00 UTC).

Directly displaying these 10-digit numbers means nothing to ordinary users, therefore we need to convert them into easily understandable formats such as "March 15, 2023

Core function:stampToDateTag debut

AnQiCMS has provided a special template tag for this -stampToDateIt can easily convert timestamps to the date format you want.This tag is very intuitive in use, only two parameters are needed: the timestamp you want to convert and a string defining the output format.

Its basic syntax is:{{stampToDate(您的时间戳变量, "您的格式字符串")}}.

Understanding these two parameters is the key to successful conversion.

  1. Your timestamp variableThis is usually the data field you get through other tags (such asarchiveListorarchiveDetail), for exampleitem.CreatedTimeorarchive.UpdatedTime. Please note that it must be a10 digitsUnix timestamp (second level).

  2. Your format string: This is the 'magic incantation' that determines the date output format. AnQiCMS is developed based on the Go language, so the format string here follows the unique date formatting standard of the Go language.This standard uses a specific reference date——2006年01月02日 15时04分05秒 -0700 MST(or simply put)2006-01-02 15:04:05As a formatting template. You need to replace each component of the reference date with the corresponding value you want to display.

    For example:

    • 2006Represents the year (four digits)
    • 01Represents the month (two digits, with leading zeros if necessary)
    • 02Represent the date (two digits, leading zero if necessary)
    • 15Represent the hour (24-hour format, two digits, leading zero if necessary)
    • 04Represent the minutes (two digits, leading zero if necessary)
    • 05Represents seconds (two digits, leading zeros if necessary)

    By combining these 'magic numbers', you can create a variety of date formats.

How to usestampToDateTag

Let's understand how to use it in AnQiCMS template with some specific examples.stampToDateLabel. Suppose we have a timestamp1678886400It corresponds to2023-03-15 00:00:00.

  • Displayed as 'Year-Month-Day':

    <p>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</p>
    {# 输出: 发布日期:2023-03-15 #}
    
  • Displayed as 'YearMonthDayHourMinuteSecond':

    <p>更新时间:{{stampToDate(archive.UpdatedTime, "2006年01月02日 15时04分05秒")}}</p>
    {# 输出: 更新时间:2023年03月15日 00时00分00秒 #}
    
  • Displayed as 'Month/Day Time:Minute':

    <p>登录时间:{{stampToDate(user.LastLogin, "01/02 15:04")}}</p>
    {# 输出: 登录时间:03/15 00:00 #}
    
  • Show as a pure year:

    <p>版权年份:{{stampToDate(someTimestamp, "2006")}}</p>
    {# 输出: 版权年份:2023 #}
    

Common application scenario examples

tostampToDateLabels integrated into actual templates can greatly enhance the professionalism of information presentation.

  1. Display the article publication date on the article list pageWhen you usearchiveListWhen looping through multiple articles, you can display the publication time of each article like this:

    {% archiveList archives with type="page" limit="10" %}
        {% for item in archives %}
            <div>
                <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
                <p>发布于:<span>{{stampToDate(item.CreatedTime, "2006年01月02日")}}</span></p>
                <p>{{item.Description}}</p>
            </div>
        {% endfor %}
    {% endarchiveList %}
    
  2. The update time of the content displayed on the article detail pageIn the article detail page, you may need to display the creation time and the latest update time of the article so that readers can understand the timeliness of the content:

    <article>
        <h1>{% archiveDetail with name="Title" %}</h1>
        <p>
            <span>发布日期:{{stampToDate(archive.CreatedTime, "2006-01-02")}}</span>
            <span>最后更新:{{stampToDate(archive.UpdatedTime, "2006-01-02 15:04")}}</span>
        </p>
        <div class="content">
            {%- archiveDetail articleContent with name="Content" %}
            {{articleContent|safe}}
        </div>
    </article>
    

    Here we assumearchiveIs the data object of the current article detail.

  3. Display user-related time information: If your website has a user center, you may need to display the user's VIP expiration time:

    {% userDetail user with name="Id" id="当前用户ID" %}
    <p>您的VIP将于:<span>{{stampToDate(user.ExpireTime, "2006年01月02日")}}</span> 到期。</p>
    {% enduserDetail %}
    

    Of course, when actually in use, you need to obtain according to the actual page contextuser.ExpireTimethis timestamp variable.

A little reminder:datethe filter meetsstampToDateDifference

In the AnQiCMS template system, in addition tostampToDateLabel, you may also see a name calleddatefilter. Here you need to pay special attention todateThe filter is natively supported by the Go language template engine, and it expects to process native Go languagetime.TimeType (a date-time object), not the 10-digit Unix timestamp (integer) we are discussing here.

If you try to pass a timestamp directly todateFilter, it is very likely to report an error during template parsing.stampToDateTag

Related articles

How to ensure that custom parameters set in the AnQiCMS backend can be displayed in the front-end template?

The AnQi CMS endows content management with high flexibility, among which the customizable parameter function is the key to meeting the needs of personalized content display.How to accurately display the unique parameters set for content models such as articles, products, or categories on the website front-end template is a concern for many users.This article will detail this process, helping you fully utilize the powerful customization capabilities of Anqi CMS.

2025-11-07

How to implement keyword batch replacement in AnQiCMS and affect the display of front-end content?

It is crucial to maintain the accuracy, timeliness, and brand consistency of content in website operations.Especially when the amount of website content is large, if you need to make global changes to specific keywords or phrases, manual operation will be a huge project that is time-consuming and labor-intensive.AnQiCMS (AnQi CMS) exactly discerned this need, providing a powerful feature named 'Full Website Content Replacement' to help users efficiently manage and update website content, thereby affecting the display effect of front-end content.Why do we need to batch replace keywords? With the development of the enterprise, product updates, or market strategy adjustments

2025-11-07

How to display the list of internal titles (H1, H2, etc.) of the article content on the article detail page?

In Anqi CMS, in order to enhance the reading experience of long articles and the page SEO effect, we usually hope to display a list of internal titles (H1, H2, etc.) on the side or above the article content on the article detail page, which is what we usually call the article catalog or navigation.This makes it convenient for readers to quickly jump to the chapters of interest, and also helps search engines better understand the structure of the article. The AnQi CMS provides a very convenient way to implement this feature, the core lies in its powerful template tags and content field extraction capabilities.###

2025-11-07

How to display the current model name or URL alias in AnQiCMS?

In website content management, we often need to make the various parts of the website adapt and display information intelligently, one common requirement being how to dynamically display the name or URL alias of the current content model on the front-end page.This is crucial for improving user experience, optimizing search engine performance, and flexible management of website structure.AnQiCMS as an enterprise-level content management system provides a very flexible and powerful template tag function, making it intuitive and efficient to achieve this goal.

2025-11-07

How to display the category thumbnail and name in the article list?

## SecureCMS: Display category thumbnails and names elegantly in article lists In website operation, the article list page is an important entry point for users to browse content and discover information.A beautifully designed, informative list of articles can not only enhance user experience but also effectively guide users to delve deeper into reading.The traditional article list may only display titles, publication time, summaries, and other information, making it somewhat monotonous.

2025-11-07

How to automatically parse the URL string in the text of AnQiCMS to make it clickable?

In daily content operation, we often need to embed various external or internal links in text content such as articles, product descriptions, and even category summaries.However, manually converting each plain text URL string into a clickable HTML link is not only time-consuming and labor-intensive, but also prone to link errors due to negligence, affecting user experience and search engine optimization.

2025-11-07

How to display the number of views and comments on articles in AnQiCMS templates?

In website operation, the number of page views and comments on articles is an important indicator of the popularity of content and user engagement.AnQiCMS provides flexible template tag functions, allowing you to easily display these key data on the website front end, thereby better attracting visitors and enhancing content value.

2025-11-07

How to customize the display of image carousel (Banner) through template tags in AnQiCMS?

AnQiCMS as an efficient and customizable enterprise-level content management system has a significant advantage in the flexibility of content display.For the common picture carousel (Banner) feature on the website, AnQiCMS provides various ways to customize the display through template tags, allowing you to easily achieve diverse Banner display effects according to different page and business needs.

2025-11-07