How to format a timestamp (timestamp) into a readable date format for display in AnQiCMS?

Calendar 👁️ 64

During the operation of a website, the timing of effective content release or updates is crucial for improving user experience and the readability of content.If time information is presented in its original timestamp format, it is often difficult for ordinary users to understand.AnQiCMS provides an intuitive and flexible way to help you convert these digitized timestamps into easy-to-read date and time formats.

How AnQiCMS handles timestamps

AnQiCMS usually uses the standard Unix Timestamp format when storing content time information.This format is a 10-digit or 13-digit integer, representing the number of seconds or milliseconds elapsed since 00:00:00 UTC on January 1, 1970.Although the machine handles it efficiently, it is obviously not user-friendly to display it directly to the user.

To solve this problem, the AnQiCMS template engine is built-in with a powerful tag that allows you to easily format these timestamps on the front-end page.

Core function:stampToDateTag

AnQiCMS providedstampToDateA label specifically used to convert timestamps to readable date and time format. This label's usage is very concise:

{{stampToDate(时间戳变量, "格式化字符串")}}

The key is in the second parameter - 'formatted string'.Due to the template engine of AnQiCMS being based on the Go language, its time formatting follows the unique reference time of the Go language.This reference time is not what we are accustomed toYYYY-MM-DDInstead of such placeholders, it is a specific date2006年01月02日 15时04分05秒. You need to replace the time format you want to display according to the year, month, day, hour, minute, and second positions in this reference date.

For example, if you want to display:

  • Year: Use2006
  • Month: Use01
  • Date: Use02
  • Hour (24-hour format): Use15
  • Minute: Use04
  • Second: Use05

Examples of actual application scenarios

After understanding the formatting rules, let's see how to apply them in a templatestampToDate.

  1. Display the publication and update time of the article

    In AnQiCMS, the article'sCreatedTime(creation time) andUpdatedTime(Updated time) are stored in timestamp format. Suppose you are writing an article detail page or a list page, and you need to display these times.

    • Show only the date (e.g., 2023-01-15)

      {# 假设 item 是当前文章对象 #}
      发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}
      更新日期:{{stampToDate(item.UpdatedTime, "2006-01-02")}}
      
    • Show the full date and time (e.g., 2023-01-15 10:30:45)

      发布时间:{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}}
      更新时间:{{stampToDate(item.UpdatedTime, "2006-01-02 15:04:05")}}
      
    • Custom Chinese format (for example: 2023/01/15)

      发布日期:{{stampToDate(item.CreatedTime, "2006年01月02日")}}
      
  2. Display the comment posting time

    In the comment systemCreatedTimeIt is also a timestamp and can be formatted in the same way:

    评论时间:{{stampToDate(comment.CreatedTime, "2006-01-02 15:04")}}
    
  3. Get and format the current server time

    In addition to storing formatted timestamps in the database, if you need to retrieve and display the current server time, AnQiCMS also provides{% now "格式化字符串" %}tags. Its formatting rules are the same asstampToDateThe formatting string is completely consistent.

    当前时间:{% now "2006年01月02日 15:04:05" %}
    

Notes and tips

  • The accuracy of the timestamp: stampToDateThe tag expects to receive a 10-digit Unix timestamp (second level).If the timestamp is 13 digits (millisecond level), it usually needs to be divided by 1000 to convert it to seconds, or the data processing level of AnQiCMS background has already made adaptation.
  • The formatting rules of Go language are the core:This is the easiest place to confuse. Please remember it2006-01-02 15:04:05This reference date, then replace the corresponding numbers according to the output format you need. For example, if you want to displayMM/DD/YYYYit is written as01/02/2006.
  • withdateThe difference between filters:AnQiCMS template still has one moredatefor example, a filter (such as{{ someDate|date:"2006-01-02" }}It is usually used for parsing into Go language alreadytime.TimeThe date object of the structure, not the raw timestamp. When processing timestamps directly taken from the database,stampToDateit is a more convenient choice.

BystampToDateLabel, AnQiCMS makes the time display on the website clear and flexible, greatly improving the efficiency of your content management and the reading experience of users.


Frequently Asked Questions (FAQ)

Q1: I want to display relative time like '5 minutes ago', can AnQiCMS do thatstampToDateCan the tag do that?A1:stampToDateThe tag is mainly used to format timestamps into specific date and time strings, such as “2023-01-15 10:30”.If you need to display relative time such as "5 minutes ago", AnQiCMS template tags currently do not provide this feature.You usually need to use a frontend JavaScript library (such asmoment.jsorday.jsTo calculate and dynamically display relative time, or to implement secondary development on the AnQiCMS backend.

Q2: If my timestamp is 13 digits (millisecond level),stampToDateCan the tag still be used?A2:stampToDateThe tag is expected to receive a 10-digit second-level Unix timestamp by default.If the timestamp you have is 13 digits in milliseconds, using it directly may cause the date to be incorrect.In this case, you need to ensure that the 13-digit timestamp is divided by 1000 to convert it to a 10-digit second-level timestamp before passing the data to the template. Usually AnQiCMS handles it internally and adapts, but if problems arise, checking the number of digits in the timestamp is one of the troubleshooting directions.

Q3: Why is the time formatting string in Go language2006-01-02 15:04:05Instead of likeY-m-d H:i:slike this placeholder?A3: Go language uses a very unique and intuitive time formatting method: it provides a fixed reference time (2006年01月02日 15时04分05秒) You only need to replace the desired output time format according to the numbers and symbols in this reference date. For example, if you want to display a four-digit year, write 2006If you want to display two-digit months, write01This way, the complexity of memorizing a large number of different placeholders is avoided, as all possible formats are defined through a specific date template.

Related articles

How to use a `for` loop to iterate over data and display content in AnQiCMS templates?

In AnQiCMS template development, we often need to display a series of dynamic content, such as the latest article list, product categories, navigation menu items, and even custom parameters.At this point, the 'for loop' has become the core tool for us to traverse these data and display it on the web page.Understand and master the `for` loop, which will greatly enhance the flexibility and development efficiency of the template.### Core Grammar: The Foundation of Data Traversal The `for` loop syntax in AnQiCMS templates is concise and intuitive, borrowing the style of the Django template engine.

2025-11-08

How to use the `if` tag for conditional judgment to control content display in AnQiCMS templates?

How to use the `if` tag for conditional judgment to control content display in AnQiCMS templates?When building website templates, we often need to display or hide specific content blocks based on different conditions.For example, the image will be displayed only when the article has a thumbnail, or the welcome message will be displayed only after the user logs in.In the AnQiCMS template system, the `if` tag is a powerful tool for implementing conditional judgments.It uses a syntax similar to the Django template engine, concise and expressive, making the content display logic clear and easy to understand

2025-11-08

How to dynamically retrieve and display the contact information of the website in AnQiCMS?

In today's digital age, a website's contact information is more than just cold numbers or addresses; it is a bridge for users to build trust and communicate with the company.Efficiently and accurately display this information on the website, and be able to update it flexibly as needed, which is crucial for any website operator.AnQiCMS as an enterprise-level content management system provides a very intuitive and powerful solution in this aspect, allowing you to easily manage these key information without touching complex code.This article will introduce how to configure and dynamically obtain the contact information of the website in AnQiCMS

2025-11-08

How to display the document list under a specific Tag in AnQiCMS?

In AnQiCMS, tags (Tag) are a powerful and flexible way to organize content, allowing you to associate documents in multiple dimensions without relying on traditional hierarchical classification structures.When you want to display all documents under a specific topic or keyword, it is particularly important to show the document list under a specific tag.This not only helps visitors find the content they are interested in faster, but also effectively improves the internal link structure and SEO performance of the website.

2025-11-08

How to truncate a string and add an ellipsis (...) to limit the display length in AnQiCMS template?

In AnQiCMS template development, we often need to refine the content displayed on the website, such as article titles, summaries, or abstracts of a text.If the content is too long, it not only affects the beauty of the page, but may also reduce the user's reading experience.AnQiCMS's powerful template engine provides a simple and efficient method to solve this problem, allowing us to easily extract strings and automatically add ellipses (...) to elegantly limit the display length of content.

2025-11-08

How to safely output HTML code in the template without escaping in AnQiCMS?

When using AnQiCMS for website template development or content display, you may encounter situations where you need to output HTML code directly on the page, only to find that the code is automatically escaped, for example, the `<p>` tag becomes `&lt;This often confuses those who are new to it.In fact, this is the default measure taken by the AnQiCMS template engine for website security.### Understanding the default security mechanism of AnQiCMS templates The AnQiCMS template engine design has drawn inspiration from

2025-11-08

How to automatically wrap long text to a specified length in AnQiCMS template?

In the presentation of website content, the way long texts are displayed is often a key factor affecting user experience and the beauty of the page.When we need to display a long text content, such as an article summary, product description, or detailed introduction, in a more readable and layout-adaptive way, AnQiCMS's template function provides a powerful and flexible solution.AnQiCMS uses syntax similar to Django template engine, with various built-in filters (Filters) that can help us easily format text.

2025-11-08

How to automatically parse a URL string into a clickable HTML link in AnQiCMS template?

In AnQiCMS template development, we often encounter the need: the URL string contained in the content should be automatically converted into a clickable HTML link to enhance user experience and page interactivity.Manually adding links is undoubtedly cumbersome and unrealistic, fortunately AnQiCMS provides powerful template filters that can help us easily achieve this function.AnQiCMS template engine supports Django-like syntax, which includes a series of practical filters that can process variable content.For automatic parsing of URL strings

2025-11-08