How to ensure that the date and time are displayed correctly on the front end using the timestamp formatting tag?

Calendar 👁️ 79

In website content operations, the correct display of date and time information is crucial, as it not only affects the user's reading experience but also directly relates to the timeliness and accuracy of the information.AnQiCMS (AnQiCMS) understands this and provides a powerful and flexible timestamp formatting tag that allows developers and operators to easily convert the original timestamps stored on the backend to a front-end user-friendly date and time format.

Why do we need timestamp formatting?

We know that the data stored on the website backend, especially the publication time, update time, comment time, etc., usually exists in the form of a timestamp (Unix timestamp). For example, you might see a string of numbers1678886400Such numbers are efficient for computers, but they are meaningless for users visiting websites.Therefore, we need a mechanism to format these original timestamp data into various easily readable forms such as "March 15, 2023", "3 PM this afternoon", "3 hours ago", etc., according to the user's habits and the design requirements of the page.

The solution of Anqi CMS:stampToDateTag

An enterprise CMS provides a dedicated timestamp label formatstampToDate. This tag can accept a timestamp and a format string as parameters and output the corresponding date and time according to the defined format.

Its basic usage method is very concise and clear:{{stampToDate(时间戳, "格式")}}

The key is in two parts:

  1. Timestamp (时间戳)This is usually a 10-digit or 13-digit number, representing the number of seconds or milliseconds elapsed since the Unix epoch (00:00:00 UTC on January 1, 1970). In AnQi CMS, for example, the article'sCreatedTimeorUpdatedTimecomments,CreatedTimeare all provided in this form.
  2. format("格式")This is the string defining the date and time display style. The format string of Anqicms follows the Go language time formatting rules and is not traditional.YYYY-MM-DDSuch placeholders, instead, use a special 'reference time' to define the layout.

Understanding Go language time formatting reference time

The time formatting in Go language is quite unique, it uses a fixed reference time:2006年01月02日 15时04分05秒Corresponding to2006-01-02 15:04:05.000000000 -0700 MST)

You can replace the part you want to display with the corresponding number in this reference time. For example:

  • If you want to displayYear, use2006.
  • If you want to displayMonth, use01(with a leading zero) or1(without a leading zero).
  • If you want to displayDate, use02(with a leading zero) or2(without a leading zero).
  • If you want to displayhours, use15(24-hour format) or03(12-hour clock, must be paired withPM/AM)
  • If you want to displayminute, use04.
  • If you want to displayseconds, use05.

By combining these numbers, you can construct various complex date and time formats.

Common formatting examples:

  • "2006-01-02": Display as2023-03-15
  • "2006年01月02日": Display as2023年03月15日
  • "15:04:05": Display as14:30:00
  • "2006/01/02 15:04": Display as2023/03/15 14:30
  • "Mon, 02 Jan 2006": Display asWed, 15 Mar 2023

Use case: Display the backend timestamp on the front end

In Anqi CMS template development, we often encounter scenarios where we need to display timestamps.For example, the publication and update time of the article detail page, the entry time in the article list, the time of each comment in the comment list, even the time of user registration or last login, and so on.

For example, with a list of articles, suppose we want to display the article title and its publication time, update time:

{% archiveList archives with type="list" limit="5" %}
    {% for item in archives %}
    <div class="article-item">
        <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
        <p>发布时间:{{ stampToDate(item.CreatedTime, "2006年01月02日 15:04") }}</p>
        <p>更新时间:{{ stampToDate(item.UpdatedTime, "2006/01/02") }}</p>
        <p class="description">{{ item.Description }}</p>
    </div>
    {% endfor %}
{% endarchiveList %}

In this code block:

  • item.CreatedTimeanditem.UpdatedTimeIt is the original timestamp obtained from the backend.
  • stampToDateThe label receives these timestamps.
  • The firststampToDateUse"2006年01月02日 15:04"Format, convert the timestamp to a Chinese display of “year, month, day, hour, minute”.
  • the secondstampToDateUse"2006/01/02"Format, convert the timestamp to a concise display of "Year/Month/Day".

In this way, even if the backend returns a string of numbers, the frontend can present it to the user in a clear and beautiful way according to the preset format.

Advanced tip: Distinguish between time types and labels

The Anqi CMS template engine also providesdateandtimeFilters, but please note that these filters are mainly used to process Go language intime.TimeType (representing an object for dates and times), not directly handling raw numeric timestamps. If you try to pass a numeric timestamp todateortimea filter, you are likely to encounter an error.

Therefore, when you clearly know that the data source is a "timestamp", you must usestampToDatea tag, which is born for this purpose."

If you need to displaythe current timeInstead of the specific event time stored in the database, Anqi CMS also provides{% now "格式" %}Tags. For example, to display the current year in the footer:{% now "2006" %}

Summary

BystampToDateLabel, you can easily convert the timestamp stored on the backend to a user-friendly date and time format, greatly enhancing the readability and user experience of the website content.Mastery of the reference time format of Go language will be the key to efficient template development in Anqi CMS.No matter whether you need to display time to the second or a concise date,stampToDateCan meet all your needs, ensure the correct display of date and time on the front end.


Frequently Asked Questions (FAQ)

1. Why do I use it directly in the template?{{ item.CreatedTime }}What I get is a string of numbers, not a date?This is becauseitem.CreatedTimeIn the backend database, it is usually stored in the form of a Unix timestamp (Timestamp), which represents the number of seconds from January 1, 1970 to the present.The browser cannot directly identify it as a readable date format.You need to use the secure CMS provided bystampToDateLabel it so that it can be correctly displayed as a date and time on the front end.

2. I tried to use{{ item.CreatedTime|date:'YYYY-MM-DD' }}filter, but the page reported an error, why is that?In the AnQi CMS template engine,dateThe filter is used to process Go languagetime.Timeobjects, rather than processing raw digital timestamp numbers directly. If you want to format the timestamp, you must usestampToDateLabel, and define the date format according to the reference time format of Go language (such as"2006-01-02"), rather than usingYYYY-MM-DDand other placeholders.

3. What tag should I use if I only need to display the current time instead of the content publish time?If you want to display the current date or time in the template instead of the timestamp of the specific content retrieved from the database, you can use{% now "格式" %}Tag. For example, {% now "2006年" %}It will display the current year. The format string also follows the time format rules of the Go language.

Related articles

How to use if and for tags in templates to implement conditional judgment and loop display of data?

When building a website with AnQiCMS, the template is the key to content display.By flexibly using template tags, we can present the content of the back-end management in various forms to the users.Among them, `if` and `for` tags serve as the core of template logic control, helping us to implement conditional judgments and the cyclic display of data, making the website content more dynamic and rich.AnQiCMS's template engine borrows the syntax style of Django, which makes learning and using these tags very intuitive.It allows us to write logic directly in the template

2025-11-09

How to configure pagination tags to provide user-friendly content list navigation?

In Anqi CMS, as the content on our website becomes richer and richer, how to efficiently organize and present this information so that visitors can easily find what they need among the vast amount of content has become a key issue.Excellent content list navigation, especially the pagination feature, not only enhances user experience, but is also an indispensable part of search engine optimization (SEO).The AnQi CMS provides powerful and flexible pagination tags, allowing you to easily configure user-friendly content list navigation.### The Foundation of Content List: Configure Content Retrieval Tag In Anqi CMS, all content lists, whether articles

2025-11-09

How to use友情链接 tags to display partner websites at the bottom of the website or other areas?

In website operation, the friendship link of cooperative websites is not only an important part of search engine optimization, but also an effective way to expand brand influence and promote mutual traffic redirection.Display these important cooperative partnerships prominently on the website, such as at the bottom or sidebar, which can effectively enhance user trust and the authority of the website.AnQiCMS (AnQiCMS) provides a convenient friend link management function and flexible tag calling method, allowing you to easily achieve this goal.### Overview of the friendship link backend management Before using the `linkList` tag

2025-11-09

How to safely and effectively display user comment lists and message forms in a template?

In modern website operations, user-generated content (UGC) is an important component for enhancing website activity and user stickiness.User comments and online message functions not only promote community interaction and provide valuable feedback, but also help enhance the richness of content and the activity of search engines.AnQiCMS as a content management system focusing on efficiency, security, and customization, provides us with powerful and flexible support to display these features safely and effectively in templates.Ensure that the comment list and message form are presented beautifully while兼顾 safety and functionality

2025-11-09

How to reference external HTML fragments (such as headers, footers) in AnQiCMS templates to display modular content?

In website operations, maintaining the modularization and high reusability of content can not only significantly improve development efficiency, but also make the website more flexible during iterative updates.AnQiCMS (AnQiCMS) provides a straightforward and efficient solution with its powerful template engine to achieve this goal.This article will delve into how to make clever use of the Anqi CMS template mechanism, referencing external HTML fragments such as headers and footers, thus building a website that is easy to manage and has a clear structure. ### The Value of Modular Content: Why Reference External HTML Fragments?Imagine

2025-11-09

How to define and use macro functions to reuse display logic in AnQiCMS templates?

In the template development of Anqi CMS, we often encounter the need to reuse HTML code snippets or display logic.If you copy and paste every time, it not only increases the amount of code, reduces development efficiency, but also brings many inconveniences in later maintenance.To solve this problem, AnQi CMS template engine provides a powerful macro function (Macro) that helps us achieve the reuse of display logic, making the template code more concise and efficient.What is a template macro function?\n\nA macro function can be understood as a "custom function" in the template.

2025-11-09

How does the template inheritance feature help developers build a unified and easy-to-maintain page layout?

In AnQiCMS, building a unified and easy-to-maintain page layout is crucial for the long-term healthy operation of any website.As the face of a website, the visual consistency of a page directly affects user experience and brand image;The maintenance efficiency behind it determines the speed of content updates and feature iterations.AnQiCMS's powerful template inheritance feature is the core tool to solve this challenge.The core concept of template inheritance lies in **reusability and layering**.It allows us to create a basic "master" template that includes the common structure and elements of all web pages, such as headers and footers

2025-11-09

How to display AnQiCMS system configuration information on the front end (such as website name, Logo, filing number)?

In AnQi CMS, when building a website, the first thing to consider is often how to elegantly display basic system configuration information on the website front-end, such as the website name, Logo image, and ICP record number.This information is not only the 'front door' of the website, it concerns the brand image, but also an important part of enhancing user trust and even complying with laws and regulations.AnQi CMS provides us with a very convenient way, easily realizing the dynamic call of these system information through template tags.###

2025-11-09