How to format a timestamp into a readable date and time using the `stampToDate` tag in AnQi CMS?

Calendar 👁️ 56

In website operations, the display of time information is crucial. Whether it is the publication date of articles, the update time of products, or the submission time of comments, a clear and easy-to-read date and time format can greatly improve user experience.The original timestamp is friendly to machines but cryptic to ordinary visitors.Fortunately, AnQiCMS (AnQiCMS) provides a very convenient template tag -stampToDateCan easily format timestamps into the familiar date and time format.

stampToDateLabel: Let timestamps speak for themselves.

stampToDateThe label is a practical feature built into Anqi CMS template engine, whose core function is to convert a string of numbers (usually a 10-digit Unix timestamp) into a specified date and time string format. Through it, the originally boring timestamp becomes intuitive and easy to understand, for example, to convert1609470335This number becomes "2021-01-01 12:25:35".

stampToDateBasic usage

UsestampToDateThe tag is very simple, its basic syntax is:

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

This includes two main parameters:

  1. timestamp: The number you want to convert, usually a 10-digit Unix timestamp. This can be the article publish time you obtained from the database (such asitem.CreatedTime)or any other timestamp variable.
  2. FormatThis is a string used to define the display style of date and time.Aqii CMS is developed based on Go language, so its time formatting follows the unique rules of the Go language, which we will explain in detail later.

Understand the time formatting rules of Go language

The time formatting rules in Go language are very special, it does not use commonY-m-d H:i:sSuch placeholders are instead used, a fixed 'reference time' is used as a template. This reference time is:

2006-01-02 15:04:05

You just need to remember this time and replace the corresponding number or character with the format you want to display according to the date and time part you want to show.

  • 2006Represents the year
  • 01Represents the month (January)
  • 02representing the date (2nd)
  • 15representing the hour (3 PM, 24-hour format)
  • 04representing minutes
  • 05representing seconds

By combining these numbers and adding separators, we can create various time display formats. For example:

  • If you only want to display the year, just write2006.
  • If you want to display the month and date, write01-02.
  • If you want to display the full time, write15:04:05.
  • If you want to add Chinese characters to the date, such as “year”, “month”, “day”, simply insert them into the corresponding number positions.

Actual application example

Now, let's look at some examples of how to usestampToDateactual examples of tags. Suppose we have a variablepublishStampIts value is1609470335.

  1. that only displays dates in the format year-month-day:

    发布日期:{{stampToDate(publishStamp, "2006-01-02")}}
    {# 显示结果:发布日期:2021-01-01 #}
    
  2. Display the date in Chinese format (Year-Month-Date):

    发布日期:{{stampToDate(publishStamp, "2006年01月02日")}}
    {# 显示结果:发布日期:2021年01月01日 #}
    
  3. Display the format of Year/Month/Date and Time:

    更新时间:{{stampToDate(publishStamp, "2006/01/02 15:04")}}
    {# 显示结果:更新时间:2021/01/01 12:25 #}
    
  4. Display the full date and time (Year-Month-Date Time:Minute:Second):

    创建时间:{{stampToDate(publishStamp, "2006-01-02 15:04:05")}}
    {# 显示结果:创建时间:2021-01-01 12:25:35 #}
    
  5. Use in document list (for example, article publish time):InarchiveListorarchiveDetailYou often encounter in tag loopsitem.CreatedTimeoritem.UpdatedTimeSuch variables, which are usually timestamps."),

    {% archiveList archives with type="list" limit="10" %}
        {% 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 %}
    

Practical tips

  • Flexible combination: You can choose to use, according to your needs, the2006/01/02/15/04/05Combine with other characters (such as slashes/、dots.、spaces, etc.) to create various formats.
  • Preview and debugAfter modifying the template, be sure to preview in the background or directly access the page to ensure that the date and time display meets expectations. If the format is incorrect, it is usually due to an error in the numeric combination of the reference time.
  • Keep consistentOn your website, try to maintain consistency in the date and time format, which helps improve the overall professionalism and user experience.

BystampToDateThe label, Anqi CMS has given website operators powerful time formatting capabilities, making the presentation of website content more refined and humanized.Mastery of this tag will bring great convenience to your content operation.


Frequently Asked Questions (FAQ)

Q1:stampToDateTags anddateWhat are the differences between filters?A1:stampToDateTags are used to format 10-digit Unix timestamps into date-time strings.dateFilters are usually used to process in Go language.time.TimeA type of time object. In AnQi CMS templates, if your data source directly provides timestamps, it is recommended to usestampToDate; If it has been processed by the backend and converted to Go'stime.TimeAn object can be useddateA filter (althoughstampToDateIt is usually more common and direct).

Q2: If the timestamp I provide is empty or inaccurate,stampToDatewhat will the tag display?A2: If the inputstampToDateThe timestamp variable of the tag is empty (for examplenilor0It usually displays an empty default string or an invalid or default date (such as '0001-01-01') depending on the internal processing logic of the Anqi CMS. To avoid this, you can usestampToDateBefore, using{% if item.CreatedTime %}such conditional judgment to ensure that the timestamp is valid.

Q3: Can I customize the separators and text in the date? For example, I want to display the format 'February 9, 2023'.A3

Related articles

How to implement diverse data layout and display with the `for` loop tag in AnQi CMS?

How to present information in a more rich and attractive way during website content operation is the key to improving user experience and site activity level.AnQiCMS (AnQiCMS) provides us with the tool to achieve this goal with its flexible content model and powerful template engine.Among them, the `for` loop iteration tag is the essential core tool for us to perform diverse formatting and display.Imagine your website is not just a static pile of articles, but can intelligently present lists of articles, product galleries, category navigation, user comments, and even custom parameters based on different scenarios

2025-11-09

What are some practical scenarios for using the `if/else` logical judgment tags in AnQi CMS to control content display conditions?

In AnQi CMS template design, the `if/else` logical judgment tag is an extremely powerful tool that endows the website content with the vitality of dynamic display.By flexibly using this tag, we can intelligently control the display and hiding of elements on the page based on different conditions, thereby providing users with more accurate and personalized browsing experiences, and also greatly improving the operational efficiency of the website.Imagine if your website needs to display different content for different scenarios or if certain information is only visible under specific conditions

2025-11-09

How to use the `pagination` tag to generate a page navigation that conforms to user habits for articles or product lists?

In website operation, how to allow users to easily browse a large amount of content and find the information they are interested in is a crucial topic.Especially on pages such as article lists and product displays, reasonable pagination navigation not only improves user experience but is also an indispensable part of search engine optimization (SEO).AnQiCMS (AnQiCMS) understands this point and therefore provides a powerful and flexible `pagination` tag to help us easily generate professional and user-friendly pagination navigation for website content.We will delve deeper into how to utilize Anqi CMS

2025-11-09

How to display the friend link list in the website background configuration of Anqi CMS?

In website operation, friendship links are an important link to enhance website weight, increase traffic, and improve user experience.AnQiCMS (AnQiCMS) provides us with a convenient way to manage and display these links.How can the list of friend links configured on the back-end be displayed on the website front-end?This is actually simpler than imagined, mainly involving two steps: backend configuration and frontend template invocation. ### Step 1: Configure the友情链接 in the background Firstly, we need to add and manage the friendship links in the Anq CMS background management system.According to system design

2025-11-09

How to use the `system` tag to retrieve and display the global information of a website, such as the website name, Logo, and filing number?

It is crucial to maintain the consistency of brand image and the accuracy of information in website operation.AnQi CMS is an efficient content management system that provides us with a convenient way to manage and display these core information.Today, let's delve into how to use the `system` tag of Anqi CMS to easily obtain and display global information on the website template, such as the website name, Logo, and filing number.

2025-11-09

How to display the contact information set in the website backend on the front page of AnQi CMS?

When using Anq CMS to build a website, displaying the contact information set in the background to the website front end is a key step to enhance user experience and establish trust.AnQi CMS provides a very intuitive and flexible way, even without a strong technical background, it can be easily realized. ### Configure the contact information of the website backend Firstly, you need to configure the contact information in the Anqi CMS backend management interface. Generally, you can find the 'Background Settings' in the left navigation bar, clicking on it will show the 'Contact Information Settings' option.

2025-11-09

How to dynamically set the `title`, `keywords`, and `description` metadata of a page using the `tdk` tag?

In the practice of website operation and search engine optimization (SEO), page metadata plays a crucial role.Among them, the page title (`title`), keywords (`keywords`), and description (`description`) directly affect the display effect and the willingness of users to click on the search engine results page.AnQiCMS (AnQiCMS) fully understands its importance and provides a powerful and flexible `tdk` tag to help us dynamically and efficiently manage these key metadata.

2025-11-09

How does Anqi CMS ensure that static rules (such as model naming patterns) are correctly generated and displayed in URLs?

In website operation, a clear and friendly URL address not only allows visitors to understand the page content at a glance but is also an indispensable part of search engine optimization (SEO).A well-structured URL can help search engines better understand the page topic, thereby improving the crawling efficiency and ranking of the website.Our CMS deeply understands this, providing a simple, easy-to-use, and highly flexible solution for generating and displaying static rules.

2025-11-09