How to format timestamp output in AnQiCMS template?

Calendar 👁️ 53

As a professional familiar with AnQi CMS and proficient in content operation, I am well aware that the accuracy and beauty of time are crucial in the display of website content.A clear and readable timestamp, which not only improves user experience, but also reflects the standardization of data display.Below, I will give you a detailed introduction on how to format timestamps in the AnQiCMS template.

Detailed guide on timestamp formatting output in AnQiCMS template

AnQiCMS as an efficient and customizable content management system, its template engine supports syntax similar to Django, providing great flexibility for content creators and developers.When building dynamic pages, it is often necessary to display information such as the time of article publication, update time, and user login time.These times are usually stored in databases in the form of timestamps (Unix epoch seconds).To display this time information in a user-friendly format, AnQiCMS provides special template tags for formatting processing.

Core formatting tags:stampToDate

The core tag for formatting timestamp output in the AnQiCMS template system isstampToDate. This tag allows you to convert a 10-digit timestamp (usually called Unix timestamp or Unix epoch seconds) to a string in the date and time format you specify.

stampToDateThe basic usage of the tag is as follows:

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

The "timestamp" here is the 10-digit number you need to format, and the "format" is the string that defines the output date and time style.

Understand the time formatting rules of Go language

The backend of AnQiCMS is developed based on Go language, thereforestampToDateThe "format" parameter in the tag strictly follows the Go language'stime.FormatThe special time formatting rule used by the function. Unlike many programming languages that useYYYY-MM-DDsuch placeholder, Go's formatting is based on a fixed reference time point:2006年1月2日15点04分05秒.

You need to replace each component of this reference time point with the corresponding format you want. The following are some commonly used reference time components and their meanings:

  • year:2006(full year, such as 2023) or06(two-digit year, such as 23)
  • month:01(month with leading zero, such as 08) or1(month without leading zero, such as 8) orJan(Month abbreviation, such as Aug) orJanuary(Full month name, such as August)
  • Day:02(With leading zero, such as 09) or2(Without leading zero, such as 9)
  • hour:15(24-hour format, with leading zero, e.g. 14) or3(12-hour format, without leading zero, e.g. 2) or03(12-hour format, with leading zero, e.g. 02)
  • minute:04(with leading zero for minutes, such as 07)
  • seconds:05(with leading zero for seconds, such as 01)
  • AM/PM:PMorpm(used for 12-hour format)
  • Week:Mon(Abbreviation of the week, such as Fri) orMonday(Full name of the week, such as Friday)
  • Time Zone:MST(Abbreviation of the time zone, such as CST) or-0700(Digital time zone offset, such as +0800)

Actual formatting example

Combine the formatting rules of the Go language, we can construct various common date and time output formats. Suppose we have a timestamp variablepublishStampIts value is1609470335(Corresponding to 2021-01-01 00:25:35 UTC+8), here are some formatting examples:

  • Displayed as "2021-01-01":
    
    <div>发布日期:{{ stampToDate(publishStamp, "2006年01月02日") }}</div>
    
  • Show as “2021-01-01”:“
    
    <div>发布日期:{{ stampToDate(publishStamp, "2006-01-02") }}</div>
    
  • Show as “01/01/2021”:“
    
    <div>发布日期:{{ stampToDate(publishStamp, "01/02/2006") }}</div>
    
  • Show as “2021-01-01 00:25”:“
    
    <div>发布时间:{{ stampToDate(publishStamp, "2006-01-02 15:04") }}</div>
    
  • Shown as “2021-01-01 00:25:35”:
    
    <div>发布时间:{{ stampToDate(publishStamp, "2006-01-02 15:04:05") }}</div>
    
  • Shown as “Friday, January 1, 2021”:
    
    <div>发布时间:{{ stampToDate(publishStamp, "Monday, 2006年1月2日") }}</div>
    
  • Show only the month and date "01-01":
    
    <div>发布月日:{{ stampToDate(publishStamp, "01-02") }}</div>
    
  • Show only the hour and minute "00:25":
    
    <div>发布时分:{{ stampToDate(publishStamp, "15:04") }}</div>
    

Apply formatting in AnQiCMS tags of various types

In the AnQiCMS template, timestamps are usually used as properties of document, comment, user and other data objects. You can directly use these properties asstampToDatethe first parameter of the tag.

In document lists or details

When displaying article or product lists, we often need to show their creation or update time.archiveListandarchiveDetailThe object returned by the tag containsCreatedTimeandUpdatedTimeProperties, which are timestamps.

  • InarchiveListIn:
    
    {% archiveList archives with type="list" limit="10" %}
        {% for item in archives %}
            <div>
                文章标题:{{ item.Title }}
                发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}
                更新时间:{{ stampToDate(item.UpdatedTime, "2006-01-02 15:04") }}
            </div>
        {% endfor %}
    {% endarchiveList %}
    
  • InarchiveDetailIn:
    
    {% archiveDetail currentArchive with name="Title" %} {# 获取当前文档标题,作为示例 #}
    <div>
        当前文档标题:{{ currentArchive }}
        创建时间:{{ stampToDate(archive.CreatedTime, "2006年1月2日 15:04:05") }}
        修改时间:{{ stampToDate(archive.UpdatedTime, "最近更新于 2006-01-02") }}
    </div>
    
    (Please note,archive.CreatedTimeand so on,archiveDetailThe context can be used directly, or as shown above, through a named variable

In the comment list

commentListThe comment object returned by the tag containsCreatedTimeProperty.

{% commentList comments with archiveId=archive.Id type="list" limit="5" %}
    {% for item in comments %}
        <div>
            评论者:{{ item.UserName }}
            评论内容:{{ item.Content }}
            评论时间:{{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}
        </div>
    {% endfor %}
{% endcommentList %}

In the user details

userDetailThe user object returned by the tag containsLastLoginandExpireTimeWaiting for timestamp attribute.

{% userDetail userInfo with name="UserName" id="1" %} {# 示例:获取ID为1的用户信息 #}
<div>
    用户名:{{ userInfo }}
    上次登录:{{ stampToDate(user.LastLogin, "2006-01-02 15:04:05") }}
    VIP过期:{{ stampToDate(user.ExpireTime, "2006年1月2日") }}
</div>

(Please note,user.LastLoginand so on,userDetailThe context can be used directly, or as shown above, through a named variable

Summary

BystampToDateLabel and understand the unique time formatting rules of the Go language, you can flexibly convert timestamps to any date and time format you need in the AnQiCMS template.This not only helps to enhance the professionalism and user experience of the website, but also ensures the accuracy and consistency of time information display.In practice, it is recommended that you try different format combinations to find the one that best suits your website design style and user reading habits.


Frequently Asked Questions (FAQ)

Question: Why does the time formatting parameter of AnQiCMS look strange and not common?Y-m-d H:i:s?

Answer: This is because the AnQiCMS backend is developed using Go language, and Go language adopts its unique "reference time" mode in time formatting. It does not use placeholders, but uses a fixed reference time2006年1月2日15点04分05秒Use as a template, you need to map the format you want to the corresponding position at this reference time point. For example, if you want to display a four-digit year, you write2006; If you want to display a two-digit month, you write01and so on.

Question: If my timestamp is not 10 digits but 13 digits (millisecond level),stampToDateCan the tag still be used?

Answer:stampToDateThe label defaults to receiving a 10-digit Unix epoch second timestamp. If your timestamp is a 13-digit millisecond timestamp, you will need to pass it tostampToDateBefore dividing it by 1000, convert it to a second-level timestamp. For example:{{ stampToDate(item.MillisecondTime / 1000, "2006-01-02") }}Make sure the division result is an integer.

Question: Can I directly get the current time and format it in the template?

Answer: Yes, AnQiCMS providesnowtags to get the current time and format it. Its usage is similar tostampToDatebut does not require passing a timestamp parameter. For example:{% now "2006-01-02 15:04:05" %}It will output the current date and time. If you only need the year, you can use{% now "2006" %}.

Related articles

How to perform conditional judgments (if/elif/else) in Anqi CMS template?

As an experienced CMS website operation personnel, I am well aware that the flexibility of templates is crucial for efficient operation in daily content management.Especially conditional judgment allows us to intelligently control the display and hiding of content based on different data states and business logic, thereby providing users with a more accurate and personalized browsing experience.The Anqi CMS template engine has adopted the syntax style of Django, making the implementation of conditional judgments both powerful and easy to understand.### The basic structure of conditional judgment in AnQi CMS template Conditional judgment in the AnQi CMS template is mainly achieved through

2025-11-06

How to use the for loop in AnQiCMS template to traverse data and handle empty results with the empty tag?

## Master the data traversal and empty result handling in AnQiCMS templates: in-depth analysis of for loops and empty tags AnQiCMS, with its efficient and customizable features, has become the preferred content management system for many content operation teams and small and medium-sized enterprises.It is crucial for the flexibility of template language when building dynamic web pages.

2025-11-06

How to display the list of related documents in AnQiCMS template?

As a website operator who has been deeply involved in the CMS of security enterprises for many years, I know that effectively guiding users to discover more interesting content is crucial for enhancing user stickiness and the depth of website visits.Among them, the 'related document list' is undoubtedly one of the core functions to achieve this goal.It not only helps users conveniently explore more related topics, but also has a positive impact on the internal link structure of the website and SEO optimization.

2025-11-06

How to call the previous and next document links in AnQiCMS template?

As a senior CMS website operation personnel in an enterprise, I am well aware of the importance of content navigation for user experience and SEO.On a day when the content of the website is becoming richer and richer, how to guide users to smoothly browse related content is a key factor in enhancing user stickiness and reducing the bounce rate.Today, we will delve into how to cleverly call the links of the previous and next documents in the AnQiCMS template to optimize your website's content structure and user path.### Enhancing User Experience and SEO: Navigation of Previous and Next Documents in AnQiCMS Templates In the Content Management System

2025-11-06

What are the common functions of filters in AnQiCMS templates (such as truncation, replacement)?

In AnQi CMS template design, filters play a vital role.As an experienced website operator, I know that skillfully using these tools can greatly enhance the quality of content presentation and user experience.The Anqicms template engine has adopted Django's syntax, allowing content creators and template developers to conveniently process and format output data, achieving rich display effects without delving into the backend code.

2025-11-06

How to implement string concatenation and array splitting in AnQiCMS templates?

AnQiCMS is an efficient and flexible content management system, whose powerful template engine provides a solid foundation for the dynamic display of website content.In daily content operation and template development, we often need to concatenate strings or split a string into multiple parts for processing according to specific rules.For example, building dynamic links, combining display information, or displaying the comma-separated tag list from the background data on the front end separately.Master the ability to concatenate strings and split arrays in AnQiCMS templates, which can greatly enhance the flexibility and expressiveness of the template

2025-11-06

How does AnQiCMS Markdown editor support math formulas and flowchart display?

As an experienced website operator proficient in the AnQi CMS content management system and with a keen perception of user needs, I am delighted to elaborate on how the AnQi CMS Markdown editor supports the display of mathematical formulas and flowcharts.Anqi CMS is committed to providing users with an efficient and flexible content management experience, and understands the importance of precise expression of mathematical formulas and clear presentation of flowcharts in specific fields (such as academic, technical blogs, or project management).

2025-11-06

How to deploy AnQiCMS using Docker, for example with 1Panel or aaPanel?

As an experienced website operator familiar with Anq CMS, I fully understand the importance of efficient deployment for enterprise content management.AnQiCMS with its efficiency in Go language, flexible customization capabilities, and easy scalability, has become an ideal choice for small and medium-sized enterprises and content operation teams.By combining Docker technology, the deployment experience of AnQiCMS has been significantly improved, especially with the assistance of panels like 1Panel or aaPanel, making the entire process unprecedentedly convenient and efficient.##

2025-11-06