How to format a `time.Time` object into a specified date string in AnQiCMS template?

Calendar 👁️ 67

In website content operation, the way dates and times are presented often directly affects user experience and the clarity of information.A professional and readable date format not only enhances the credibility of the content but also allows visitors to quickly obtain key information.AnQiCMS as a powerful content management system naturally also provides a flexible way to handle and format date and time.

When we need to display the article publish time, update time, or any other date information in the AnQiCMS template, we may encounter a problem: how to convert this original data (usually timestamps) into the desired年-月-日/时:分:秒Or a more customized format? This article will delve into the handling of AnQiCMS templatestime.TimeA strategy and method for formatting a type (or more common timestamp) object into a specified date string.

Understanding date data in AnQiCMS.

AnQiCMS stores the article publishing time (such asarchive.CreatedTime)、Update Time(archive.UpdatedTimeWhen dealing with such date data, the Unix timestamp format is usually used.This represents the number of seconds elapsed since the Unix epoch (January 1, 1970, 00:00:00 UTC).Therefore, directly displaying these timestamps in the template often results in a string of digits that is difficult to understand, and we need to format them into human-readable date strings.

Core Tool:stampToDateTag

AnQiCMS provides a very practical template tag for this:stampToDate. This tag is specifically used to convert timestamps into date strings in a specified format.

The basic syntax of its use is:

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

The "timestamp variable" here is usually a timestamp field available in the template, such asitem.CreatedTime. And the format string is the key to defining the style in which you want the date to be displayed.

Understand the formatting strings in Go language.

This isstampToDateThe label is the most unique and the most important to understand. It is different from what we are accustomed to.YYYY-MM-DDThe placeholder is different, the Go language (the development language of AnQiCMS) uses a fixed reference date to define the date format:

2006年1月2日 15点04分05秒

You need to use each part of this date to represent the format you want. For example:

  • 2006Represents the year
  • 01Represents the month (with leading zero)
  • 02Represents the day (with leading zero)
  • 15Represents the hour in 24-hour format
  • 04representing minutes
  • 05representing seconds

If you want to display the day of the week, you can also refer to the more complex reference date section provided in the official Go language documentation, such asMonrepresenting the abbreviation Monday,Mondayrepresenting the full week, etc.

How to usestampToDateFormat the date

Let's see a few actual examplesstampToDateHow tags work. Suppose you are dealing with a list of articles and need to display the publication date and time next to each article.

  1. Only show the year, month, and day (for example:2023-10-26)

    This is the most common way to display dates.

    <span>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>
    

    Whenitem.CreatedTimehas a value of1698292800At the time, the output will be2023-10-26.

  2. Display the full date and time (for example:')2023-10-26 14:30:00)

    If you need to format the full time down to the second

    <span>发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02 15:04:05") }}</span>
    

    The output will be2023-10-26 14:30:00(Assuming the timestamp corresponds to this local time).

  3. Custom format (for example:10月26日, 星期四)

    Go's formatting capabilities are very powerful and can combine various styles:

    <span>文章日期:{{ stampToDate(item.CreatedTime, "01月02日, 星期一") }}</span>
    

    Here星期一Is the reference date in Go languageMonThe localized format, which will be converted to the correct day of the week based on the timestamp. For example, the output might be10月26日, 星期四.

  4. InarchiveDetailused directly in tags

    InarchiveDetailretrieved from tagsCreatedTimeorUpdatedTimewhen, you can also directly use itformatproperty to format, which makes the code more concise.

    {# 显示文章发布日期 #}
    <div>发布日期:{% archiveDetail with name="CreatedTime" format="2006年01月02日" %}</div>
    {# 显示文章更新日期和时间 #}
    <div>更新时间:{% archiveDetail with name="UpdatedTime" format="2006-01-02 15:04" %}</div>
    

other date and time processing scenarios

  1. Display the current date and time:{% now %}Tag

    If you want to display the current date and time in the template instead of the article's publication time, you can use{% now %}tags. Its format string rules are withstampToDateSame.

    <span>当前日期:{% now "2006-01-02" %}</span>
    <span>当前完整时间:{% now "2006-01-02 15:04:05" %}</span>
    
  2. aboutdateFilter

    AnQiCMS template still contains a filter nameddate(for example:{{ someGoTimeObject|date:"2006-01-02" }}). It is usually used to process Go language primitivestime.TimeAn object of type, rather than the common Unix timestamp. In common applications of AnQiCMS templates (such asarchive.CreatedTimeThese fields are directly exposed as timestamps, so please use them firststampToDateTag or within the tagformatProperty to ensure correctness and simplicity

Tips and **Practice

  • Keep in mind the reference date of Go language: 2006-01-02 15:04:05Is the foundation for mastering all date formatting. By memorizing this date, you can flexibly combine any format you want.
  • Maintain uniform formatting:In your website, try to maintain a uniform date and time format, which helps improve user experience and the professionalism of the website.
  • Test different formats:Before applying in actual use, be sure to test your formatted strings in the development environment to ensure they display as expected.
  • Utilize the flexibility of AnQiCMS:CombinearchiveList/archiveDetailTags such as this allow you to easily display formatted date information in article lists, detail pages, sidebars, and any other location.

Summary

BystampToDateThe tag and its formatting rules based on the Go language reference date, AnQiCMS makes the presentation of dates and times in templates intuitive and powerful.Whether it is a concise date, a detailed time to the second, or a custom format with the day of the week, AnQiCMS can help you easily achieve it, thereby adding professionalism and readability to your website content.


Frequently Asked Questions (FAQ)

**Q1: Why do I use `YYYY

Related articles

How to remove specific characters from a string in the AnQiCMS template, such as spaces or special symbols?

In website content operation, we often encounter scenarios where we need to process string data.For example, the title, description, or keywords extracted from the database may contain extra spaces, unnecessary special characters, or even inconsistent prefixes or suffixes.These characters may not only affect the aesthetic beauty of the layout, but may also cause interference when performing data analysis or search engine optimization (SEO).AnQiCMS as an efficient content management system, deeply understands the needs of users in content processing.

2025-11-08

In AnQiCMS template, can the `count` filter count the number of occurrences of elements in an array?

In AnQiCMS template development, we often need to handle various data, one common requirement is to count the number of times a specific element or keyword appears.When faced with array or string data, the `count` filter can become your powerful assistant.So, can the `count` filter in the AnQiCMS template count the number of occurrences of elements in an array?The answer is affirmative, it can not only count the word frequency in a string, but also accurately count the number of occurrences of elements in an array.

2025-11-08

How to count the frequency of a keyword in a string in AnQiCMS template?

In AnQi CMS template development, we often need to handle and analyze various data of page content.Among them, counting the number of times a specific keyword appears in a string is a very practical need.This not only helps content operators understand the density of keywords, thereby optimizing SEO, but also provides data support for certain feature displays, such as showing how many times a certain feature is mentioned.The Anqi CMS template engine provides a rich set of filter (Filters) functions, which act as mini data processing tools, allowing variables to be formatted, converted, or calculated

2025-11-08

Does the `contain` filter support checking array or Map type data in the AnQiCMS template?

During the development of AnQi CMS templates, we often need to decide how to display the page based on the specific content of the data, or determine whether a certain specific element exists in the data we pass in.When dealing with complex arrays or key-value pairs (Map), it is particularly important to make efficient and accurate judgments of this kind.Here, the `contain` filter becomes a very practical tool.### `contain` filter: Flexibly judge whether data contains specified content `contain`

2025-11-08

Which date and time formatting parameters are supported by the `date` filter in AnQiCMS?

In website content management, the accurate display of dates and times is crucial for improving user experience and ensuring the timeliness of information.AnQiCMS provides a flexible and powerful template engine, allowing you to easily customize the display of dates and times on web pages.Among many practical tools, the `date` filter is an important member for handling date and time formatting.### `date` filter: A powerful tool for formatting date and time The `date` filter in AnQiCMS templates is used to format `time.Time`

2025-11-08

How to set a default display value for possibly empty variables in AnQiCMS templates?

When using AnQiCMS to build a website, we often encounter such situations: some content fields may not have values every time, such as articles may not have thumbnails, products may not have detailed descriptions, or some contact information may not have been filled in.If variables that may be empty are directly called in the template, ugly blank spaces may appear on the page, or even program errors, which will greatly affect the user experience and the professionalism of the website.Luckyly, the AnQiCMS template system is based on syntax similar to Django and Blade, providing a powerful and flexible mechanism to handle these situations

2025-11-08

What is the difference between the `default` and `default_if_none` filters for handling null values in AnQiCMS?

In AnQiCMS template development, we often need to handle the case where data may be empty to ensure the stability of page display and user experience.AnQiCMS provides rich template filters to meet such needs, where `default` and `default_if_none` are very practical tools for handling null values.They are both intended to provide a fallback value for missing or empty data, but there is a subtle yet critical difference in the definition of 'empty,' understanding these differences can help us more precisely control the template rendering logic.###

2025-11-08

How to check if a number is divisible by another number in AnQiCMS template?

During the development of AnQiCMS templates, we often encounter situations where we need to adjust the display of content or apply different styles based on specific conditions.For example, when a number in the list meets a certain rule, such as being divisible by 3, we would like it to have a special performance.Lucky enough, AnQiCMS's powerful template engine provides a simple and efficient way to meet this requirement.This article will deeply explore how to flexibly judge whether a number can be evenly divided by another number in the AnQiCMS template, and provide practical code examples in real-world scenarios.### Core Function

2025-11-08