How to format a timestamp with the `stampToDate` tag to display the date and time in a user-friendly manner?

Calendar 👁️ 53

In the daily content operation of AnQi CMS, we often need to display the original timestamp data such as publication time and update time in a way that is more accustomed and easier to read for users. For example, the original timestamp is1609470335Such a pure numeric sequence has no meaning to ordinary users. To solve this problem, AnQi CMS provides a very practical template tag -stampToDateIt can help us convert these numeric timestamps into a clear and user-friendly date and time display.

stampToDate: Making timestamps readable

stampToDateThe core function of the tag is to beautify the output of a 10-digit or 13-digit (if internally processed as milliseconds) Unix timestamp according to the specified format.This greatly enhances the professionalism and user experience of the website content.Imagine if the posting time of the article only showed a string of numbers, the user might feel confused.And if it is displayed as “October 27, 2023, 10:30 AM” or “10/27/2023”, it is clear at a glance.

Its use is very intuitive:{{stampToDate(时间戳, "格式")}}. Here, you need to provide two parameters:

  1. Timestamp:It is usually in AnQiCMSCreatedTime/UpdatedTimeThis field returns the number.
  2. Format:This part is the template defining how you want the date and time to be displayed.

Understand the formatting rules: Special "magic numbers" in Go language

Of Security CMSstampToDateLabels follow the unique time formatting rules of Go language. Unlike what we might be familiar withYYYY-MM-DD/HH:MM:SSDifferent placeholder, Go language uses a fixed reference time——2006年01月02日 15时04分05秒(also known as "magic number" or "reference time") as a template.

The key to understanding this reference time is that each number or part represents a specific component of time:

  • 2006Represents the year (Year)
  • 01Representing the month (Month), with a leading zero
  • 02Representing the date (Day), with a leading zero
  • 15Representing the hour (Hour), 24-hour format, with a leading zero
  • 04represent minutes (Minute), with leading zero
  • 05represent seconds (Second), with leading zero

If you want to display other formats, just use the corresponding parts of these 'magic numbers' to construct your format string. For example, if you want to display 'year-month-date', the format string is"2006-01-02".

Common Formatting Examples

Below are some common formatting methods, as well as their corresponding format strings and display effects:

  • Only show the date (Year-Month-Day): {{stampToDate(item.CreatedTime, "2006-01-02")}}For example:2023-10-27

  • Display the full date and time: {{stampToDate(item.UpdatedTime, "2006-01-02 15:04:05")}}For example:2023-10-27 10:30:00

  • Chinese date and time format: {{stampToDate(archive.CreatedTime, "2006年01月02日 15时04分")}}For example:2023年10月27日 10时30分

  • Custom delimiter date: {{stampToDate(item.CreatedTime, "2006/01/02")}}For example:2023/10/27

  • Show hours and minutes only: {{stampToDate(item.CreatedTime, "15:04")}}For example:10:30

  • Show weekday (in English): {{stampToDate(item.CreatedTime, "Mon Jan 02 2006")}}For example:Fri Oct 27 2023(here is theMonRepresents the week,JanRepresents the English abbreviation for the month,02Is the date)

Actual application in template

In the Anqi CMS template, we usually loop through the document list (for examplearchiveList) or display document details (for examplearchiveDetailUsed whenstampToDate.

For example, on an article list page, you might display the publication time of each article like this:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <div class="article-card">
        <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
        <p class="meta-info">
            发布于:{{ stampToDate(item.CreatedTime, "2006年01月02日 15:04") }}
            浏览量:{{item.Views}}
        </p>
        <p>{{item.Description}}</p>
    </div>
    {% endfor %}
{% endarchiveList %}

And on an article detail page, the update time can be displayed like this:

<article>
    <h1>{% archiveDetail with name="Title" %}</h1>
    <div class="article-meta">
        发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04:05") }}
        更新时间:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05") }}
        浏览量:{% archiveDetail with name="Views" %}
    </div>
    <div class="article-content">
        {% archiveDetail articleContent with name="Content" %}{{articleContent|safe}}
    </div>
</article>

By reasonable applicationstampToDateTags and formatting rules ensure that time information on the website is always presented in a clear, professional, and user-friendly manner, thereby enhancing the overall user experience.


Frequently Asked Questions (FAQ)

  1. Why can't I useYYYY-MM-DDthis format placeholder?Of Security CMSstampToDateThe tag uses the time formatting standard of the Go language, it does not recognizeY/M/DUse traditional placeholders. On the contrary, you need to use a fixed reference time (2006年01月02日 15时04分05秒) to construct your format string. For example, if you want to display a four-digit year, use2006; Use to display two-digit months,01and so on.

Related articles

How to implement conditional display and loop display of dynamic content in AnQiCMS template using `if` and `for` tags?

In the Anqi CMS template world, `if` and `for` tags are the foundation for building dynamic and flexible pages.They give the template the ability to display content based on different data conditions, as well as the ability to efficiently loop through data sets, making your website content生动地呈现给visitors vividly.AnQi CMS adopts a template engine syntax similar to Django, making it easy for you, who is familiar with web development, to get started quickly, obtaining data through double curly braces `{{variable}}`, and using `{% tags %}` syntax for conditional judgment and loop control.### Use `if`

2025-11-08

How to generate and control the pagination display effect of the `pagination` tag?

In website operation, when the amount of content is large, how to efficiently and beautifully display these contents while ensuring that users can browse conveniently is the key to improving user experience.If you dump all the content on the same page, not only will it load slowly, but it will also deter visitors.At this moment, content pagination becomes particularly important. It breaks massive information into several pieces, which not only reduces the burden of page loading but also allows users to explore the content they are interested in step by step at their own pace.AnQiCMS provides a powerful and flexible `pagination` tag

2025-11-08

How to implement content aggregation and thematic display based on `tagList` and `tagDetail` tags?

In content management and website operations, how to efficiently organize and display content is the key to improving user experience and SEO performance.AnQiCMS provides powerful tag features, with the `tagList` and `tagDetail` template tags, it is easy to implement content aggregation based on tags and thematic display, bringing more flexible classification and richer navigation experience to the website.### The role of tags in content aggregation Tags are an important way to organize content in a content management system

2025-11-08

How to manage and display independent single-page content for the `pageList` and `pageDetail` tags?

In website operation, in addition to dynamically updated articles or product lists, we often need to display some relatively fixed and independent content pages, such as "About Us", "Contact Information", "Privacy Policy" or "Service Introduction" and so on.These pages are usually called "single-page content". AnQiCMS provides us with powerful and flexible tools for managing and displaying these pages, especially the core tags `pageList` and `pageDetail`.### Single Page Content: The Foundation of a Website Single page content is an indispensable part of a website

2025-11-08

How to retrieve and display the global configuration and SEO information of the website using `system`, `contact`, and `tdk` tags?

In the daily operation of AnQiCMS (AnQiCMS), accurately displaying global configuration information, contact information, and carefully optimized SEO elements on the website front end is the key to improving user experience and search engine visibility.Fortunately, AnQiCMS provides several intuitive and powerful template tags that allow users to easily access this important data.This article will delve into the three core tags `system`, `contact`, and `tdk`, helping you seamlessly integrate the background configuration into various pages of the website.Get the global configuration of the website

2025-11-08

How to build and display the navigation path of the current page in the website using the `breadcrumb` tag?

In website construction and operation, user experience (UX) is always at the core.When visitors delve deeper into your website, a clear navigation path can effectively prevent them from getting lost and quickly understand their current location.This is the value of the 'Breadcrumb Navigation'.It not only enhances user experience, but is also an important part of Search Engine Optimization (SEO), helping search engines understand the hierarchy of your website structure.AnQi CMS knows this and therefore provides a convenient and easy-to-use `breadcrumb` tag

2025-11-08

How does AnQiCMS support Markdown content editing and correctly render mathematical formulas and flowcharts on the front end?

AnQiCMS understands the desire of content creators for efficient and flexible editing tools.The new version introduces Markdown editor, greatly enhancing the convenience of content publishing.It is even more surprising that AnQiCMS not only supports basic Markdown syntax but also allows complex mathematical formulas and flowcharts to be elegantly presented on your website front-end through simple configuration.This is undoubtedly a powerful boost for websites that need to display professional knowledge, technical documentation, or educational content.### Enable Markdown Editing Feature First

2025-11-08

In AnQiCMS content settings, how to finely control the display of images (such as WebP, automatic compression, thumbnails)?

In website operation, images are important elements to attract users and convey information.However, unoptimized images may also be the culprit behind slow website loading, which can then affect user experience and search engine rankings.AnQi CMS knows the importance of image optimization and therefore provides a variety of fine-grained image control features to help you achieve a perfect balance between website performance and visual effects.### One, WebP: The intelligent choice for lightweight and fast image acceleration WebP, as a modern image format, typically has a file size 25-34% smaller than JPEG at the same visual quality

2025-11-08