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

Calendar 👁️ 64

As an experienced CMS website operation person, I know that the details of content presentation are crucial for user experience.Accurate and clear display of time information, which can not only help users quickly understand the publication or update timeliness of the content, but also indirectly affect the professionalism and credibility of the website.In Anqi CMS, we have a high-efficiency and flexible mechanism that can convert timestamp data stored in the background into user-friendly date and time strings.

Timestamp formatting feature in Anqi CMS

In AnQi CMS, the system internally stores date and time data in the form of timestamps, which is a digital format convenient for computer processing. However, displaying such things directly to users as1609470335This timestamp is not intuitive enough. To solve this problem, Anqi CMS provides a powerful template tagstampToDateIt can convert these original timestamp data into readable date and time strings according to the format you define.

ThisstampToDateTags are the core tools we use in content creation and template design processes to ensure that time information is presented clearly and professionally.Whether displaying the publication date on the article detail page or showing the update time on the list page, it provides precise control.

Understanding Timestamps and Go Language Formatting Layout

While usingstampToDateBefore the label, we need to understand two key concepts: timestamps and Go language time formatting layout.Timestamp usually refers to the number of seconds elapsed since the Unix epoch (January 1, 1970, 00:00:00 UTC), which is typically a 10-digit integer in AnQi CMS.

The 'format' part, Anqi CMS adopts the special time formatting mechanism of the Go language. Like many that use symbols such asYYYY-MM-DD)to define the system of formats, the Go language uses a specific reference time2006-01-02 15:04:05.999999999 -0700 MST(i.e.),January 2, 3:04:05 PM, 2006, located in the Mountain Time Zone of the United States, UTC-07:00Use it as a template. You just need to replace the corresponding part of this reference time with the format you want to display. For example, if you want to display the year, just write it in the format string.2006If you want to display the month, you write01.

stampToDateBasic usage of tags

stampToDateThe label usage is very intuitive. It requires two parameters: the first is the 10-digit timestamp you want to format, and the second is the format string defined in the Go language style.The basic syntax structure is as follows:

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

For example, if you have a variable nameditem.CreatedTimetimestamp variable, and you want to format it as年-月-日format, you can write it like this:

{{stampToDate(item.CreatedTime, "2006-01-02")}}

Actual case: Display of document publishing time

In our daily operations, we often usestampToDateOne of the scenarios for tags is to display the publication or update time of content in document lists and detail pages. For example, on the article list page, we might usearchiveListTag to get document data, where each document object containsCreatedTime(creation time) andUpdatedTimesuch timestamp fields like (update time).

Assuming we want to display the publication date of each article in a list, formatted as "Year-Month-Day", the code can be organized as follows:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
    <li>
        <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
        <p>发布日期:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</p>
    </li>
    {% endfor %}
{% endarchiveList %}

In the article detail page, we can also usearchiveDetailtags to get the detailed information of the current document and format its time field in the same way:

{% archiveDetail archiveContent with name="Content" %}
    <h1>{% archiveDetail with name="Title" %}</h1>
    <p>发布于:{{stampToDate(archive.CreatedTime, "2006-01-02 15:04:05")}}</p>
    <div>{{archiveContent|safe}}</div>
{% endarchiveDetail %}

This display method allows users to clearly obtain key time information at a glance, enhancing the readability and user experience of the content.

Advanced formatting options

The formatting layout of the Go language provides a rich combination to meet various complex date and time display needs. Here are some commonly used and practical formatting string examples:

  • Only show the year:"2006"
  • Show only the month and date:"01-02"
  • Show hours and minutes (24-hour format):"15:04"
  • Show hours, minutes, and seconds (24-hour format):"15:04:05"
  • Complete date and time:"2006-01-02 15:04:05"
  • Date with the week:"Mon Jan 2, 2006"
  • Custom delimiter:"2006/01/02"or"02.01.2006"

By flexibly combining the numbers and text in these reference times, we can create any date and time format that fits the brand style of the website and the habits of users.This allows us to customize the presentation of time information for different content types or different display areas.

Summary

stampToDateThe label is a simple but powerful tool in Anqi CMS, which converts the original timestamp data in the background into user-friendly date and time strings.As a website operator, mastering the usage and flexible application of Go language formatting layout can significantly improve the professionalism and user experience of the website content, making our website more accurate and efficient in conveying information.


Frequently Asked Questions (FAQ)

Q1: Why does the Go language use time formatting strings with2006-01-02 15:04:05as a reference instead ofYYYY-MM-DDsuch symbols?

A1: This is a unique feature of Go language design.It does not use the common symbol placeholders but instead directly uses a fixed reference date and time (i.e., "Go Time": January 2, 2006, 15:04:05) as the "template" for formatting.Each number you write in the formatted string (such as2006represents the year,01Representing months and so on, each corresponds to a specific part of this reference time, in this way indicating how you want the date and time to be displayed.This design may seem unnatural at first contact, but once you understand its logic, you will find it very flexible and intuitive.

Q2: If the timestamp I obtain from the external system is not the standard 10-digit second-level Unix timestamp (for example, it is a 13-digit millisecond timestamp),stampToDatecan the tags still work?

A2:stampToDateThe tag expects to receive a 10-digit second-level Unix timestamp. If your timestamp is 13 digits in milliseconds, you need to convert it before passing it tostampToDateBefore, first convert it to a second-level timestamp by dividing by 1000. For example, if your variable isitem.MillisecondTimeYou may need to handle it like this:{{stampToDate(item.MillisecondTime / 1000, "2006-01-02")}}Please note that the template engine may need to support basic arithmetic operations.

Q3: Can I customize the text in the date, such as displaying 'Monday' as 'Weekday'?

A3:stampToDateThe tag itself is responsible for converting dates and times according to the formatting rules of the Go language.It does not automatically perform language or localization text replacement. If your template supports multi-language functionality (such as throughtrTranslate label), and you want to display different names for weekdays or months based on the language environment. This usually requires combining multi-language files and template logic to achieve. You can use the weekday part in the formatted date (such asMon) as a key, providing the corresponding translation value in the multilingual file.

Related articles

How to traverse an array or list and output each item on the page?

In AnQi CMS template development, effectively traversing and outputting arrays or lists is a core skill for building dynamic and rich content pages.As a senior security CMS operator, I know the importance of flexibly using template tags for content presentation.The Anqi CMS adopts a syntax similar to the Django template engine, making content looping and conditional judgments intuitive and powerful.

2025-11-06

How to use if-else logic in a template to control the display of content?

In Anqi CMS template creation, flexible control of content display is the key to improving user experience and meeting diverse business needs.As an experienced website operator, I am well aware of the importance of dynamic content presentation.The AnQi CMS adopts a syntax similar to the Django template engine, which allows content operators to implement conditional judgments in templates in a straightforward manner, thereby precisely controlling which content is visible to users at what time and place.The template syntax design of AnQi CMS makes it easy for developers and content operators to get started.

2025-11-06

How to customize the display of TDK (Title, Keywords, Description) content on a page?

As a senior AnQiCMS website operations personnel, I fully understand the core role of TDK (Title, Keywords, Description) in website search engine optimization (SEO).They are not only the key for search engines to understand the content of a page, but also important factors to attract users to click.In Anqi CMS, the configuration and custom display of TDK have high flexibility, which can meet the fine-grained optimization needs of different pages.

2025-11-06

How to display all related Tag tags on the article detail page?

As a professional who deeply understands the operation of Anqi CMS, I know that every detail of content creation and display is related to user experience and the SEO performance of the website.Properly displaying related Tag tags on the article detail page can not only help readers quickly understand the core of the article and find more related content, but also has a positive effect on search engine optimization.Below, I will elaborate on how to implement this feature on the article detail page of Anqi CMS.

2025-11-06

How to declare and use custom variables for temporary assignment in a template?

As an experienced security CMS website operator, I know the importance of flexibly using variables in the template development process to improve content display efficiency and maintainability.In Anqi CMS, we can use concise syntax to declare and use custom variables for temporary data assignment, thereby better organizing and presenting website content.### Variable declaration method in Anqi CMS template In the Anqi CMS template system, based on its support for the Django template engine syntax, it provides two main ways of declaring custom variables and temporary assignment for developers

2025-11-06

How to split a template into multiple reusable parts (such as header, footer)?

As a practitioner who is deeply familiar with the operation of AnQiCMS, I know that content quality and website efficiency are the core of attracting and retaining users.In daily operation, we must not only pay attention to the content itself, but also ensure the efficiency and maintainability of the website structure.The modular decomposition of templates is the key technology to achieve this goal. Aqie CMS provides a powerful and flexible template mechanism that allows us to easily decompose website templates into smaller, more manageable reusable parts, such as headers, footers, sidebars, etc.

2025-11-06

How to implement lazy loading of images in article content?

Implementing the lazy loading of images in article content in AnQi CMS is a key link in improving website performance and user experience.As a website operator, I know full well the importance of loading speed for search engine rankings and user retention, and images, as an important part of web content, are often the main factors causing web pages to become bulky and slow to load.

2025-11-06

How to display the custom parameter fields of an article on the article detail page?

As an experienced CMS website operation personnel of an enterprise, I know that the standard fields often cannot meet the growing personalized needs of the website.The introduction of custom parameter fields has provided us with great flexibility, allowing us to add various unique attributes to articles based on different content models, thereby enriching the content dimensions and enhancing user experience.This article will detail how to effectively display the custom parameter fields of an article on the AnQi CMS article detail page.

2025-11-06