How to format a timestamp into a readable date and time format and display it in AnQi CMS?

Calendar 👁️ 61

In website content operation, the presentation of time is often ignored, but it is an important detail to enhance user experience and content professionalism.The original timestamp number lacks meaning to ordinary users; formatting it into a clear and readable date and time format makes the content more engaging.AnQi CMS knows this, providing a simple and powerful way to handle such needs.

Understanding the timestamp in Anqicms

In Anqi CMS, whether it is the publication time of the article (CreatedTime) or the update time (UpdatedTime), these information is usually stored and transmitted in the form of timestamps.A timestamp is a representation of the number of seconds elapsed since a specific moment (usually the Unix epoch, January 1, 1970, 00:00:00 UTC) to a certain point in time.For example, you may get a shape like in the template1609470335The number, this is the unformatted timestamp. Displaying this number directly makes it difficult for users to understand the actual date and time it represents.

Core function:stampToDateTag

An enterprise CMS to solve the readability problem of timestamps has provided a namedstampToDateThe template tag. The core function of this tag is to convert the original timestamp according to the specified format into a human-friendly date and time string.

Its basic usage is very intuitive:{{stampToDate(时间戳变量, "格式字符串")}}

There are two key parts:

  1. Timestamp variableThis is usually fromarchiveDetail/archiveListOr other content tags obtainedCreatedTimeorUpdatedTimefields.
  2. Format stringThis is the key to how date and time are displayed. Used by many CMSY-m-d H:i:sThis placeholder is different, the format string of Anqi CMS follows the special conventions of the Go language, namely using oneFixed reference date and timeServe as a format template. This reference date time is2006-01-02 15:04:05. Just write the reference date in the format you wish, and the system will intelligently format the actual timestamp.

For example:

  • If you wish to display年-月-日Format the string accordingly"2006-01-02".
  • If you wish to display年/月/日Format the string accordingly"2006/01/02".
  • If you wish to display月日Format the string accordingly"01-02".
  • If you wish to display年-月-日 时:分:秒Format the string accordingly"2006-01-02 15:04:05".
  • If you wish to display年时分Format the string accordingly"2006年15时04分".

In this way, you can almost flexibly combine any date and time format you want.

Actual application example

Assuming you are creating an article list page or article detail page and want to display the publication time of each article.

Scenario one: Display the publication date in the article list.

InarchiveListIn the loop, you can use it like thisstampToDateto format tagsCreatedTime:

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

This code will iterate through the list of articles and display the title, link, publishing time (for example2023年10月27日) and summary of each article.

Scenario two: Display the update time on the article detail page

In the article detail page, you may need to display the article's update time to the minute:

<article>
    <h1>{% archiveDetail with name="Title" %}</h1>
    <p>
        发布于:<span>{{stampToDate(archive.CreatedTime, "2006-01-02 15:04")}}</span>
        更新于:<span>{{stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05")}}</span>
    </p>
    <div>
        {%- archiveDetail articleContent with name="Content" %}
        {{articleContent|safe}}
    </div>
</article>

We used herearchive.CreatedTimeandarchive.UpdatedTime(In the document detail page,archiveVariables usually represent the current document object and are used separately2006-01-02 15:04and2006-01-02 15:04:05Formatted.

Practical skills and precautions

  • Maintain consistencyKeep the date and time format consistent throughout the website, which helps improve user experience and the professional image of the website.
  • Consider the target audience: If your website has an international audience, consider using a more generic date format (such asYYYY-MM-DD) or switch to different formats according to the language pack.
  • Memory of Go language string formatting: At first you may feel2006-01-02 15:04:05difficult to remember, you can think of it as a special 'magic date', just remember the meaning of each number and symbol. For example:
    • 2006Year
    • 01Month
    • 02Day
    • 15Hour (24-hour format)
    • 04-> Minutes (Minute)
    • 05-> Seconds (Second)

by masteringstampToDateThe use of tags, you can easily convert timestamp data from Anqin CMS into a user-friendly date and time information, making your website content more professional and easier to read.


Frequently Asked Questions (FAQ)

1. Why do format strings use2006-01-02 15:04:05such specific number combinations instead ofYYYY-MM-DDsuch general placeholders?

This is because Anqi CMS is developed based on Go language, and Go language uses a fixed reference time for formatting date and time2006-01-02 15:04:05 -0700 MST(commonly referred to as 'magic time', in which each number and abbreviation corresponds to a specific time unit, for example2006represents the year,01Representing months, etc.). Simply apply the output format you expect to this reference time, and the system will automatically complete the conversion. This is a concise and efficient format definition method unique to the Go language.

2. If I only want to display the time, for example10:30How should the format string be set?

If you only want to display the time, you can directly use the time part of the reference time to construct the format string. For example, if you want to display时:分The format string can be set to"15:04"If you want to display时:分:秒Then use"15:04:05".

3.stampToDateDoes the tag support time zone conversions for all?

stampToDateThe tag is responsible for converting timestamps to strings in a specified format.By default, it will convert based on the time zone set by the server.If you need to display different time zones for different users, this usually requires handling time zone conversion of timestamps in backend logic, or ensuring that the server's time zone settings match the main audience's needs.The template tag itself does not provide direct timezone conversion parameters, it focuses on formatting timestamps with the existing timezone.

Related articles

How to perform a loop (for) in Anqi CMS template to display list data?

When building a website page, we often need to display various list data, such as the latest articles, product categories, navigation menus, or comment details.The template system of AnQiCMS provides us with a powerful and flexible loop traversal function, making the display of dynamic lists very simple.The template syntax of AnQi CMS borrows from the Django template engine, so users familiar with this syntax will feel very close.It uses the `for` loop label to iterate over the data collection and display each item one by one.###

2025-11-08

How can conditional judgments (if/else) be used in the AnQi CMS template to control content display?

AnQiCMS template development skills: flexibly use conditional judgment (if/else) to control content display In website content operation, we often need to display or hide specific content blocks based on different conditions, or to differentiate the display based on different data states. AnQiCMS (AnQiCMS) powerful template engine provides us with flexible conditional judgment capabilities, through the clever use of `if/else` statements, you can easily achieve the fine-grained control of content, making your website more dynamic and user-friendly.

2025-11-08

How does the comment function of AnQi CMS display comment lists and reply levels?

AnQiCMS (AnQiCMS) as an efficient and flexible content management system, provides rich content display capabilities while also offering a comprehensive comment function for user interaction.Understand how to effectively display a comment list, especially in handling multi-level replies, which is crucial for enhancing the user engagement and content depth of a website. ### AnQiCMS Comment Function Implementation Basics Commenting functionality is an important part of website content interaction in AnQiCMS.The system is not only built-in with support for comments on articles, products, and other content

2025-11-08

How to add and display friendship links on the Anqi CMS website?

In website operation, friendship links play an indispensable role.They not only help to improve the search engine ranking (SEO) of a website, but also bring potential traffic and improve the user experience, allowing visitors to get more information through related websites.AnQiCMS (AnQiCMS) understands this and therefore provides an intuitive and convenient friend link management feature, allowing you to easily add, manage, and display these important external connections.This article will introduce in detail how to add friend links in the Anqi CMS background, as well as how to flexibly display them on the website's front-end template

2025-11-08

Does AnQi CMS support the automatic conversion of uploaded images to WebP format for optimized display?

In the era where content is king, the loading speed of website images directly affects user experience and search engine optimization (SEO) performance.Efficient image management and optimization strategies are crucial for any website committed to providing high-quality content services.Many website operators are actively seeking solutions that can automatically optimize images, especially those converted to WebP format.So, for those who are using or considering using AnQiCMS, does this system support automatic conversion of images to WebP format after upload to optimize display?The answer is affirmative

2025-11-08

How to set up automatic image compression in Anqi CMS to speed up page loading?

Optimize images, speed up the website: AnqiCMS Automatic Compression Settings Guide Website access speed is one of the key factors in user experience and an important consideration for search engine optimization (SEO).And large images are often the culprit that slows down website loading speed.Fortunately, AnqiCMS, as a fully functional content management system, is built with a series of powerful image optimization features, which help us easily deal with this issue, while keeping the website smooth in providing rich visual content.Today, we will take a detailed look at how to use AnqiCMS

2025-11-08

How to configure content automatic external link filtering in Anqi CMS and how it affects the display of front-end links?

In website content operation, the management of external links is a vital aspect that cannot be ignored.It not only relates to user experience, but also directly affects the website's search engine optimization (SEO) performance.AnQi CMS knows this and therefore provides a flexible and practical external link handling mechanism to help content operators efficiently manage external links in articles.Where is the AnQi CMS external link configuration? AnQi CMS provides a very convenient function to handle external link issues, and you can easily configure it in the system background.The specific path is: log in to your AnQi CMS backend

2025-11-08

How to reference common code snippets (such as headers and footers) in Anqi CMS templates to unify page display?

It is crucial to maintain consistency in the display of web pages during website operations.A unified website appearance can not only enhance brand professionalism but also optimize user experience.AnqiCMS provides a powerful and flexible template mechanism, allowing us to efficiently manage common website elements such as headers and footers, thus avoiding repetitive code writing and ensuring the consistency of the entire site style.The AnqiCMS template system draws on the syntax of the Django template engine, with the core idea being that developers can define reusable code snippets and page skeletons

2025-11-08