How to use the `stampToDate` tag to format timestamps in AnQiCMS templates?

Calendar 👁️ 61

As an experienced website operator deeply familiar with AnQi CMS, I know the importance of content timeliness and presentation for user experience.In website content management, a timestamp (Timestamp) is a common method for recording the time of content publication and updates, but directly displaying a timestamp is not intuitive for users.Therefore, formatting timestamps into a readable date and time format is a key aspect in enhancing the professionalism and user-friendliness of a website.AnQiCMS provides a very convenient template tagstampToDateMake this operation easy.

UnderstandingstampToDateTags and their uses

In the AnQiCMS template system,stampToDateThe tag is a powerful auxiliary tool, specifically used to convert Unix timestamps into human-readable date and time strings. Whether you want to display the publication date of an article, the update time of a product, or any other data based on timestamps,stampToDateCan help you present this information in a clear and unified format.The existence of this tag greatly simplifies the work of front-end developers handling time data, eliminating the need for complex programming logic, and can be achieved with simple template syntax.

stampToDateThe syntax and parameter parsing of the tag

stampToDateThe calling syntax of the tag is very intuitive:{{stampToDate(时间戳, "格式")}}. Let's analyze the two core parts of this syntax in detail.

The first parameter isTimestamp (Timestamp). It is usually a 10-digit Unix timestamp representing the number of seconds from the Unix epoch (January 1, 1970, 00:00:00 UTC) to a specific time.In AnQiCMS content tags (such asarchiveDetailorarchiveList) obtained.CreatedTimeorUpdatedTimefields, their original value is such a timestamp. You need to pass these field values directly asstampToDateas the first parameter.

the second parameter isFormat StringThis is the key to determining how the date and time will be displayed.AnQiCMS's template engine is based on the Go language, therefore, the time formatting string here follows the specific reference time format of the Go language.Go language uses a fixed reference time2006-01-02 15:04:05.999999999 -0700 MSTIt serves as the template for all date and time formatting. This means you cannot use arbitrarilyYYYY-MM-DDSuch a general placeholder, but instead you need to use the corresponding numbers or words in this reference time to represent the year, month, day, hour, minute, and second you want.

Here are some commonly used formatting string examples and their corresponding output effects to help you quickly understand and apply them:

  • "2006-01-02": Will output年-月-日For example2021-06-30
  • "2006年01月02日": Will output年年年年年年年年For example2021年06月30日
  • "15:04:05": Will output时:分:秒For example12:30:00
  • "2006-01-02 15:04": Will output年-月-日 时:分For example2021-06-30 12:30
  • "Mon Jan _2 15:04:05 2006": Complete reference time format in Go language, for exampleWed Jun 30 12:30:00 2021

Pay close attention to the accuracy of the format string, even if it is01and1Such subtle differences can affect the final display results. For example,01Represents a two-digit month (leading zeros if less than ten). For example,1It represents a month represented by one or two digits (without leading zeros).

stampToDateActual application cases

In the template development of AnQiCMS,stampToDateTags are usually tags related to content (such asarchiveDetailTo get the details of a single document,archiveListRetrieve document list) combined to format the publication time or update time of these content items.

Assuming you are building an article detail page, you need to display the article's publish date. You can usearchiveDetailtags to retrieve the article data, and thenCreatedTimethe field passed instampToDate:

{# 假设当前页面是文章详情页,archiveDetail 默认获取当前文档 #}
<div>
    <span>文章标题:{% archiveDetail with name="Title" %}</span>
    <span>发布日期:{{stampToDate(archive.CreatedTime, "2006年01月02日")}}</span>
    <span>精确到秒:{{stampToDate(archive.CreatedTime, "2006-01-02 15:04:05")}}</span>
</div>

{# 如果您想获取指定ID文章的更新时间,也可以这样做: #}
{% archiveDetail specifiedArchive with id="123" %}
    <span>文章ID 123的更新时间:{{stampToDate(specifiedArchive.UpdatedTime, "2006/01/02 15:04")}}</span>
{% endarchiveDetail %}

On the article list page, you may need to iterate over multiple articles and display their publication time:

{% archiveList archives with type="list" limit="5" %}
    {% for item in archives %}
        <div>
            <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
            <p>发布于:{{stampToDate(item.CreatedTime, "2006-01-02")}}</p>
        </div>
    {% endfor %}
{% endarchiveList %}

By these examples, you can seestampToDateHow to flexibly format timestamps in different scenarios to meet various content display requirements.

stampToDateThe importance and practice

Properly formatting time information is crucial for enhancing the user experience of a website.Clear dates and times help users quickly understand the publication and update of content, enhancing the reliability of information.At the same time, a unified time format also makes the website look more professional and standardized.

In practice, it is recommended that you:

  • Maintain consistencyAvoid using the same format for similar time information throughout the website to prevent user confusion.
  • Consider the user's region.If your website is aimed at global users, you may need to consider using different date formats (such as American date formatMM/DD/YYYYor European date formatDD/MM/YYYY),AnQiCMS's multilingual support and custom template capabilities can help you achieve this.
  • Avoid redundant informationIf you only need the date, don't show the time, and vice versa. Concise and clear is the best.

Summary

stampToDateThe label is a practical and efficient tool in the AnQiCMS template system, making timestamp formatting simple and powerful.By precisely controlling the format string, you can convert the original timestamp data into dates and times that meet various display requirements, thereby optimizing the presentation of website content and enhancing the reading experience of users.Mastering the use of this tag will bring greater flexibility and convenience to the operation of your AnQiCMS website.


Frequently Asked Questions (FAQ)

1. WhystampToDateThe format string should be used2006-01-02 15:04:05Instead of such a number combination,YYYY-MM-DDsuch general placeholders?

This is because the template engine of AnQiCMS is based on Go language. When designing its date and time formatting function, Go language chose a fixed reference time2006-01-02 15:04:05.999999999 -0700 MSTAs a template. The format string you provide actually tells the Go language how to parse or rewrite the parts of the reference time to generate the date and time format you expect.For example, when you write2006When, it indicates the extraction of the year; write01When, it indicates extracting the month (with leading zeros). This design may seem special at first glance, but once you understand its logic, you can very flexibly customize various time formats.

2. The timestamp I received is not 10 digits, but 13 digits (millisecond level),stampToDateCan the tag still be used?

stampToDateThe tag is designed to handle 10-digit Unix second-level timestamps.If your timestamp is a 13-digit millisecond timestamp, you need to process it before passing the label, converting it to a second timestamp.In some template environments, you may need to use mathematical operations to divide it by 1000.For example, ifitem.MilliCreatedTimeIt is a millisecond timestamp, you may need{{stampToDate(item.MilliCreatedTime / 1000, "2006-01-02")}}Please confirm the source of your timestamp and its accuracy, and make the corresponding adjustments.

3. How tostampToDateWhich day of the week is displayed (e.g., "Monday", "Monday")?

To display what day of the week it is, you need to use the corresponding part of the time representation in the Go language.

  • If you want to display the full name of the week (such asMonday), you can useMondayAs part of the format string, for example:{{stampToDate(item.CreatedTime, "Monday, 2006-01-02")}}.
  • If you want to display abbreviated weekday names such asMon), you can useMonFor example:{{stampToDate(item.CreatedTime, "Mon, 2006-01-02")}}. To display "Monday" and so on in Chinese, you need to combine the AnQiCMS multi-language translation function or use it in the template.ifThe statement outputs the Chinese equivalent after conditional judgment of the English week.

Related articles

How to set the page title, keywords, description, and attach the website name using the `tdk` tag in AnQiCMS?

As a senior security CMS website operator, I am well aware of the core position of content in website construction and SEO optimization.TDK (Title, Description, Keywords) tags are an important basis for search engines to understand page content and judge the relevance of the page, as well as the information that users see first on the search results page.Efficiently setting and optimizing TDK is crucial for attracting target users and improving website rankings.Our Anqi CMS provides us with flexible and powerful TDK management functions, allowing us to finely control the metadata of each page

2025-11-06

How to use the `system` tag to obtain website name, LOGO, filing number and other system information?

As an experienced CMS website operation personnel, I fully understand the importance of dynamically obtaining basic website information in daily template development and content presentation.To ensure the consistency, ease of maintenance, and efficient management of the website, Anqi CMS provides an extremely convenient `system` tag.This tag allows template developers to directly call various system-level data from the global settings in the background, thereby avoiding the inconvenience brought by hard coding.The website's name, logo, filing number, and other information are key elements in building a brand image and legal compliance.Pass through the `system` tag

2025-11-06

How to get all Tag lists or Tags of a specified document in AnQiCMS template?

As an experienced CMS website operations personnel in the cybersecurity field, I deeply understand the importance of content tags (Tags) in website content organization, SEO optimization, and user experience.They can not only help readers quickly find the content they are interested in, but also enhance the internal links of the website and improve the efficiency of search engine crawling.In the Anqi CMS template system, the `tagList` tag is the core tool we use to achieve this goal.This article will discuss in detail how to flexibly use the `tagList` tag in Anqi CMS template to obtain all tags on the website

2025-11-06

How to implement document parameter filtering in AnQiCMS templates, for example, filtering by house type?

As an experienced CMS website operator for a security company, I know how important it is for users to efficiently find the content they need when visiting a website with a large amount of information.The content filtering function can not only greatly improve user experience, but also is a key factor in optimizing website structure and SEO performance.Today, I will elaborate on how to implement document parameter filtering in AnQiCMS templates, taking 'filtering by house type' as an example, to help your website better serve users.

2025-11-06

How to get the contact information configured in the `contact` tag of AnQiCMS template?

As an experienced website operator for Anqi CMS, I am well aware of the core position of content in website construction and user interaction.Effectively and conveniently displaying contact information is an indispensable part of a website's functionality, directly affecting the efficiency and willingness of users to communicate with the company.In AnQiCMS, we provide a simple and powerful template tag——`contact`, allowing you to easily display the contact information configured in the background on the website front-end.--- ###

2025-11-06

How to implement pagination display of document list in AnQiCMS template using `pagination` tag?

As a senior Anqi CMS website operator, we fully understand that it is crucial to efficiently display a large amount of content in a content management system and ensure a user-friendly browsing experience.For a document list, pagination is the core function to achieve this goal.In the AnQiCMS template system, the `pagination` tag plays a key role in connecting the document list with page navigation, allowing a large number of documents to be presented to the user in a structured manner while maintaining flexibility and customizability.

2025-11-06

What is the difference between the `safe` and `escape` template filters in AnQiCMS and when should they be used?

As an experienced website operator for AnQiCMS, I am well aware that the correct use of templates is crucial for the safety and display effect of the website during content creation and publishing.AnQiCMS's powerful template engine provides rich features, among which the `safe` and `escape` filters play a key role in handling HTML special characters during content output.Understanding the differences and proper application is the foundation for ensuring website content security and enhancing user experience.###

2025-11-06

Which operating systems does AnQiCMS support for installation?

AnQiCMS is an enterprise-level content management system developed based on the Go language, dedicated to providing efficient, customizable, and easily scalable content management solutions for small and medium-sized enterprises, self-media operators, and those who need to manage multiple sites.Its lightweight architecture and excellent performance make AnQiCMS highly compatible in deployment and operation.As an experienced website operator, I am well aware of the importance of choosing a system with strong compatibility to ensure the stable operation of the website.Next, we will discuss AnQiCMS in detail

2025-11-06