How to format a timestamp into a readable date and time in the AnQiCMS template?

Calendar 👁️ 60

As an experienced CMS website operation personnel of an enterprise, I know that every detail of user experience is crucial in content management.The clear presentation of dates and times often directly affects readers' perception of the timeliness and professionalism of the content.Today, I will give a detailed explanation of how to convert those seemingly mysterious timestamps in AnQiCMS templates into the readable date and time format that we are accustomed to.

Understand the timestamp formatting mechanism of AnQiCMS

In AnQiCMS, the database usually stores Unix timestamps, which are represented as integers indicating the number of seconds elapsed since 00:00:00 UTC (Coordinated Universal Time) on January 1, 1970.Although this format is convenient for system processing and storage, it lacks intuitiveness for users.stampToDateUsed to format timestamps into familiar dates and times in front-end templates.

stampToDateThe core usage of tags

stampToDateThe usage of tags is very direct, and its basic syntax structure is as follows:

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

Here时间戳Generally, fields are obtained from AnQiCMS data objects (such as articles, products, etc.), for example:item.CreatedTimeorarchive.UpdatedTime.格式The parameter is used to define the style in which you want the date and time to be displayed.

It should be noted that,格式The parameter follows the specific time formatting rules of the Go language. Go language does not use it like PHP or PythonY-m-d H:i:ssuch symbols, but rather adopted a fixed reference time2006-01-02 15:04:05Define the format. This means that you just need to replace each part of this reference time string with the time component you want to display.

Common timestamp field acquisition

In the AnQiCMS template, articles (archive) or list items (item) usually contain the following timestamp fields, which are all 10-digit Unix timestamps:

  • CreatedTime: Indicates the creation time of the content.
  • UpdatedTime: Indicates the last update time of the content.

For example, on a document detail page, you might directly go througharchive.CreatedTimeGet the creation timestamp; while in a document list loop, you will go throughitem.CreatedTimeto get the creation timestamp of each list item.

Go language time formatting example

The reference time to master the Go language is to use it correctlystampToDateThe key. Here are some common formatting examples that can help you quickly understand and apply:

  • Only display the date (year-month-date):{{ stampToDate(publishStamp, "2006-01-02") }}
    • Example Output:2021-06-30
  • Show only the date (Year/Month/Day):{{ stampToDate(publishStamp, "2006/01/02") }}
    • Example Output:2021/06/30
  • Show the full date and time (Year-Month-Day Hour:Minute:Second):{{ stampToDate(publishStamp, "2006-01-02 15:04:05") }}
    • Example Output:2021-06-30 12:00:00
  • Show Chinese date:{{ stampToDate(publishStamp, "2006年01月02日") }}
    • Example Output:2021年06月30日
  • Show hour and minute:{{ stampToDate(publishStamp, "15:04") }}
    • Example Output:12:00
  • Show the day of the week and the date:{{ stampToDate(publishStamp, "Mon Jan _2 2006") }}
    • Example Output:Wed Jun 30 2021(Note in Go language_2Used for placeholder single-digit day characters, if you expect two-digit display, you should use02)

Actual application scenarios and code examples

In the AnQiCMS template, you can use timestamps anywhere you need to display themstampToDate.

Display the publication time of articles on a document list page:

Suppose you are usingarchiveListTag loops to display the article list and hopes that each article shows its publication date.

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

Show the article update time on the document detail page:

On the detail page of a single document, you may want to display the latest update time of the article.

<article>
    <h1>{{ archive.Title }}</h1>
    <p>作者:{{ archive.Author }}</p>
    <p>更新时间:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05") }}</p>
    <div class="article-content">
        {{ archive.Content|safe }}
    </div>
</article>

Display the copyright year of the website in the footer (using the current year):

Although it is not a timestamp, butnowThe label is also related to time, it can obtain the current time and format it, often used to display the dynamic year in the footer.

<footer>
    <p>版权所有 &copy; {% now "2006" %} {{ system.SiteName }}。保留所有权利。</p>
</footer>

Summary

stampToDateThe tag is an indispensable tool for handling timestamps in the AnQiCMS template.By using it, you can flexibly convert the Unix timestamp passed from the backend into a user-friendly date and time format, thereby greatly enhancing the professionalism and user experience of the website content.Remember the reference time format of Go language and adjust it according to your actual needs to easily handle the time display on the website.

Frequently Asked Questions

Q1: Why does my timestamp formatting show incorrect or directly show the year 1970?

The most common reason is provided时间戳An invalid 10-digit Unix timestamp was entered or provided格式The string does not match the time formatting rules of the Go language. Please ensure that the timestamp obtained from the database is a numeric type and the length is correct (usually 10 digits), and carefully check that the format string you are using matches the Go language exactly.2006-01-02 15:04:05Refer to the time rule. For example, if you want to display “month”, you should use01instead ofMM.

Q2: Can I usearchive.CreatedTimeoritem.CreatedTimein the template withoutstampToDateprocessing

directly{{ archive.CreatedTime }}or{{ item.CreatedTime }}The original 10-digit Unix timestamp is usually output.Although it is technically feasible, this will severely affect user experience because most users cannot intuitively understand the dates and times represented by these numbers.stampToDateLabel formatting to provide more readable information.

Q3:stampToDateDoes the label support displaying relative time, such as '5 minutes ago'?

Built-in in AnQiCMSstampToDateThe tag is mainly used to format timestamps into fixed date and time strings.It does not provide the relative time display function such as 'N minutes ago' directly.If you need to implement relative time display, you may need to combine it with a frontend JavaScript library (such as Moment.js or date-fns) to handle it, or look for whether AnQiCMS community provides custom filters or plugins with this feature.

Related articles

How to generate a pagination navigation with page number control in AnQiCMS template?

Hello! As a senior CMS website operator, I fully understand the importance of high-quality content and user experience.Page navigation is an indispensable part of content-based websites, directly affecting the user's browsing experience and the discoverability of content.In AnQiCMS, generating a pagination navigation with page number control is an intuitive and powerful process. Below, I will elaborate on how to implement this feature in the AnQiCMS template.### In AnQiCMS template, generate pagination navigation with page number control In the content management system

2025-11-06

How to display the friend link list of the website in AnQiCMS template?

As an experienced CMS website operation person in the security industry, I fully understand that in website operation, friendship links are not only an embodiment of external resource cooperation, but also an important aspect of SEO optimization and user experience construction.AnQiCMS has always been committed to providing simple and efficient solutions in content management and template integration, and displaying the friend link list is no exception.Now, I will introduce to you how to elegantly display the website's friend link list in the AnQiCMS template.In modern website operation

2025-11-06

How to render and process the message form fields in AnQiCMS template?

As an experienced security CMS website operation personnel, I am very clear about the importance of high-quality content and smooth user experience for attracting and retaining users.The feedback form is an important bridge for website interaction with users, and its correct rendering and processing in the template is directly related to whether users can contact us conveniently and quickly.Below, I will elaborate on how to render and process the comment form fields in the AnQiCMS template.### Location of the comment form template file In AnQiCMS, the comment form usually has a dedicated template file to carry its content

2025-11-06

How to integrate and display comment lists in AnQiCMS templates?

Effectively integrating and displaying the comment list in AnQi CMS is crucial for enhancing website user interaction, activating community atmosphere, and obtaining valuable user feedback.As an experienced CMS website operation personnel of an enterprise, I know that high-quality content cannot be separated from the active participation of users.The Anqi CMS provides powerful and flexible template tags, allowing us to easily implement this feature on the front end of the website.### Overview of Comment Functionality and Template File Organization AnQi CMS has built-in comment management functionality, allowing website administrators to review, reply to, and manage user comments

2025-11-06

How to declare and assign variables in AnQiCMS templates for subsequent use?

As an experienced security CMS website operator, I am well aware of the importance of template design for website content display and user experience.Using variables in templates is the key to customizing pages and improving development efficiency.Today, I will elaborate on how to declare and assign variables in the AnQiCMS template, so that you can build and manage your website content more effectively.

2025-11-06

How to include other template files in AnQiCMS templates (such as public header/footer)?

In the daily operation of AnQiCMS, efficient content management is not only reflected in the background data processing, but also cannot do without flexible front-end template design.For website operators, extracting common parts (such as navigation bars, footers, sidebars, etc.) and managing them independently, and introducing them to each page in a unified way, is the key to improving work efficiency, maintaining website consistency, and simplifying maintenance.AnQiCMS provides a powerful and intuitive template introduction mechanism, making this process effortless.AnQiCMS's template system is based on Go language and has borrowed from

2025-11-06

How to use the `extends` tag in AnQiCMS templates to achieve template inheritance and content rewriting?

In the daily operation of AnQi CMS, we know that high-quality, well-structured website content is the key to attracting and retaining users.This is not only reflected in the words of the article, but also in the overall user experience and maintenance efficiency of the page.AnQi CMS, with its powerful functionality based on the Django template engine syntax, provides us with an efficient template inheritance mechanism, which is exactly what we are going to delve into today - how to use the `extends` tag to achieve template inheritance and content rewriting.

2025-11-06

How to define and use macro functions to create reusable code blocks in AnQiCMS templates?

As an experienced security CMS website operation personnel, I am well aware that the template function of the content management system is crucial for improving website operation efficiency and content display quality.AnQiCMS's powerful Django template engine syntax provides us with flexible content presentation capabilities, and the Macro function feature is an essential tool for code reuse, simplifying template structure, and improving development and maintenance efficiency.During the process of content creation and publishing, we often encounter some repetitive code snippets, such as the display format of article lists and the layout of product cards

2025-11-06