How to accurately format the display of the article's publication and update timestamps in AnQiCMS templates?

Calendar 👁️ 85

In website content operation, clearly displaying the publication and update time of articles is crucial for improving user experience, providing timeliness information of content, and optimizing search engine indexing.AnQiCMS (AnQiCMS) provides a powerful and flexible timestamp formatting function in the template, allowing us to convert original timestamp data into user-friendly date and time display according to specific needs.

We need to use a core template tag to display the publication and update time of the article accurately in the AnQiCMS template:stampToDate. This tag can convert the 10-digit timestamp stored by the system into the date and time format we want.

UnderstandingstampToDateThe core usage of tags

stampToDateThe syntax structure of the tag is very intuitive:{{stampToDate(时间戳变量, "格式字符串")}}. Among them:

  • Timestamp variableThis is usually obtained from article or list dataCreatedTime(Publish time) orUpdatedTime(Updated time), they exist in the form of a 10-digit timestamp.
  • Format stringThis is the most critical part, which determines how the timestamp will be displayed. Common with other CMS systemsY-m-d H:i:sDifferent placeholder, AnQiCMS (because it is developed based on Go language) adopts the special date and time formatting reference string of Go language. This reference string is fixed2006-01-02 15:04:05. You do not need to remember the meaning of this string, just use it as a template to adjust it according to the style you want to output.

For example, if you want to display2023年02月09日Then your format string should be"2006年01月02日"If you want to display02/09/2023The format string is then"01/02/2006"Here are the numbers.2006/01/02/15/04/05The elements, such as year, month, day, hour, minute, and second in Go language date format, can be freely combined according to your needs to define the final display format.

Get the timestamp data of the article

Before formatting the timestamp, we first need to obtain the original timestamp from the article data. This can be done byarchiveDetailTags (used for the article detail page) orarchiveListLabel (used to implement on article list page).

  • Get the timestamp on the article detail page:When you are in the article detail template (such as{模型table}/detail.html), you can directly accessarchivethe current article's object.CreatedTimeandUpdatedTimefield.

    <p>发布时间:{{stampToDate(archive.CreatedTime, "2006-01-02 15:04")}}</p>
    <p>更新时间:{{stampToDate(archive.UpdatedTime, "2006年01月02日 下午03时04分")}}</p>
    
  • Retrieve timestamp on article list page:In article list template (such as{模型table}/list.html), you usually usearchiveListLabel loops through multiple articles. Inside the loop, the data of each article is assigned to a variable (for exampleitem), and then you can extract the timestamp from it.

    {% archiveList archives with type="page" limit="10" %}
        {% for item in archives %}
            <div>
                <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
                <p>发布于:{{stampToDate(item.CreatedTime, "2006-01-02")}}</p>
                <p>最近更新:{{stampToDate(item.UpdatedTime, "01/02 15:04")}}</p>
            </div>
        {% endfor %}
    {% endarchiveList %}
    

Common time formatting examples

To better illustratestampToDateThe flexibility, here are some common formatting requirements and their corresponding format strings:

  • Only display the date (year-month-date)For example:2023-02-09Format string:"2006-01-02"

  • Display the full date and timeFor example:2023-02-09 15:04:05Format string:"2006-01-02 15:04:05"

  • Chinese date formatFor example:2023年02月09日Format string:"2006年01月02日"

  • Short date format (Month/Day)For example:02/09Format string:"01/02"

  • Format with the day of the weekFor example:周四, 2023年02月09日Format string:"Mon, 2006年01月02日"(Note here thatMonrepresents the day of the week, which will be automatically translated.

  • Displays time (hour:minute)For example:15:04Format string:"15:04"

You can freely choose and combine these formats according to the overall design of the website and user habits.

Summary

BystampToDateLabel and flexible date and time formatting reference for Go language, AnQiCMS makes it very simple to accurately control the display of article publication and update times in templates.Whether it is a concise date or detailed to the second-level time, you can easily achieve it by adjusting the format string to ensure the accuracy of the timeliness of the website content.


Frequently Asked Questions (FAQ)

  1. Why does the AnQiCMS time format string look strange and not commonY-m-d?Answer: This is because AnQiCMS is developed based on the Go language. Go language does not use placeholders when formatting time, but uses a fixed reference time.2006-01-02 15:04:05. Just replace the corresponding part of this reference time with the output style you want. For example, if you want to display the year, write the format string in it.2006, to display the month, write01.

  2. Ask: If my article has not been updated,UpdatedTimewhat will be displayed?Answer: If the article has never been updated,UpdatedTimethe field usually goes withCreatedTimeThe value of the field is the same. In actual display, you can optionally only displayCreatedTimeorUpdatedTimewithCreatedTimeThe update time is displayed only when different to avoid unnecessary repeated information.

  3. Ask: Can some localized names of weekdays, such as "Monday", be added when formatting timestamps?Answer: Yes, you can. Go language time formatting refers to the string in,MonAbbreviation for the day of the week (for example, "Monday"),MondayRepresent the full name of the week (for example, "Monday"). The system will automatically localize the display according to the language setting of the current website. For example, on a Chinese site, it may be displayed as"Mon, 2006年01月02日", it may be displayed as周一, 2023年02月09日.

Related articles

Does AnQiCMS provide a convenient tag to display the website logo and name, and support multi-site calls?

In the process of managing and operating a website, the brand image of the website - Logo and website name - is undoubtedly the core identification.They not only represent the identity of the website, but also directly affect users' perception of the brand.For a system like AnQiCMS that is committed to providing an efficient content management solution, how conveniently to display this key information, and to support flexible calls under a multi-site environment, is an important standard to measure its usability and powerful features.AnQiCMS' design concept in this aspect is centered around 'convenience' and 'flexibility'

2025-11-09

How to correctly display user comments and their review status in the AnQiCMS comment list?

In AnQiCMS, managing and displaying user comments, especially their review status, is a very practical feature in content operation.It not only helps us maintain community order, but also allows flexible control over the presentation of website content, ensuring the quality and compliance of published content.Next, let's take a detailed look at how to correctly display user comments and their review status in your website comment list. ### Core Function: Comment List Invocation To display user comments on the website, we need to rely on the powerful template tag system of AnQiCMS.

2025-11-09

How does AnQiCMS ensure that the HTML content output through the `safe` filter is secure and correctly parsed and displayed on the front end?

In website operation, we often need to display user input or HTML content generated by a rich text editor.How to ensure that these dynamic contents are displayed correctly and avoid potential security risks is a challenge that every content management system must face.AnQiCMS as a Go language CMS that focuses on security has a mature mechanism in this aspect, especially the `safe` filter it provides, which plays a key role in ensuring the security and correct parsing of front-end content. ### Default security considerations: Why escape automatically?

2025-11-09

How to set up an independent display layout and elements for the single page of AnQiCMS, such as the 'About Us' page?

In AnQiCMS, setting up an independent display layout and elements for single pages like "About Us" can make the website content more distinctive and professional.AnQiCMS with its flexible template engine and modular design makes this process intuitive and efficient.Why do you need a custom layout for a single page?

2025-11-09

How does AnQiCMS dynamically generate and display breadcrumb navigation and control whether to include the current page title?

When building a user-friendly website, breadcrumb navigation plays an indispensable role.It not only can intuitively display the user's current position on the website, avoid getting lost, but also optimize the website structure, which is very beneficial for search engine optimization (SEO).For users of AnQiCMS, utilizing its powerful template tag system to dynamically generate and flexibly control breadcrumb navigation is the key to improving the website experience.

2025-11-09

How to configure AnQiCMS so that it displays correctly as an independent mobile site on mobile devices?

In AnQiCMS, to provide a better user experience for mobile device users, we can configure the website to display independent site content on mobile devices.This independent operation model for PC and mobile terminals not only helps to design and optimize for different devices in a refined manner, but also can improve the SEO performance of mobile terminals in specific scenarios.AnQiCMS provides us with a variety of website display modes, including the "PC+Mobile independent site" mode, which is the basis for realizing independent content display on the mobile end.Next, we will step by step discuss how to configure AnQiCMS

2025-11-09

How to automatically render Markdown format content to HTML when AnQiCMS displays article content?

In Anqi CMS, automatically rendering Markdown content into HTML is a core feature provided by the system, aiming to help content creators publish rich text content in a concise and efficient manner.This process is not a mysterious black box operation, but is achieved through clear settings and flexible template tags.

2025-11-09

How to use AnQiCMS's `archiveFilters` tag to dynamically display multi-dimensional content filtering conditions?

How to help visitors quickly find the information they are interested in on a content-rich website is the key to improving user experience and website efficiency.The traditional fixed category navigation often fails to meet users' multi-dimensional and personalized filtering needs.At this time, the `archiveFilters` tag provided by AnQiCMS can fully display its capabilities, helping us dynamically display multi-dimensional content filtering conditions, making your website content management and presentation more flexible and intelligent.

2025-11-09