How to format an article's publish timestamp into a user-friendly date and time format, such as '2023 January 01 14:30'?

Calendar 👁️ 71

User-friendly date and time display is crucial for website content.A clear and readable date format not only enhances the visitor's reading experience but also helps them quickly understand the timing of the content's publication or update.In AnQiCMS, formatting the publication timestamp of articles into a format such as '2023-01-01 14:30' is both flexible and efficient.

Understanding the use of timestamps in AnQiCMS

In AnQiCMS managed content, whether it is articles, products, or other custom models, the publication or update time is usually stored in the form of a timestamp (Unix Timestamp).These timestamps are essentially numbers representing the number of seconds from 00:00:00 UTC (Coordinated Universal Time) on January 1, 1970, to a specific moment.In template files, we usually come acrossCreatedTimeAnd (content creation time) as well asUpdatedTime(content update time) such fields are variables that store these original timestamps.

Directly displaying the original timestamp number is not intuitive for users, therefore we need a tool to convert it into a date and time format that people are accustomed to reading.

Core Tool:stampToDateTag

AnQiCMS provides a namedstampToDateThe template tag, specifically used for converting timestamps to user-friendly date and time formats. This tag's usage is very concise and clear, it mainly accepts two parameters:

  1. The timestamp to be formatted:This is usually the value you get from articles, products, or other content data.CreatedTimeorUpdatedTimeof the field. For example, on the article detail page it might bearchive.CreatedTimeIn the article list loop it may beitem.CreatedTime.
  2. Formatting string:This is the core parameter that determines the final format in which the date and time are displayed.AnQiCMS is a system developed based on the Go language, and its date and time formatting follows the specific rules of the Go language.

Deep understanding of formatted strings (Go language date and time reference)

The date and time formatting mechanism of Go language and other languages (such as PHP'sY-m-d H:i:sIt is slightly different. It does not use abstract placeholders, but uses onea fixed reference date and timeas a format template. This reference time is:

2006年01月02日 15时04分05秒 -0700 MST

You can simply remember each number in this reference time representing the date and time component:

  • 2006Represents the year
  • 01Represents the month (two digits)
  • 02Represents the date (two digits)
  • 15representing hours (24-hour system)
  • 04representing minutes
  • 05representing seconds
  • PMRepresenting the AM/PM indicator (if 12-hour format is needed)

When you want to format a timestamp into a specific style, just build your format string according to the style you want, using the corresponding numbers and words from the above reference time.

For example, if we want to get the format '2023-01-01 14:30', then the format string should be written as:"2006年01月02日 15:04".Please note, here,15Represents the hour part in 24-hour time format,04Represents the minutes part.

Some common format string examples and their meanings:

  • "2006-01-02": Displays as '2023-01-01'
  • "2006年01月02日": Displays as '2023 January 01'
  • "15:04": Displayed as '14:30' (24-hour time format)
  • "03:04 PM": Displayed as '02:30 PM' (12-hour time format with AM/PM indicator)
  • `“2006-01-0

Related articles

How to receive and display custom form fields from the backend in a frontend comment form?

In website operation, the message form is an important channel for interacting with users, collecting feedback and leads.AnQiCMS provides flexible comment functions, one very practical ability is to allow us to customize form fields in the background, and seamlessly present them in the front-end comment form to meet the personalized information collection needs of different business scenarios.Next, we will discuss in detail how to implement this feature in Anqi CMS.### One, define the custom message field in the background First, we need to enter the Anqi CMS backend management interface

2025-11-08

How to use the article's `flag` attribute (such as 'Top Story', 'Recommendation') to filter and highlight specific content?

In AnQi CMS, efficiently managing and highlighting specific content is the key to website operation success.When the content of the website becomes increasingly rich, how to ensure that important information can quickly attract the attention of visitors and guide them to browse the core content, which has become a problem that every operator needs to think deeply about.The Anqi CMS provides a powerful "recommendation attribute" (flag) feature that allows you to easily filter and highlight specific content, thereby enhancing user experience and website operation effects.### Understanding the `flag` attribute of Anqi CMS The `flag` attribute, as the name suggests

2025-11-08

How to obtain and display the contact information configured on the back-end, such as phone numbers, email addresses, WeChat QR codes, and other diversified information?

AnQiCMS (AnQiCMS) is an efficient and flexible content management system that excels in helping enterprises display their brand image and provide convenient communication channels.For any website, having clear and easily accessible contact information is crucial for building trust with users and promoting cooperation.AnQi CMS deeply understands this, providing a set of intuitive and powerful functions that allow you to easily manage and display various contact information such as phone numbers, email addresses, WeChat QR codes, etc.### Backend Configuration: Central Management Center for Contact Information In AnQi CMS

2025-11-08

How to retrieve and display the global settings information of a website, such as the website name, Logo, and filing number in the template?

When building website templates in AnQiCMS, we often need to display some general information at the website level, such as the name of the website, Logo image, and legal filing number required by law.This information is usually managed in the background global settings to ensure consistency across the entire site and ease of maintenance.AnQiCMS has a powerful template tag system, especially the `system` tag, which makes it very intuitive and efficient to obtain and display these global settings in the template.### Understanding Global Settings and `system`

2025-11-08

How to display a dynamic banner image on the homepage of a website and implement click-through?

Displaying dynamic banner images with click-through functionality on the website homepage is a common need to enhance the visual appeal and user interaction of the site.By utilizing the powerful functions and flexible template mechanism of AnQiCMS, we can easily achieve this goal.The entire process is mainly divided into two parts: background configuration and front-end template invocation. ### First Step: Configure Banner Image and Link To display a dynamic rotating Banner on the website homepage, you first need to upload the image and set the link in the AnQiCMS backend. Typically

2025-11-08

How can AnQi CMS achieve automatic image compression and conversion to WebP format to optimize page loading performance?

Website images are important elements to enhance the attractiveness of content, but if not handled properly, they may also become the 'culprits' that slow down the website loading speed.For website operators, optimizing image loading performance can not only improve user experience but also have a positive impact on search engine rankings (SEO).AnQi CMS is an efficient content management system that fully considers this point, providing users with convenient and powerful image optimization features, especially automatic compression and WebP format conversion, allowing the website to maintain image quality while having faster loading speed.###

2025-11-08

How to determine if a variable (such as the article title) exists or is empty in a template and display a custom default value?

It is crucial to ensure the integrity and user experience of the content in website management.Sometimes, due to missing content or unassigned variables, blank areas or error messages may appear on web pages, which undoubtedly affects the professionalism of the website.The template engine of AnQiCMS provides a flexible and powerful mechanism to help users elegantly handle these situations, ensuring robust presentation of page content even if variables do not exist or are empty.In AnQiCMS template design, we often encounter situations where we need to display a variable (such as article title, description, image URL, etc.)

2025-11-08

How to implement complex conditional judgment and content iteration display in templates using `if/else` and `for` loop tags?

AnQi CMS as an efficient and flexible content management system, its template design capability is the key to building a rich and varied website content.During the template creation process, flexibly using conditional judgment (`if/else`) and content iteration (`for` loop`) tags can help us achieve a more intelligent and dynamic content display, allowing the web page to present personalized layouts and information according to different situations.

2025-11-08