How to format a timestamp into the display format of "Year-Month-Day" in AnQiCMS?

Calendar 81

In the daily operation of websites, the display format of dates is crucial for user experience.A clear, consistent and easy-to-understand date format that allows visitors to easily obtain information and enhance the professionalism of the website.For users of AnQiCMS, we often encounter data stored in the form of timestamps for content publishing time, update time, etc., and how to convert these long strings of numbers into a friendly display format such as 'Year-Month-Date' is an indispensable part of content presentation.

AnQi CMS is an efficient content management system that fully considers the flexibility of templates and the display needs of content. Many of the internal data related to time in the system, such as the creation time of articles (CreatedTimeAnd update time(UpdatedTime), the default storage is all timestamps.These timestamps are precise, but they are undoubtedly not intuitive to display directly to users.Fortunately, AnQiCMS provides a very convenient template tag that can easily format these timestamps into any date string we need.

The core secret:stampToDateThe use of tags

To format the timestamp in AnQiCMS, we need to use a name calledstampToDateThe template tag. This tag is specifically used to handle timestamps and output dates in the specified format. Its usage is very intuitive:

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

The "timestamp variable" is usually the timestamp field we get in loops or detail pages, for exampleitem.CreatedTimeorarchive.UpdatedTime. The "formatted string" is the key to defining the date output style.

The template engine of AnQi CMS adopts the formatting rules of Go language, which is very important.Different from the common formats such as 'Y-m-d' or 'yyyy-MM-dd', Go language uses a specific reference time '2006-01-02 15:04:05' to represent year, month, day, hour, minute, and second."2006-01-02".

For example, if a variablepublishStampstores a timestamp1609470335(corresponding to January 1, 2021), we can format it as “2021-01-01”:

<div>{{ stampToDate(publishStamp, "2006-01-02") }}</div>

Or, if you want to display it in a format with Chinese characters like "2021-01-01", you can write it like this:

<div>{{ stampToDate(publishStamp, "2006年01月02日") }}</div>

Actual operation: apply the timestamp to the content display

In the presentation of actual website content, timestamp formatting is mainly applied to the creation or update time of articles, products, and other content.

In the article or product detail page:

When we are displaying a single article or product details, we usually go througharchiveDetailtags to get specific content. This tag will return a list that includesCreatedTimeandUpdatedTimeObjects of the fields. We can format these fields directly.

Suppose we are usingarchiveDetailTag to get the details of the current document and assign it to a variable namedarchiveThe variable, then you can display the publish time in the format of "Year-Month-Date" on the page like this:

<div>发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</div>

In the article or product list page:

On the list page, we usually go througharchiveListTag loop to display multiple articles or products. In the loop body, eachitemrepresents an item, and also includes a timestamp field. We can format the timestamp of eachitemthe timestamp.

For example, in a loop through an article list, we can display the publication date of each article like this:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
        <p>文章标题:<a href="{{item.Link}}">{{item.Title}}</a></p>
        <p>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</p>
    {% endfor %}
{% endarchiveList %}

Except for articles and products, other fields containing timestamps, such as the recent login time in the user profileLastLogin(ThroughuserDetailLabel acquisition), you can also use the same method to format it to ensure that all time information is presented in a user-friendly manner.

Understand the formatting of strings: Time formatting rules in GoLang

At first glance, the date formatting rules in GoLang are somewhat special, as they are not formatted using symbols such asYRepresents the year) instead, it is mapped through a fixed reference date “2006-01-02 15:04:05”.This means that when you want to represent a part of a date, you use the corresponding number from the reference date.

The following are some common Go language date formatting references and their corresponding outputs:

  • "2006": represents a four-digit year, such as 2023
  • "01": represents a two-digit month, such as 07
  • "1"Indicates a single digit month, such as 7
  • "02"Indicates a two-digit date, such as 09
  • "2"Indicates a single digit date, such as 9
  • "15"Indicates a two-digit hour (24-hour format), such as 14
  • "03"Indicates two digits for hours (12-hour clock), such as 02
  • "04"Indicates two digits for minutes, such as 30
  • "05"Indicates two digits for seconds, such as 00
  • "Jan"Indicates the abbreviation for months, such as Jul
  • "Monday": denotes the full name of the week, such as Tuesday

Therefore, when you need the "year-month-date" format, select the year, month, and day from the reference date and combine them with a separator, that is"2006-01-02"This rule may require some adaptation at first, but once mastered, it can be flexibly combined to produce various date display formats required.

BystampToDateTags and flexible date formatting rules in Go language, AnQiCMS users can easily convert background timestamp data into clear, professional, and style-compliant date display, greatly enhancing the reading experience of website content.

Frequently Asked Questions (FAQ)

Q1: Why did I usestampToDateThe label, but the date is displayed incorrectly or blank?

A1: This usually has several reasons. First, please check the inputstampToDateDoes the timestamp variable of the tag valid, for example, is it an actual timestamp value (usually a 10-digit or 13-digit Unix timestamp), rather than an empty value or other types.Secondly, make sure that your 'formatted string' follows the date formatting rules of the Go language (e.g., '2006-01-02'), rather than the common formats of PHP or Java languages.If the format string is incorrect, the template engine may not be able to parse it.

Q2: Can I format the timestamp into other more detailed forms besides "year-month-date"?

A2: Completely okay.The date formatting rules of Go language are very flexible.You can use any combination of the reference date "2006-01-02 15:04:05" to define the format.

  • To display "year/month/day HH:mm:ss", you can use"2006/01/02 15:04:05".
  • You can display "Month-Day Weekday" using"01-02 Monday".
  • You can display "July 9, 2023 2:30 PM" using"2006年1月2日 15点04分".\nIt\'s all about what you want to display, use the corresponding number or text in the reference date to represent it.

Q3: Does AnQiCMS have a global setting to unify the date and time format for the entire website?

A3: Currently, the AnQiCMS template system mainly usesstampToDateLabels to control the display format of dates.This means you need to explicitly specify the format in each template code where the date needs to be displayed.There is no one-click global background setting to unify the display format of all dates, but this template-level control provides great flexibility. You can set different date formats according to the needs of different pages or content types (such as detailed time may be needed on article detail pages, while list pages may only need year-month-day).

Related articles

How to safely extract strings or HTML content and add an ellipsis in AnQiCMS templates?

When operating a website, we often need to display article summaries, product descriptions, or user comments and the like.This text, if not processed, is displayed directly and completely. Long text may disrupt the page layout, affecting the overall aesthetics and information transmission efficiency.This is particularly important to truncate the content appropriately and add an ellipsis.AnQiCMS as an efficient and customizable content management system, its powerful template engine developed based on the Go language provides us with a simple and intelligent solution.Pass through built-in filters (filters)

2025-11-08

How to debug template in AnQiCMS and view the structure and value of variables (using dump filter)?

When developing website templates, we often encounter such confusion: what data does a variable in the template contain?What is its structure? What is the type? And what is the specific value?If these questions cannot be answered quickly, the debugging process will become extremely繁琐and time-consuming.Fortunately, AnQiCMS has provided us with a powerful and convenient debugging tool——`dump` filter, which can help us easily reveal the true face of variables.AnQiCMS developed in Go language, using a template engine syntax similar to Django

2025-11-08

How to generate random text of a specified length as placeholder content in AnQiCMS template?

## Generate random text of a specified length as placeholder content in AnQiCMS template During website development and content operation, we often encounter scenarios where we need to fill in placeholder content (Placeholder Content).Whether it is designing a new template, testing page layout, or giving an initial preview when the content is not fully ready, placeholder text can help us quickly build prototypes, intuitively evaluate the visual effects, and do not need to wait for the real content to be filled in.

2025-11-08

How does AnQiCMS filter or replace external links in content to standardize display?

External links contained in website content, if not managed properly, may have a negative impact on the website's search engine optimization (SEO) effect, user experience, and even the quality of the content.AnQiCMS as a comprehensive content management system, provides a series of powerful and flexible tools to help you effectively filter or replace external links in the content, ensuring the standardized display of website content.

2025-11-08

How can simple mathematical arithmetic operations be performed in the AnQiCMS template to display the result?

Handling arithmetic operations in AnQiCMS templates is an important link to the dynamic and personalized display of website data.No matter whether you need to calculate the total price of goods, display discount information, or make conditional judgments based on certain values, AnQiCMS's powerful template engine can provide flexible support.Thanks to its Django template engine features, simple mathematical arithmetic operations can be performed directly in the template without complex code logic.### Directly perform expression calculation in the template AnQiCMS template engine supports double braces `{{ }}`

2025-11-08

How to split a string into an array by a specified separator and iterate and display it in the template in AnQiCMS?

In website operations and content management, we often encounter such scenarios: a certain field in the background stores multiple related information, such as multiple keywords of an article, various functional tags of a product, or multiple aliases of an author.This information is usually connected into a string with commas, semicolons, or other specific symbols.When it is necessary to display this information one by one or perform style processing in the website front-end template, it is necessary to split this string into independent items and then traverse and display them one by one.AnQiCMS provides a powerful and flexible template engine, its syntax style is similar to

2025-11-08

How to remove specific characters or spaces from a string in AnQiCMS template?

In website content management, string processing is inevitable and crucial.In order to maintain the beauty of the page, keep the standardization of data output, or for SEO optimization, we often need to make fine adjustments to text content, such as removing extra spaces, punctuation marks, or other specific characters.AnQiCMS provides a flexible and powerful template engine, allowing us to easily implement these string operations in front-end templates.

2025-11-08

How does AnQiCMS determine if a string or array contains a specific keyword and display content accordingly?

In website operation, we often need to dynamically adjust the display of the page based on the specific attributes or keywords of the content.For example, when the article title includes the words 'latest', we may want to add a prominent 'NEW' label; or when a product description mentions 'Free Shipping', an icon showing free shipping should be automatically displayed.AnQiCMS (AnQiCMS) provides powerful template functions and rich filters, making it easy to meet these needs.

2025-11-08