How to format a timestamp in AnqiCMS template to display a custom date and time format?

Calendar 👁️ 68

Manage website content in AnqiCMS, we often need to display the publishing time, update time, or registration and login time of users, etc.These time information is usually stored in the database in the form of a timestamp, which is a sequence of numbers.If displayed directly on the website front end, these numbers are not intuitive and lack aesthetic appeal.Fortunately, AnqiCMS provides very flexible template tags, allowing us to easily format these timestamps into the date and time display format we want.

AnqiCMS's template system, draws on the syntax style of the Django template engine, powerful and easy to use. It contains a dedicated tag for formatting timestamps -stampToDateBy this tag, we can convert the original timestamp into various custom date and time strings, whether it is displaying the year, month, date, or accurate to the hour, minute, and second, it can be easily realized.

Understand the timestamp and formatting mode of AnqiCMS

Further understandstampToDateBefore the label, we first need to know the source and formatting mechanism of the timestamp in the AnqiCMS template.A timestamp is typically a 10-digit or 13-digit integer (Unix timestamp), representing the number of seconds or milliseconds from 00:00:00 UTC on January 1, 1970 to the current time.AnqiCMS'stampToDateThe label mainly handles 10-digit second-level timestamps.

And the formatting mode is a layout string unique to AnqiCMS, based on the Go language style. Unlike the commonY-m-d H:i:sThis pattern is different, Go language uses a specific reference date-2006-01-02 15:04:05Define the parts of the date and time. This means that you want to display the year, write '2006';Want to display 'month', write '01'; want to display 'day', write '02', and so on.

Here are some commonly used formatting patterns examples, convenient for us to understand and use:

  • Year: 2006
  • Month (two digits): 01
  • Month (abbreviation): Jan
  • Month (full): January
  • Date (two digits): 02
  • Weekday (abbreviation): Mon
  • Weekday (full): Monday
  • Hour (24-hour format): 15
  • Hour (12-hour format): 03or04
  • Minute: 04
  • Seconds: 05
  • AM/PM: PMorpm

You can freely combine these "reference values" to create any date-time format you want.

Core operation: usestampToDateTag

stampToDateThe usage method of tags is very intuitive, its basic syntax is:

{{ stampToDate(时间戳变量, "格式化模式字符串") }}

here,时间戳变量Fields usually obtained from the backend, such as the object of the article,CreatedTime(Creation time) orUpdatedTime(update time), the object of the user,LastLogin(last login time), etc.格式化模式字符串This is the date and time format mentioned above, combined according to the Go language style.

Let's see how to apply it with some practical examples.

Suppose we are looping through a list of articles, each of which has aCreatedTimefield, which is a timestamp.

{% archiveList archives with type="list" limit="5" %}
    {% for item in archives %}
    <div class="article-card">
        <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
        <p class="description">{{item.Description}}</p>
        <p class="meta">
            <!-- 显示为 "2023-10-26" -->
            发布于:<span>{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
            <!-- 显示为 "2023年10月26日" -->
            <span>{{stampToDate(item.CreatedTime, "2006年01月02日")}}</span>
            <!-- 显示为 "10-26" -->
            <span>{{stampToDate(item.CreatedTime, "01-02")}}</span>
            <!-- 显示为 "15:30:45" -->
            <span>{{stampToDate(item.CreatedTime, "15:04:05")}}</span>
            <!-- 显示为 "Oct 26, 2023" -->
            <span>{{stampToDate(item.CreatedTime, "Jan _2, 2006")}}</span>
        </p>
    </div>
    {% endfor %}
{% endarchiveList %}

On the article detail page, we may need to display the detailed publication and update time of the article:

{% archiveDetail archive with name="Id" %} {# 假设这里获取了当前文章详情 #}
<article>
    <h1>{{archive.Title}}</h1>
    <p class="info">
        发布时间:<span>{{stampToDate(archive.CreatedTime, "2006-01-02 15:04:05")}}</span>
        更新时间:<span>{{stampToDate(archive.UpdatedTime, "2006年01月02日 星期一 下午03点04分")}}</span>
    </p>
    <div class="content">
        {{archive.Content|safe}}
    </div>
</article>

Even in the scenario where user information needs to be displayed, such as the user's last login time:

{% userDetail userInfo with name="Id" id="1" %} {# 假设获取了ID为1的用户信息 #}
<div class="user-profile">
    <h2>{{userInfo.UserName}}</h2>
    <p>注册时间:<span>{{stampToDate(userInfo.CreatedTime, "2006年01月02日")}}</span></p>
    <p>上次登录:<span>{{stampToDate(userInfo.LastLogin, "2006/01/02 15:04")}}</span></p>
</div>

By these examples, we can seestampToDateThe power of tags, which makes the display of time information extremely flexible

Practical tips

  • Remember the reference date of the Go language:“2006 January 02 at 15:04:05” is the key to understanding all time formats.Which part do you want, use the corresponding number or text in this reference date to represent it.
  • The source of timestamps:AnqiCMS's document model(archive), classification model(category), user model(user) and others, usually all containCreatedTimeandUpdatedTimeThis timestamp field. It is convenient to get these timestamps when calling the corresponding tags.
  • Ensure it is a timestamp: stampToDateThe tag is used specifically for handling timestamps. If your data field is already a formatted date string (e.g., "2023-10-26"), then it is not necessary to usestampToDateIt can be directly output. If you are unsure whether a field is a timestamp, you can try using{{ item.CreatedTime|dump }}to view its type and value,

Related articles

How to iterate through data using the for loop tag in AnqiCMS templates and display list content on the front end?

In modern website construction, content lists are everywhere, whether it is article lists, product displays, user comments, or navigation menus, they cannot do without efficient organization and presentation of data.AnqiCMS as an enterprise-level content management system based on Go language, provides intuitive and powerful loop tags in template creation, allowing you to easily display various list content on the front page.

2025-11-09

How does the AnqiCMS template use if/else tags for conditional judgments to dynamically display different content?

In website content operation, we often need to flexibly display different content modules or styles based on different conditions to enhance user experience and page interactivity.AnqiCMS (AnqiCMS) provides powerful template tag features, among which the `if/else` conditional judgment tags are the core tools to achieve this goal.By it, we can easily make the website content come to life according to specific rules, no longer monotonous static display.### AnqiCMS template basic conditional judgment: `if`

2025-11-09

How to implement pagination navigation in AnqiCMS templates and customize the display of page numbers and style?

In a content management system, especially when a website carries a large amount of articles, products, or other list information, reasonable pagination navigation is the key to improving user experience and facilitating content browsing.It can not only help visitors quickly find the information they need, but also avoid the slow loading caused by too long single-page content, and it is also very beneficial for search engine optimization (SEO).AnqiCMS provides a powerful and flexible template tag set, allowing you to easily implement pagination functionality on your website and customize its display style and appearance as needed.### Enable pagination feature: Starting from the list tag At

2025-11-09

How to display the AnqiCMS backend management's friend links on the front page and control the Nofollow attribute?

In website operation, friendship links are an important means to enhance website authority, attract traffic, and strengthen industry associations.However, how to effectively manage these links, especially in terms of search engine optimization (SEO), ensuring that they can play a positive role while not causing negative effects on their own website, is a concern for many operators.AnqiCMS as a content management system dedicated to providing efficient and customizable solutions offers very convenient and flexible tools in this regard.

2025-11-09

How to use with or set tags in AnqiCMS templates to define variables for optimizing content display and maintenance?

In AnqiCMS template development, defining and using variables are indispensable skills to make our website content more flexible and code easier to maintain.The system uses a syntax similar to the Django template engine, where `with` and `set` tags are used to define variables, optimizing content display and managing templates effectively.Understanding and using them effectively can make your website operation twice as effective.Why do we need to define variables in the template?

2025-11-09

How to use AnqiCMS template include tag to introduce common code fragments and achieve page structure reuse?

In website content management, we often encounter situations where it is necessary to display the same content blocks, such as headers, footers, sidebars, or navigation menus, on multiple pages.If you copy and paste this code every time, it is not only inefficient, but also when you need to modify it, you have to face the繁琐 of repeated operations in multiple files.If this continues, the maintenance and updates of the website content will become extremely difficult, and inconsistencies in the experience may even occur.

2025-11-09

How to implement template inheritance using the extends tag in AnqiCMS templates to unify the visual style of the website?

In website operation, maintaining a consistent visual style is the key to brand image and user experience.Imagine if every page layout on your website was different, with navigation positions changing back and forth, users would feel confused and even lose interest.Fortunately, AnqiCMS provides a powerful and elegant way to solve this problem - that is through the `extends` tag in the template inheritance mechanism.AnqiCMS has adopted a template engine syntax similar to Django, making template development intuitive and efficient.

2025-11-09

How does AnqiCMS ensure that mathematical formulas and flowcharts can be displayed correctly on the front end after enabling the Markdown editor?

Today, with the increasing diversity of content creation, websites not only carry text and images, but also often need to display complex mathematical formulas or intuitive flowcharts.For those who use AnqiCMS, the good news is that the system has built-in powerful Markdown editor support, and with simple configuration, it can ensure that these professional contents are perfectly presented on the website front-end.Below, let's take a look at how to operate, making your website content richer and more professional.

2025-11-09