How does the AnQiCMS template format timestamps and display them in a specified date and time format?

Calendar 👁️ 67

Easily format timestamps in AnQiCMS templates: Custom date and time display guide

In the operation of the website, the date information such as content release time, update time, and comment time is an important part of the information we convey to visitors.Most of the time, the time data obtained from the database is usually a timestamp (a string of numbers), which is not intuitive for users.How to convert these original timestamps into a readable date and time format, and flexibly adjust the display style according to the page design, is a common requirement in template development.

AnQiCMS provides very practical and flexible tools for templates to solve this problem, the most core of which isstampToDate.

Get to knowstampToDateTag: The 'magic wand' of timestamps

stampToDateThe tag is an AnQiCMS template engine built-in feature, specially designed to convert numeric timestamps into common date and time strings. Its usage is simple and straightforward:

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

The key is in two parts:

  1. Timestamp:This is typically a 10-digit number representing the number of seconds elapsed since the Unix epoch (00:00:00 UTC on January 1, 1970). For example, in AnQiCMS documentation, comments, user data, etc.item.CreatedTime(Creation time),item.UpdatedTime(Update time) etc. fields, all return this timestamp.

  2. Format:This is the key to how date and time are displayed. The template engine of AnQiCMS is based on Go language, and its time formatting method is very unique.It does not use the common ones such asY-m-d H:i:sInstead of such placeholders, use oneFixed reference time 2006-01-02 15:04:05As a formatting template.

    Understanding this reference time is crucial:

    • Year:using2006Indicates
    • Month:using01(Represents January) Indicates
    • Date:using02(Representing two) indicates
    • Hour:using15(24-hour system, representing 3 PM) indicates
    • Minute:using04(Representing the fourth) indicates
    • Seconds:using05(Representing five seconds) indicates
    • AM/PM:usingPMorAMIndicates (for example)3:04PM)
    • Weekday:usingMon(Monday abbreviation) orMonday(Monday full name) indicates

    When you want a certain date-time format, just spell out the desired format using the corresponding numbers or letters in the reference time.

Use case: Timestamp in AnQiCMS data

In the AnQiCMS template, you will often encounter situations where you need to format timestamps. Below, we will illustrate through several common examples:

1. The creation/update time in the document list or detail page

Whether it is an article, product, or other custom content model, their creation and update time are stored in timestamp form.

Example code:

{# 假设这是在一个文档列表的循环中,或者文档详情页直接获取 archive 对象 #}
<div>
    <span>发布日期:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</span>
    <span>更新时间:{{stampToDate(item.UpdatedTime, "2006-01-02 15:04")}}</span>
</div>

{# 如果是在文档详情页直接调用当前文档的时间 #}
<div>
    <p>文章发布于:{{stampToDate(archive.CreatedTime, "2006/01/02")}}</p>
    <p>最后更新于:{{stampToDate(archive.UpdatedTime, "01-02 15:04:05")}}</p>
</div>

2. Comment time in the comment list

The time when the user comments needs to be clearly displayed.

Example code:

{# 假设这是在一个评论列表的循环中 #}
{% for comment in comments %}
    <div>
        <p>{{comment.UserName}} 于 {{stampToDate(comment.CreatedTime, "2006年1月2日 15:04")}} 评论:</p>
        <p>{{comment.Content}}</p>
    </div>
{% endfor %}

3. Login and expiration time in user information

When displaying user personal information, such as the last login time or VIP membership expiration time, formatting is also important.

Example code:

{# 假设这是在一个用户详情页 #}
{% userDetail userInfo with name="LastLogin" id="1" %}
{% userDetail vipExpire with name="ExpireTime" id="1" %}
<div>
    <p>上次登录:{{stampToDate(userInfo, "2006-01-02 15:04")}}</p>
    <p>VIP 到期:{{stampToDate(vipExpire, "2006年01月02日")}}</p>
</div>

Flexible and diverse formatting examples.

Mastered the reference time formatting rules of the Go language, you can create various date and time display effects as you wish.

The format you want to display stampToDateformat string Sample output (assuming the timestamp is1609470335that is, January 1, 2021, 11:05:35 AM
Year-Month-Date "2006-01-02" 2021-01-01
Year-Month-Day (in Chinese) "2006年01月02日" 2021年01月01日
Month/Day/Year "01/02/2006" 01/01/2021
Time:Minutes (24-hour format) "15:04" 11:05
Time:Hour:Minute:Second (24-hour format) "15:04:05" 11:05:35
Time:Hour:Minute AM/PM (12-hour format) "3:04PM" 11:05AM
Year-Month-Date Hour:Minute "2006-01-02 15:04" 2021-01-01 11:05
Week, Day Month Year "Mon, 02 Jan 2006" Fri, 01 Jan 2021
Complete Date and Time (with timezone) "2006-01-02T15:04:05Z07:00" 2021-01-01T11:05:35Z+08:00

Tip:

  • Go language reference time memorization method:There is a simple mnemonic to help remember the reference time of the Go language: 'On January 2, at 3:04:05, 2006'. It corresponds to01(month),02(day),03(PM hour, also known as)15of a 24-hour format),04(minutes),05(seconds),2006(years).
  • stampToDatewithdateThe difference between filters:There is also one in the AnQiCMS template.dateFilter ({{obj|date:"格式"}}Please note that.dateThe filter is only applicable to Go language primitives.time.TimeAn object of type, not a direct numeric timestamp. For the 10-digit timestamps commonly found in the AnQiCMS database, it is imperative to usestampToDateFormat labels.

Summary

stampToDateThe tag is the core tool for handling date and time display in the AnQiCMS template.By understanding its formatting mechanism based on the Go language reference time, you can easily convert the original timestamp into various easy-to-read and design-satisfying date and time formats, thereby enhancing the professionalism of the website's user experience and content presentation.Use this tag flexibly to make your website content more vivid and humanized.


Frequently Asked Questions (FAQ)

1. Why do I use it directly in the template{{item.CreatedTime}}but it shows a string of numbers instead of the date format I want?

This is becauseitem.CreatedTimeIn AnQiCMS, a standard 10-digit Unix timestamp is stored, which is a pure number. To display it in the common date and time format we are familiar with, you need to usestampToDateTag

Related articles

How to only display documents of the current category on the category list page and not include documents of subcategories?

When operating a website, we often encounter the need to display content, one common scenario being how to precisely control the displayed content on a category list page.Sometimes, we hope to display documents under a main category page, including all subcategory documents as well, to provide more comprehensive information.However, in some cases, in order to maintain the purity of the page theme and the focus of the content, we may only want to strictly display the documents under the current category without including any content from subcategories.AnQi CMS as a flexible content management system

2025-11-07

How is the 'Multi-site Management' feature of AnQiCMS implemented to display different site content independently?

## Deeply analyze the "Multi-site Management" feature of AnQiCMS: How to ensure independent display of content?How to efficiently manage content when operating multiple websites or owning multiple brand sub-sites, while ensuring the independence of content on each site, is a challenge faced by many operators.The "multi-site management" feature provided by AnQiCMS is designed to solve this pain point.It not only allows you to manage all sites in a single background, but also fundamentally ensures that the content of each site is displayed independently, avoiding confusion and errors.So, AnQiCMS

2025-11-07

How to set the display logic and hierarchy of breadcrumb navigation in AnQiCMS template?

When building a website, breadcrumb navigation not only effectively improves user experience and helps visitors understand their current location, but is also an indispensable part of search engine optimization (SEO), as it clearly shows the hierarchical structure of the website.For websites using AnQiCMS to manage content, flexibly setting the display logic and hierarchy of breadcrumb navigation is an important task in template creation.AnQiCMS provides a powerful and easy-to-use template tag for handling breadcrumb navigation, allowing you to easily implement it in the front-end template without writing complex backend logic

2025-11-07

How to display custom product parameters and specifications on the product detail page?

Add custom parameters and specifications to the product detail page in AnQiCMS, which can make your product information more detailed and professional.Whether it is the technical parameters of electronic products or the size and color options of clothing, you can easily achieve these personalized display requirements through a flexible content model.Next, we will step by step understand how to set, fill in, and finally display these customized parameters and specifications on your product detail page.--- ### Step 1: Define product custom parameters in the background Start adding custom parameters to the product

2025-11-07

How to display the title and link of the previous and next articles on the article detail page?

In AnQi CMS, adding the titles and links of the previous and next articles to the article detail page can not only optimize the user experience on your website, guide them to discover more related content, but also improve the internal link structure of the website to a certain extent.AnQi CMS with its simple and efficient template engine makes the implementation of this feature very intuitive.### Core Feature Revelation: prevArchive and nextArchive Tags The AnQi CMS template system is built-in with two tags specifically designed for handling article navigation: `prevArchive`

2025-11-07

How to implement batch replacement of specific keywords in article content and display it on the front end in AnQiCMS?

In the daily work of content operation, we often encounter the need to make uniform adjustments to a large amount of article content.This may involve updating the brand name, correcting industry terminology, unify the product model, and even adjusting keyword density for SEO purposes.Manually modifying hundreds or even thousands of articles one by one is undoubtedly a huge task and is prone to errors.AnQiCMS provides a very practical feature that can help us efficiently implement batch replacement of specific keywords in article content and ensure that these changes are displayed correctly on the website front-end.Why do you need to replace keywords in articles in bulk

2025-11-07

How to display custom field content of different content models in the frontend template?

The content model feature of AnQiCMS brings great flexibility to website operations, allowing us to define various personalized content structures according to our actual business needs.Whether it is an article, product, event, or any other custom content type, we can add unique fields for it.How can we flexibly display custom fields in the content model on the website front-end template?Let's explore this practical skill together. ### Understanding AnQiCMS's Content Model and Custom Fields Using AnQiCMS

2025-11-07

Does AnQiCMS website support automatically converting uploaded images to WebP format to speed up display speed?

The loading speed of a website is an important factor for user experience and search engine optimization.Among them, images play an important part in the page content, and their loading efficiency has a significant impact on the overall performance of the website.WebP is a modern image format, compared to traditional JPEG, PNG, it can provide similar or even higher image quality with smaller file size, thereby significantly speeding up web page loading speed.AnQiCMS as a content management system that focuses on efficiency and performance, fully considers the optimization needs of the website in image processing.For "AnQiCMS"

2025-11-07