How to format a timestamp in AnQiCMS template and display it in a specified date format?

Calendar 👁️ 57

In AnQiCMS template, processing timestamps and displaying them in a specific date format is a common requirement in content operation.No matter whether you need to display the publication time of an article or the update date of a product, understanding how to flexibly format time will greatly enhance the presentation of website content.AnQiCMS provides simple and powerful template tags, making this process easy and convenient.

Time processing mechanism in AnQiCMS

The AnQiCMS template engine is designed to be similar to Django template syntax, it uses standard Unix timestamps as the internal storage format for time processing, and provides special tags for formatting in the template layer. This means that the time usually obtained from the database or system internally is typically a 10-digit integer (for example1678886400It represents the number of seconds from 1970-01-01 00:00:00 UTC to the present.

To convert this original timestamp into a date and time that is easy to understand in our daily lives, AnQiCMS provides a namedstampToDateThe tag is at the core of its support for Go language time formatting rules, understanding this is the key to mastering time formatting.

stampToDateUsage method of the tag

stampToDateThe basic syntax of tags is very intuitive:

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

There are two important components:

  1. Timestamp (Timestamp)This is a 10-digit integer representing the time point you wish to format. In the AnQiCMS template, you will usually start fromarchive/itemOr obtain these timestamp fields from other data objects, for examplearchive.CreatedTimeoritem.UpdatedTime.
  2. Format (Format String)This is a string that defines the specific style of timestamp formatting. AnQiCMS follows the special reference time of the Go language.2006-01-02 15:04:05This group of seemingly random numbers and time actually has special meanings in Go language, they correspond to specific time units:
    • 2006Represents the year
    • 01Represents the month
    • 02Represents the date
    • 15representing hours (24-hour system)
    • 04representing minutes
    • 05representing seconds
    • (such as otherPMrepresenting AM/PM,MSTrepresenting time zones, etc.)

You just need to use the date and time format you expect, using2006-01-02 15:04:05this reference time to 'simulate' it. For example, if you want to display年-月-日then the format string is"2006-01-02".

Get the timestamp and format it in the template

In the AnQiCMS template, you can usearchiveDetail/archiveListTags get the creation or update time of articles, products, and other content. These time fields are usually inCreatedTime(Creation Time) orUpdatedTime(Update time) Naming, their values are 10-digit timestamps.

Let's see how to apply it with an example of an article detail page:

Suppose you are on the article detail page (archiveDetail) and you want to display the publication date of the article:

{# 假设当前页面是文章详情页,直接通过 archive.CreatedTime 获取时间戳 #}
<p>发布日期:{{stampToDate(archive.CreatedTime, "2006年01月02日")}}</p>

If you are on an article list page (archiveListIn the loop, the timestamp of each article item can be obtained byitem.CreatedTimeObtain:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <div>
        <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 %}

Practical cases and common scenarios

According to different display requirements, you can flexibly adjust the format string:

  1. Show year, month and date only:

    <p>发布日期:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</p>
    {# 或者更简洁的横杠分隔 #}
    <p>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</p>
    
  2. Show full date and time (to the second):

    <p>更新时间:{{stampToDate(item.UpdatedTime, "2006-01-02 15:04:05")}}</p>
    
  3. Show month and date only:

    <p>日期:{{stampToDate(item.CreatedTime, "01月02日")}}</p>
    
  4. Show hours and minutes only:

    <p>时间:{{stampToDate(item.CreatedTime, "15:04")}}</p>
    
  5. Custom separator or text: You can add any text or symbol to the format string.

    <p>发布于:{{stampToDate(item.CreatedTime, "发布于 2006-01-02")}}</p>
    

What are the precautions

  • Precision of timestamps:stampToDateThe label expects to receive a 10-digit Unix timestamp (second level).If your timestamp is in milliseconds (13 digits), you may need to process it first, such as dividing by 1000 to convert it to a second-level timestamp.
  • Characteristics of time format in Go languagePlease remember the reference time of the Go language2006-01-02 15:04:05All format strings should be built around this date and time, not usingYYYY-MM-DDSuch a general placeholder. For example, to represent a year, you must use2006instead ofYYYY.
  • withdateThe difference between filters: AnQiCMS template also supports onedatefor example, a filter (such as{{ value|date:"2006-01-02" }}), but it is designed to handle Go language intime.TimeType of data, not the original 10-digit timestamp. For fromarchive.CreatedTimePlease make sure to use the original timestamp obtained fromstampToDate.

By mastering the use ofstampToDateThe label and Go language formatting rules allow you to easily present beautiful, clear, and demand-compliant time information on the AnQiCMS website, thereby enhancing user experience and the readability of content.


Frequently Asked Questions (FAQ)

Q1: Why instampToDatemust be used2006-01-02 15:04:05as a format reference instead of how other systems doYYYY-MM-DD?

A1: This is the unique design of the Go language. The Go language does not use abstract placeholders likeYYYY-MM-DDbut uses a specific reference date and time2006-01-02 15:04:05It uses each number and symbol in this date and time to represent the corresponding time unit, such as2006represents the year,01Represents the month).Just use this reference date to "build" the display format you want.

Q2: If my timestamp is not 10 digits (for example, it is a 13-digit millisecond timestamp),stampToDatecan the tags still work?

A2:stampToDateThe tag expects to receive a 10-digit Unix timestamp (second level).If the timestamp you have is 13 digits (millisecond level), using it directly may cause formatting errors or incorrect display.{{ stampToDate(item.CreatedTime / 1000, "2006-01-02") }}This form, depending on whether AnQiCMS supports simple mathematical operations within templates, is converted to a 10-digit second timestamp.

Q3: Can I display the current date and time in the template in addition to the content publishing time?

A3: Yes, AnQiCMS provides{% now %}Labels to display the current date and time. Its usage is withstampToDateThe formatting rules are similar and follow the Go language's reference time format. For example, to display the current year, you can use{% now "2006" %}.

Related articles

How does the AnQiCMS template loop through arrays or objects to display list data?

In the Anqi CMS template system, the core of looping through arrays or objects and displaying list data lies in understanding its Django-like template syntax, especially the use of the `for` loop tag.This mechanism makes the presentation of dynamic content intuitive and efficient, whether it is for article lists, category navigation, or product displays, it can adapt flexibly. ### Core Mechanism: The Use of `for` Loops Data traversal in Anqi CMS templates mainly relies on `{% for ...in ...

2025-11-08

How to implement conditional logic in AnQiCMS to control the display of content?

In website operation, displaying different content based on different conditions is the key to improving user experience and achieving refined operation.AnQiCMS provides powerful and flexible template tags and logical judgment functions, allowing us to easily implement conditional display of content, thereby meeting various business needs.This article will delve into how to make use of the template engine features in AnQiCMS, controlling the display of website content precisely through conditional logic judgments.

2025-11-08

How to set and display the global information of AnQiCMS, such as Logo, filing number, and copyright?

AnQiCMS provides an intuitive and powerful way to manage and display the global information of the website, such as the website logo, filing number, and copyright statement.This information is not only an important part of the website's brand image, but also concerns the legal and compliant operation of the website.The detailed introduction on how to set and utilize these global information will follow. ### Set global information in the background All the global information of the website is concentrated in the "Global Function Settings" of AnQiCMS backend.

2025-11-08

How to display pagination links in AnQiCMS and control the number of page numbers displayed?

In website operation, effectively managing and displaying a large amount of content is the key to improving user experience.When the content list is too long, a reasonable pagination mechanism can not only make it easier for users to browse, but also optimize the page loading speed, and indirectly improve the search engine friendliness.In AnQiCMS, implementing pagination and flexibly controlling the display quantity of page numbers is a relatively direct and powerful process.--- ## Display pagination links elegantly and flexibly control the number of pages in AnQiCMS In your AnQiCMS website, when the number of articles, products, or other list content increases

2025-11-08

How to define and use variables in AnQiCMS to display data flexibly in templates?

AnQiCMS (AnQiCMS) is an efficient and flexible content management system, whose powerful template function is the key to building a personalized website.In templates, defining and using variables flexibly allows us to easily handle and display various types of data, whether it's article titles, categorization information, or custom field content. All of this can be dynamically called through variables, making the website full of vitality.We will delve deeper into how to define, obtain, and use variables in AnQiCMS templates, helping everyone build and maintain websites more efficiently.

2025-11-08

How to use the template inheritance and reference feature of AnQiCMS to build reusable display modules?

When using AnQiCMS to build a website, we often encounter such situations: each page of the website has common header navigation, footer information, or some modules (such as sidebars, article recommendation lists) need to be displayed in multiple places, but the content or style may be slightly different.If you copy and paste this code every time, not only is it inefficient, but also once it needs to be modified, you have to adjust it one by one on all related pages, which is very easy to make mistakes and difficult to maintain.

2025-11-08

How to display different content and manage interfaces for multiple sites in the AnQiCMS website?

AnQiCMS, with its powerful multi-site management functions, provides an efficient and flexible solution for operators with multiple brands, sub-sites, or content branches.It allows you to easily create, manage, and display multiple independently operated websites under the same core system, greatly enhancing the efficiency of content operation.

2025-11-08

How to configure and display the contact information of the AnQiCMS website, such as phone number, address, and social media?

The contact information of the website is crucial for establishing a connection and providing support to users.AnQiCMS (AnQiCMS) provides an intuitive and flexible way to manage and display this important information, whether it's phone numbers, addresses, or various social media platform links, all can be easily achieved. ### Configure the website contact information in the background To configure the website contact information, you can first log in to the Anqi CMS backend management interface.In the left navigation bar, find and click "Background Settings", then select "Contact Information Settings".Here is a collection of all the contact information you may need to display

2025-11-08