How to format timestamp output to date or time string in AnQiCMS template?

Calendar 👁️ 76

In website operations, date and time information such as content release time and update time is crucial for user experience and content management.AnQiCMS as a rich-featured content management system, provides flexible template tags, allowing users to easily format the timestamp data stored in the background into various easily readable date or time strings to meet different display needs.

Understand the timestamp in AnQiCMS

Most of the date and time fields, such as the article's, inside AnQiCMS,CreatedTime(Creation time) orUpdatedTimeThe update time, stored in the form of Unix timestamp.This is a 10-digit integer representing the number of seconds from 00:00:00 UTC on January 1, 1970 to the present.Although this format is convenient for system storage and processing, it is not very intuitive to display directly to users.Therefore, we need to convert it to a human-readable date or time format.

Core function:stampToDateTag

AnQiCMS provides a dedicated template tagstampToDateto solve the timestamp formatting problem. The use of this tag is very concise and intuitive, it accepts two parameters:

  1. Timestamp variable: A timestamp that needs to be formatted, typically a field you obtain from the content model, for exampleitem.CreatedTime.
  2. Format string: A string used to define the output format.

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

The mystery of format strings: Reference time in Go language

Pay attention to the format string writing.AnQiCMS is developed based on the Go language, therefore it follows the conventions of Go language in date and time formatting.Y-m-d H:i:sSuch placeholders are replaced with a special 'reference time' to define the desired output style. This reference time is:2006-01-02 15:04:05.

You can imagine this reference time as a template, you can write down how you want the date and time to be displayed using this reference time. For example:

  • If you want to display the year (2006),will format the string within2006reserve.
  • If you want to display the month(01),will format the string within01reserve.
  • etc.,02represents the day,15represents the hour (24-hour format),04represents the minute,05represents seconds.

Here are some common formatting examples:

  • Show only the date (Year-Month-Day)If you wish the date to be2023-01-01The format string is written as"2006-01-02".
  • Display the full date and timeIf needed2023-01-01 12:30:00Such an output, the format string is"2006-01-02 15:04:05".
  • Custom delimiter: Want2023年01月01日Such Chinese format, can be written as"2006年01月02日"If you want it to be01/01/2023Then it is written as"01/02/2006".
  • Only show the timeFor example12:30:00The format string is"15:04:05".
  • Display short year and monthIf only01-02(Month-Day), it can be written as"01-02".

Actual application in template

In the AnQiCMS template, you can usearchiveList/archiveDetail/commentListThe tag retrieves data items containing timestamps. Then, pass the timestamp field of these data items tostampToDateFormat labels.

For example, on a list page of articles, we want to display the publication date of each article:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <li>
        <a href="{{item.Link}}">
            <h5>{{item.Title}}</h5>
            <div>
                <span>发布时间:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</span>
                <span>阅读量:{{item.Views}}</span>
            </div>
        </a>
    </li>
    {% endfor %}
{% endarchiveList %}

On the article detail page, we may need to display more detailed update time:

{% archiveDetail archiveDetailData %}
    <h1>{{archiveDetailData.Title}}</h1>
    <p>
        <span>发布于:{{stampToDate(archiveDetailData.CreatedTime, "2006-01-02 15:04")}}</span>
        {% if archiveDetailData.CreatedTime != archiveDetailData.UpdatedTime %}
        <span>更新于:{{stampToDate(archiveDetailData.UpdatedTime, "2006-01-02 15:04:05")}}</span>
        {% endif %}
    </p>
    <div>
        {{archiveDetailData.Content|safe}}
    </div>
{% endarchiveDetail %}

Tip:time.TimeType anddateFilter

AnQiCMS template indeed exists adatefilter, but it requires the variable to be in Go languagetime.TimeAn object, not the raw Unix timestamp. If we pass the timestamp directly todateThe filter, the system usually fails or does not work as expected. Therefore, when processing the original Unix timestamp obtained from the database or content model, be sure to usestampToDateLabel, it is specially designed for this kind of scenario.

If you just want to get and format the current server time without handling specific content timestamps, you can conveniently use{% now "格式字符串" %}Label, for example{% now "2006-01-02 15:04" %}Will directly output the current date and time.

BystampToDateLabel, AnQiCMS makes timestamp formatting output simple and powerful, meeting various refined needs for website content display.


Frequently Asked Questions (FAQ)

Related articles

How to flexibly control the display quantity and style of pagination page numbers with the `pagination` tag of AnQiCMS?

When managing a content-rich website, an efficient page navigation system is crucial for enhancing user experience and search engine optimization (SEO).AnQi CMS knows this, its built-in `pagination` tag provides us with detailed control, making the website page number display both flexible and beautiful.This article will guide you to understand how to use the `pagination` tag, flexibly control the display quantity and style of pagination page numbers.

2025-11-08

How to automatically generate the breadcrumb navigation of the page in the AnQiCMS template using the `breadcrumb` tag?

When browsing websites, we often notice a path information string at the top or bottom of the page, which clearly indicates our current location, such as "Home > Category > Article Title."}This is what we commonly refer to as Breadcrumb Navigation.It not only helps users better understand the website structure and improve the browsing experience, but also has a significant value for search engine optimization (SEO).Why is breadcrumb navigation important? For users, breadcrumb navigation is like a mini-map.

2025-11-08

How to use the `archiveFilters` tag in AnQiCMS to achieve combined filtering of document parameters?

## Mastering AnQiCMS: The Art of Using `archiveFilters` Tag for Document Parameter Combination Filtering It is crucial to provide users with accurate and efficient content search experience in content operation.Imagine if your website has a rich variety of content, but users find it difficult to quickly find the information they need. This will undoubtedly greatly affect user experience and the conversion rate of the website.

2025-11-08

How to enable and use the captcha feature of the留言表单in AnQiCMS?

The website guestbook is a good place to interact with visitors, which brings the website closer to the users.However, without proper protection, the message board will soon be overwhelmed by various spam, malicious submissions, and even automated scripts, which not only affects the cleanliness of the website but may also consume server resources and even damage the reputation of the website.Fortunately, AnQiCMS provides a simple and effective solution: the captcha feature.

2025-11-08

How do the `urlize` and `urlizetrunc` filters handle URL links in the AnQiCMS template?

In website operation, we often need to embed various links in the content, whether they point to internal resources or external references.Manually converting plain text links into clickable hyperlinks is not only inefficient but also prone to errors.It is more important that standardized link handling is crucial for search engine optimization (SEO) and user experience.

2025-11-08

How to use the `length` filter to get the length of a string, array, or key-value pair in AnQiCMS templates?

In AnQiCMS template development, we often need to process and judge the data displayed on the page.Among them, getting the length of a string, array, or key-value pair is a very basic and practical operation.The AnQiCMS template engine provides the `length` filter, making this task simple and intuitive. ### Understanding the `length` filter: A tool for getting data length The `length` filter, as the name implies, is used to get the 'length' of the data.

2025-11-08

What search engines does the "Link Push" feature of AnQiCMS backend support for active push?

Today, with the rapid growth of internet content, the quick inclusion of website content is an important factor in increasing website traffic and influence.For many content operators and enterprises, how to make search engines discover and index new content on their websites in a timely manner is a core issue in SEO work.AnQiCMS (AnQi CMS) deeply understands this need, especially in the backend, integrating a convenient and powerful "link push" function, aimed at helping users deliver their carefully crafted content to major search engines in the shortest possible time, effectively shortening the cycle from content publication to indexing.###

2025-11-08

How does AnQiCMS's `filter` tag combine multiple filters to process content in a chain?

AnQiCMS provides flexible and powerful content management capabilities, among which the template engine is the core of our daily operations and content display.When processing website content, we often encounter situations where we need to perform multi-step transformations to achieve the final display effect.For example, a long article summary may need to be truncated first, then ensure all letters are lowercase, and finally calculate the length of the processed text.If each time is handled manually through complex code logic, it is not only inefficient but also makes the template difficult to maintain.

2025-11-08