How to format timestamps and display them on the page as a readable date and time?

Calendar 👁️ 63

In website operation, the timeliness of content is often the key to attracting readers' attention.Whether it is the publication date of the article or the update time of the product, presenting this information in a clear and understandable way on the page will greatly enhance the user experience.However, you may find that the time information recorded by AnQiCMS backend, when directly called in the template, often appears as a long string of numbers, which is what we commonly call a timestamp.These original timestamps are convenient for internal system processing, but they are lacking in readability for ordinary users.

Luckily, AnQiCMS provides a very practical and powerful tag that can easily convert these timestamps into the easily readable date and time format we are familiar with in our daily life.

Familiarize yourself with timestamps and the time data in AnQiCMS.

In AnQiCMS, many content objects contain time-related fields. For example, when you usearchiveDetailorarchiveListLabels to get articles (archive) or product details will findCreatedTime(creation time) andUpdatedTimeThe fields such as (update time). These fields return the Unix timestamp, which is the number of seconds elapsed since 00:00:00 UTC on January 1, 1970.They accurately record the time points of events, but further formatting is needed to display them in a friendly manner to visitors.

applystampToDateFormat the tag

To solve the readability problem of timestamps, AnQiCMS built instampToDateThis template tag. Its usage is very intuitive, you just need to provide the timestamp to be formatted and specify the date-time format you want it to display. Its basic syntax structure is as follows:

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

The "timestamp" here is the number value you obtained fromCreatedTime/UpdatedTimethese fields. The "format" section is the key to defining the output style.

Mastering date-time formatting: The magic numbers in Go language

AnQiCMS is developed based on the Go language, therefore, its date and time formatting rules also follow the unique way of the Go language. You may need to remember a special reference time:2006-01-02 15:04:05. This string is not random, but a standard template used by the Go language to represent the parts of date and time:

  • 2006: Represents the year
  • 01: Represents the month (preceded by a zero)
  • 02: Represents the date (preceded by a zero)
  • 15representing 24-hour format hours03means 12-hour format
  • 04Represents minutes
  • 05Represents seconds

By cleverly combining and arranging these 'magic numbers', you can flexibly customize various date and time display effects.

Some common formatting examples:

  • Show only year, month and day (separated by hyphens): "2006-01-02"For example, it will show as:2023-10-27.
  • Show only year, month and day (separated by slashes): "2006/01/02"For example, it will show as:2023/10/27.
  • Show full date and time (to the minute): "2006-01-02 15:04"For example, it will show as:2023-10-27 10:30.
  • Display the complete date and time (to seconds): "2006-01-02 15:04:05"For example, it will show as:2023-10-27 10:30:45.
  • Chinese date format: "2006年01月02日"For example, it will show as:2023年10月27日.

Practical application: tostampToDateintegrate into the template

Now, let's see how to apply it in the actual AnQiCMS template.stampToDateTags:

Example 1: Display the publishing time on the article detail page

<div class="post-meta">
    <span class="publish-date">发布时间:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}</span>
</div>

Example 2: Display the update time of each article on the article list page

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <div class="article-item">
        <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
        <p class="update-info">最近更新于:{{ stampToDate(item.UpdatedTime, "2006/01/02") }}</p>
    </div>
    {% endfor %}
{% endarchiveList %}

You can see these simple code snippets.stampToDateHow to combine tags witharchiveDetailorarchiveListUsing the timestamp field to combine. Whether you want to display time accurate to seconds or just need a simple date, this tag can easily help you achieve it, making your website content come alive.

When designing a template, it is recommended that you choose a unified date and time display format for the entire website to ensure consistency in page style, thereby providing visitors with a smoother and more professional browsing experience. MasteringstampToDateLabel, it is an indispensable practical skill in AnQiCMS content operation.


Frequently Asked Questions (FAQ)

  1. Q: Why is there a deviation between the formatted time and the actual time?A: The AnQiCMS server uses the time zone of the server it is located on by default when processing timestamps.If your server time zone does not match the time zone you expect to display (such as your local time zone), there will be a deviation.You can check the time zone configuration of the server, or adjust it if AnQiCMS provides the corresponding time zone configuration options.In some special cases, it may be necessary to convert the timestamp to a time zone by the backend logic before passing it to the template.
  2. Q:stampToDateCan the label handle all types of timestamps? For example, 13-digit millisecond timestamps?A:stampToDateThe tag is expected to handle a 10-digit Unix timestamp, which is a second-level timestamp. If your timestamp is a 13-digit millisecond timestamp, you may need to convert it before passing it instampToDateFirst, convert it to a 10-digit timestamp in seconds (for example, by dividing it by 1000 in the Go language timestamp acquisition logic).
  3. Q: Can I add non-numeric charactersstampToDateto the format string of the label, such as "AM/PM"?A:stampToDateThe format string is based on the reference time of the Go language2006-01-02 15:04:05In this reference time,03represents the hour in a 12-hour format (with a leading zero), along withPMcan indicate morning or afternoon. For example,"2006-01-02 03:04 PM"It will display2023-10-27 10:30 AMor2023-10-27 10:30 PMYou can combine these elements and customize the text as needed to achieve a more complex display effect.

Related articles

How to iterate over list data in a template and display it one by one?

Managing and displaying content in Anqi CMS is one of its core advantages.When we need to display dynamic list data on the front end of a website, such as the latest articles, popular products, and content collections under categories, understanding how to efficiently iterate through these data in templates and display them one by one is an essential skill for website operators and template developers.The Anqi CMS template system adopts syntax similar to Django, which is intuitive and powerful, making the transformation of technical information and practical application extremely convenient.### Core Loop Syntax: `{% for %}`

2025-11-08

How to use conditional judgment logic in a template to control the display and hiding of content?

In the presentation of website content, we often need to decide based on different situations which information should be displayed to visitors and which should be temporarily hidden.AnQiCMS (AnQiCMS) provides flexible template conditional judgment logic, making content display and hide intuitive and efficient.By these judgments, you can easily implement personalized page layouts, content display under permission control, and adjustment of page elements based on data status, which greatly enhances the dynamicity and user experience of the website.The Anqi CMS template system uses a syntax similar to the Django template engine

2025-11-08

How to display breadcrumb navigation on the page to enhance users' understanding of the content structure?

When we browse websites, we often see a line of text at the top or above the content area, clearly indicating the position of the current page within the overall website structure, like "Home > Product Center > Some Product Category > Some Product Detail" and so on.This is what we commonly refer to as 'Breadcrumb Navigation'.It can not only help users quickly understand the content hierarchy of the website, avoid getting lost, but also play an important role in user experience and search engine optimization (SEO).Aq CMS fully understands the importance of breadcrumb navigation

2025-11-08

How to build and display a multi-level navigation menu for a website?

Website navigation, like the skeleton and compass of a website, it guides visitors to explore the website content, and is the core of user experience and information architecture.A clear and accessible navigation menu that not only helps users find the information they need quickly, but is also crucial for search engine optimization (SEO).AnQiCMS (AnQiCMS) is well-versed in this, providing powerful and flexible navigation management features that allow you to easily build and display multi-level navigation menus.Next, we will delve deeper into how to build and display your website navigation step by step in Anqi CMS.--- ### One

2025-11-08

How to set the recommendation attribute to affect the display priority of the front-end content when publishing a document?

How to effectively guide visitors to focus on key information when managing content in AnQi CMS, and enhance the visibility and interactivity of the content, is a common concern for operators.By setting the recommended properties of the document reasonably, we can finely control the display priority of the front-end content, thereby achieving this goal.This not only helps to optimize the user experience, but also better serves the overall operation strategy of the website.### Backend settings recommended attribute: clear content positioning When we publish or edit a document, the content editing interface of Anqi CMS provides a feature area named "Recommended Attribute".

2025-11-08

How to customize the document URL alias to optimize its display in browsers and search results?

In website operation, a URL (Uniform Resource Locator) is not only the address of the content, but also the first step for users and search engines to recognize the website.A clear, meaningful, and friendly URL alias that can greatly enhance the display effect of content in the browser and achieve better visibility in search results.AnQiCMS provides flexible and powerful features, allowing us to easily customize documents, categories, tags, and even the URL aliases of the page itself, thereby optimizing the overall performance of the website.Why is it important to use custom URL aliases?Firstly, for search engines

2025-11-08

How to use the scheduled publishing feature to automatically go live and display documents at specified times?

## Precisely Control Content Rhythm: A Practical Guide to AnQi CMS Scheduled Publishing Function In today's increasingly refined content operation, how to ensure that content reaches the target audience at the most appropriate time is a core challenge faced by every operator.Manually publishing is inefficient and is prone to delays or errors due to temporary affairs or time differences.Fortunately, AnQi CMS provides a powerful and practical feature - scheduled publishing, which can make content going live automated and precise, bringing unprecedented flexibility and efficiency to website operations.The timing release function of Anqi CMS, as the name implies

2025-11-08

How to add multiple Tag tags to documents and associate them for display and filtering on the front end?

In AnQiCMS, adding Tag tags to documents and associating them for display and filtering on the front end is an effective way to enhance the organization of website content and user experience.Tag labels can not only help users find the content they are interested in faster, but also optimize the keyword layout of the website from the SEO level, increasing the exposure of the content.Next, we will discuss in detail how to implement this feature in AnQiCMS. ## One, add Tag label to document in the background Adding a Tag label to the AnQiCMS backend is a straightforward and convenient process. First

2025-11-08