How to format timestamp data into a readable date and time format and display it?

Calendar 👁️ 74

In website operation and content management, we often need to handle various data, among which date and time information is particularly common.However, these data are often stored in a string of seemingly unordered numbers (i.e., timestamps), which is difficult for ordinary users to understand intuitively.AnQiCMS as an efficient enterprise-level content management system fully considers this user's needs, provides a simple and powerful way, helps us convert these timestamp data into an intuitive date and time format, and present it on the website.

The template engine design of AnQiCMS draws on the syntax style of Django templates and Blade templates, making it easy for content developers to process and display data through specific tags and filters. To solve the problem of timestamp formatting, we will mainly rely on the built-in features of AnQiCMS.stampToDate.

Understand timestamps: what are they and why do they need to be formatted?

In simple terms, a timestamp is a number that represents the number of seconds elapsed since January 1, 1970, 00:00:00 UTC. In AnQiCMS, you usually encounter 10-digit Unix timestamps, such as1609470335.

Such numbers are efficient and easy to store and calculate for computers, but they are meaningless for users accessing websites.Therefore, formatting these original timestamp data into readable date and time formats such as “October 27, 2023”, “10:30 AM” or “2023-10-27 10:30:00” and so on is a key step to improving user experience.

Timestamp data source in AnQiCMS

In AnQiCMS, many core time information of the website content exists in the form of timestamps. For example, when you publish an article, update a product, or a user submits a comment, the relevant creation time (CreatedTime)、Update Time(UpdatedTimeFields such as these are stored in timestamp format. These fields are usually used as content models or list items (item) as properties.

For example, in the document details or document list, you may encounter the following fields representing timestamps:

  • item.CreatedTime: Creation time of the content.
  • item.UpdatedTime: Last update time of the content.
  • archive.CreatedTime: The creation time of a single document.
  • user.LastLogin: The last login time of the user.

Next, let's take a look at how to do theseitem.CreatedTimeTimestamp variables, converted to the date and time format we are familiar with.

Core function:stampToDateThe use of tags

AnQiCMS provides a namedstampToDateThe built-in template tag is used specifically to format timestamp data into a specified readable date and time format. This tag is very intuitive and flexible to use.

The basic syntax of its use is:{{stampToDate(时间戳变量, "格式字符串")}}

Here is the 'timestamp variable' that you want to format, for example,item.CreatedTime. The format string is used to define the key to display the date and time in the desired format.

It is worth noting that the template engine of AnQiCMS is based on Go language, and its date formatting string follows the unique rules of Go language. Unlike what we are accustomed to,YYYY-MM-DDoryyyy-MM-ddDifferent, Go language uses a fixed reference date2006-01-02 15:04:05(UTC time, i.e., 3:04:05 PM on January 2, 2006) as the format template.You only need to replace the year, month, day, hour, minute, and second in this reference date with the format characters you want, and the system will automatically generate the corresponding date and time string based on the timestamp passed in.

Here are some common formatting examples:

  • Only display the date (year-month-date)If you only need to display the date, for example “2023-10-27”:

    {{stampToDate(item.CreatedTime, "2006-01-02")}}
    
  • Show the full date and time (Year-Month-Day Hour:Minute:Second)If you need to display the date and time to the second, for example, "2023-10-27 10:30:00":

    {{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}}
    
  • Custom Chinese formatIf you want to display it in a format more in line with Chinese habits, for example, "October 27, 2023":

    {{stampToDate(item.CreatedTime, "2006年01月02日")}}
    
  • Other custom formatsYou can combine it flexibly as needed, for example, "10/27/2023" or "Friday Morning 10:30"

    {{stampToDate(item.CreatedTime, "01/02/2006")}}  {# 月/日/年 #}
    {{stampToDate(item.CreatedTime, "Mon 15:04")}} {# 星期几 时:分 #}
    

Practice exercise: Apply date format in the content list

In the actual website content display, we usually use this feature on list pages (such as article lists, product lists) or detail pages (such as article details).

Assuming we have a list of articles that include the titles and publication dates. Below is an example of how to apply in a templatestampToDateLabel example:

`twig {# Assuming

Related articles

How to use `if` and `for` tags to flexibly control content display logic?

## Unlock the mysteries of AnQi CMS content display: the flexible use of `if` and `for` tags AnQi CMS is committed to providing users with an efficient and customizable content management experience.In website content operation, we often need to display different information based on different conditions, or display a series of contents in bulk.

2025-11-08

How to correctly display pagination navigation for articles or category lists?

When managing website content, whether it is an article list, product display, or other category pages, a clear and effective pagination navigation is crucial for improving user experience and search engine optimization.AnQiCMS (AnQiCMS) provides powerful template tag features, allowing us to easily add correct pagination navigation to the list page.This article will introduce how to implement this feature in AnQi CMS.

2025-11-08

How to integrate and display a friend link list on a website?

In website operation, friendship links play an indispensable role. They are not only the bridges to establish contact with other websites but also effective ways to improve website authority, obtain external traffic, and enhance user experience.AnQiCMS as an efficient and flexible content management system provides an intuitive and convenient solution for the integration and display of friend links. ### First Part: Admin Friend Links One of the design philosophies of AnQiCMS is to make website operation more efficient, and the management of friend links is naturally integrated very well.

2025-11-08

How to display the user comment list below the article page?

In website operation, user comments are an important aspect for enhancing content interaction, strengthening community atmosphere, and optimizing search engine rankings (SEO).AnQiCMS provides a flexible and powerful template tag system, making it easy and convenient to integrate a user comment list and comment form at the bottom of the article page.This article will provide a detailed introduction on how to elegantly display user comments on the article detail page of AnQi CMS, and also provide a function for users to submit comments.

2025-11-08

How to customize website structured data (JSON-LD) to improve search engine display?

It is crucial to make the website content stand out in the search engine results page (SERP) in today's highly competitive online environment.Structured data, especially JSON-LD format, is the key tool to achieve this goal.It helps search engines better understand the content of the page, thereby giving them the opportunity to display richer and more attractive "rich media summaries" (Rich Snippets), such as the publication date of the article, the author, ratings, product prices, etc., which can significantly increase click-through rates.

2025-11-08

How to display language switch options on the front-end page and support hreflang attributes?

AnQiCMS as an efficient and flexible content management system provides a solid foundation for users in global content promotion, and its built-in multilingual support feature is the key to achieving this goal.Display language switch options correctly on your website and configure the `hreflang` attribute for search engines, which not only greatly enhances user experience but is also an important step for international SEO optimization.### One, Preparation: Enable AnQiCMS Multilingual Functionality AnQiCMS itself has strong multilingual support capabilities

2025-11-08

How to manage and display the homepage banner carousel?

The homepage banner carousel is the face of the website, which not only attracts the attention of visitors in the first place, but also an important carrier for conveying brand information and promoting core products or services.In AnQiCMS, managing and displaying the homepage banner carousel is a straightforward and flexible process, allowing you to easily create an eye-catching visual focus for your website. ### One, manage your Banner content in the background To manage the homepage Banner carousel, you first need to log in to the AnQiCMS backend management interface. Usually

2025-11-08

How to safely display HTML or JS code in a template without escaping?

In AnQi CMS template development and content operations, sometimes we need to directly display HTML code or run JavaScript scripts on the page, rather than have them parsed by the browser as plain text.This usually occurs when it is necessary to embed third-party statistical code, advertisement scripts, or when displaying code examples in article content.However, AnQi CMS, for security reasons, defaults to escaping the content output to templates to prevent security vulnerabilities such as cross-site scripting (XSS).To safely display HTML or JS code in a template without being escaped

2025-11-08