How to format a timestamp into a readable date and time string for display?

Calendar 👁️ 60

In Anqi CMS, how to format timestamps into readable dates: a practical guide

When managing website content, we often encounter the need to display article publishing time, update time, user registration time, and other information.These data are usually stored in the database in the form of a series of numbers - that is, 'timestamps'.Although computers can easily understand these numbers, they may not be as friendly for users visiting websites.For example, seeing a number like '1678886400' is not as intuitive as '2023 March 15 10:00:00'.

Fortunately, AnQi CMS provides a very convenient and powerful template tag that helps us easily convert these timestamps into various readable date and time formats.

Get to know the core tools:stampToDateTag

The AnQi CMS template engine adopts a syntax style similar to Django, one very practical built-in tag beingstampToDate. It is used specifically for formatting timestamp displays.

This tag's basic usage is very intuitive:{{stampToDate(时间戳, "格式")}}

Let's take a detailed look at these two parameters:

  1. Timestamp (the first parameter)This is the original timestamp you need to convert. In AnQi CMS templates, we often get data objects (such as document lists in)itemor document details inarchiveTimestamp can be obtained from the specific field, for exampleitem.CreatedTime(Creation time) orarchive.UpdatedTime(Update time). These fields usually store 10-digit Unix timestamps (second-level).

  2. Format (second parameter)This is the specific format you want the date and time string to appear. It is very important to note that the AnQi CMS template engine follows the Go language time formatting rules rather than using like many other systemsY-m-dorYYYY-MM-DDSuch placeholder.

    Go language uses a fixed 'reference time' to define the format.You just need to write the reference time in the display format you want, and the system will automatically convert it.This magical reference time is:2006年01月02日 15时04分05秒Sometimes it is also written as2006-01-02 15:04:05)

    That is to say, if you want to display the year, you write2006; If you want to display the month, you write01; If you want to display the day, you write02and so on.

To better understand, let's look at some common formatting examples:

The format you want to display Go language format string Sample output (assuming the timestamp is1678886400Represents2023-03-15 10:00:00 UTC+08:00)
Year-Month-Date 2006-01-02 2023-03-15
Year/Month/Day (Chinese) 2006年01月02日 2023年03月15日
Hour:Minute:Second 15:04:05 10:00:00
Year-Month-Date Hour:Minute 2006-01-02 15:04 2023-03-15 10:00
Year/Month/Day Hour:Minute:Second 2006/01/02 15:04:05 2023/03/15 10:00:00
Month/Day/Year (US format) 01/02/2006 03/15/2023
Sunday, Month-Date-Year Time:Minute Monday, 02-Jan-06 15:04 Wednesday, 15-Mar-23 10:00

Actual application example

Now, let's takestampToDateApply the label to the specific template code.

  1. Display the publish date in the document list.Assuming you are iterating over a document list (archives), and hope to display the creation time of each article.

    {% archiveList archives with type="list" limit="10" %}
        {% for item in archives %}
        <li>
            <a href="{{item.Link}}">{{item.Title}}</a>
            {# 显示格式为“2023-03-15” #}
            <span>发布于:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
            {# 显示格式为“2023年03月15日 10:00” #}
            <span>更新于:{{stampToDate(item.UpdatedTime, "2006年01月02日 15:04")}}</span>
            <span>阅读量:{{item.Views}}</span>
        </li>
        {% endfor %}
    {% endarchiveList %}
    
  2. Show detailed time on the document detail pageYou can directly use it on the detail page of a single document.archive.CreatedTimeorarchive.UpdatedTime.

    <article>
        <h1>{{archive.Title}}</h1>
        <div>
            <span>发布日期:{{stampToDate(archive.CreatedTime, "2006年01月02日 15时04分05秒")}}</span>
            <span>最后更新:{{stampToDate(archive.UpdatedTime, "2006/01/02 15:04")}}</span>
            <span>浏览次数:{{archive.Views}}</span>
        </div>
        <div class="content">
            {{archive.Content|safe}}
        </div>
    </article>
    

By these examples, you can seestampToDateThe flexibility and ease of use of the label. Just adjust the format string, and the timestamp can be presented in the way you want.

aboutdateFilter description

When using the AnQi CMS template, you may also notice that

Related articles

How to use a loop (for label) to traverse and display a content list?

In Anqi CMS, whether it is displaying article lists, product catalogs, navigation menus, or friend links, we will frequently deal with content lists.To dynamically display this content on the website front-end, you can't do without powerful looping functions.Today, let's delve deeper into how to use the loop (`for` tag) in Anqi CMS templates to traverse and display your content list. ### The core syntax of the `for` tag: Bring content to life The Anqi CMS template syntax is similar to Django and Blade, very intuitive.When you need to handle a set of data

2025-11-08

How to dynamically display different content blocks based on conditions (if tag)?

In Anqi CMS, the dynamic display of website content is the key to improving user experience and operational efficiency.You often encounter such needs: you want a certain content block to appear only under specific conditions, or to display different information for different situations.At this point, the powerful conditional judgment feature of Anqi CMS - the `if` tag, can be put to good use.The Anqi CMS template system adopts a syntax similar to the Django template engine, allowing you to implement logical judgments in templates in a straightforward manner.By flexibly using the `if` tag, you can easily control the display and hide of web elements

2025-11-08

How to display the background custom banner (Banner) image list in the template?

The banner image on the website is an important element to attract visitors' attention and display core information.In Anqi CMS, you can flexibly customize these banner images in the background and elegantly present them in various template positions on the website.Below, we will discuss in detail how to display the custom banner image list in the template, making your website more attractive. ### Configuration and Management of Background Banner Images In the Anqi CMS backend, the management of banner images is mainly divided into two cases: global homepage banners and specific content banners. 1.

2025-11-08

How to convert image addresses to thumbnail format for display?

In website operation, images play a crucial role.They can not only attract users' attention but also effectively convey information.However, very large images can severely affect website loading speed, thereby damaging user experience and search engine rankings.Therefore, converting the image address to a thumbnail format for display has become an indispensable part of optimizing website performance.AnQiCMS provides very convenient features in this aspect, it can automatically generate and manage thumbnail images for your website, and also provides flexible calling methods to help you easily meet the challenges of image optimization.### AnQiCMS

2025-11-08

How to define template variables and reuse them in content display?

In AnQi CMS, managing and displaying website content, flexibly using template variables is undoubtedly the key to improving efficiency and maintaining consistency.Whether it is necessary to unify the information of the entire site or to customize unique attributes for specific content, understanding how to define and reuse template variables can make content operation more skillful. ### From the background source to define variables In AnQi CMS, many of the information we see on the website every day is actually managed through backend predefined variables.This means you don't have to touch a single line of code to easily define and modify these contents. First

2025-11-08

How does Anqi CMS manage and display uploaded images and video resources?

When using Anqi CMS to manage website content, images and video resources are undoubtedly the key to enriching the page and improving the user experience.AnQi CMS provides a set of intuitive and powerful functions to help us efficiently upload, manage, and display these multimedia contents. ### Easy upload and centralized management of resources When creating articles, products, or single-page content, images and videos can be directly inserted into the content editing area.This process is very smooth, just like using a document editor in everyday life.In addition to this immediate insertion method, Anqi CMS also has a dedicated "image resource management" area

2025-11-08

How do static rules affect the display form and SEO-friendliness of website URLs?

## Static rule of AnQi CMS: How to create a URL structure that users and search engines both love In the operation of a website, the URL is not only the path for users to access the page, but also an important clue for search engines to understand and evaluate the content of the website.A clear, concise, and meaningful URL that greatly enhances user experience and website SEO performance.AnQi CMS knows this, and therefore provides a flexible and diverse static rule management function to help us easily create an ideal URL structure.What is pseudo-static and why is it so important

2025-11-08

How does the automatic compression of images and the handling of large images in content settings affect page display performance?

In website operation, images are indispensable elements that enrich content and attract users.However, if image processing is not handled properly, the large file size often becomes the culprit that slows down page loading speed, thereby affecting user experience and search engine optimization (SEO) performance.AnQiCMS provides a series of powerful image processing features in its content settings, aimed at helping us easily optimize images and significantly improve the display performance of the website.When a visitor opens a page, the browser needs to download all the resources on the page, including text, styles, scripts, and images.If the image file is too large

2025-11-08