How to format the display of the publication and update time of articles in the AnQiCMS template?

Calendar 👁️ 71

In the Anqi CMS website template, displaying the publication and update time of articles is a key factor in improving user experience and content management efficiency.Clearly present this time information, which can not only help visitors quickly understand the timeliness of the content, but also help search engines better crawl and index.AnQiCMS provides a powerful and flexible template tag that allows you to format timestamps into various easily readable date and time formats as needed.

Core mechanism:stampToDateTag

The template system of AnqiCMS is built-in with a namedstampToDateThe formatted timestamp label, which is the core tool for processing article publishing and update times. This label is used to convert the 10-digit Unix timestamp stored in article data (such as1609470335Convert it to the date and time string commonly used in our daily life.

stampToDateThe usage of the tag is very intuitive, it requires two main parameters:

  1. Timestamp (timestamp)This is the original time data you want to format, usually obtained from the article object, such asarchive.CreatedTimeoritem.UpdatedTime.
  2. format string (format_string)This is a string that defines the output format. It is used by many CMS systems.Y-m-d H:i:sThis placeholder is different, AnQiCMS follows the time formatting rules of the Go language, it uses a fixed reference time2006-01-02 15:04:05Use as the 'template' for format definition. You need to replace the year, month, day, hour, minute, second, and other elements in this reference time with the display style you want.

For example, if you want to display年-月-日then the format string is"2006-01-02"; if you want to display月/日 时:分then the format string is"01/02 15:04".

Get the timestamp of the article

In the AnQiCMS template, the publication time of the article is usually obtained throughCreatedTimefield, while the update time is obtained throughUpdatedTimefield. These fields are stored in the form of Unix timestamps.

When you display the publish time of a single article on the article detail page (for exampledetail.htmlWhenarchiveyou can directly access these timestamps through

  • archive.CreatedTimeused for publish time
  • archive.UpdatedTimeused for update time

If you are looping through the article list (for examplelist.htmlOr on the home page) the article data is usually defined as a loop variable, for exampleitemAt this point, you can access the timestamp in this way:

  • item.CreatedTime
  • item.UpdatedTime

Formatting practical example

Here are some common formatting scenarios and their corresponding template codes:

1. Display year-month-date only

This is one of the most commonly used date display formats, for example2023-10-26.

{# 在文章详情页显示发布日期 #}
<span>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</span>

{# 在文章列表中显示更新日期 #}
{% for item in archives %}
    {# ... 其他文章信息 ... #}
    <span>更新于:{{ stampToDate(item.UpdatedTime, "2006-01-02") }}</span>
{% endfor %}

2. Display the complete year-month-date time:minute:second

For example, if you need time information accurate to the second2023-10-26 14:35:01.

{# 显示发布时间的完整格式 #}
<span>发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04:05") }}</span>

3. Display the date in Chinese format

For example2023年10月26日.

{# 显示中文格式的发布日期 #}
<span>发布于:{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}</span>

4. Show only the month and day, or only the hour and minute

You can extract the specific part of the timestamp as needed

{# 只显示月日,例如 10-26 #}
<span>{{ stampToDate(archive.CreatedTime, "01-02") }}</span>

{# 只显示时分,例如 14:35 #}
<span>{{ stampToDate(archive.CreatedTime, "15:04") }}</span>

5. Combine other text and HTML tags

You can combine formatted time with other text, icons, or HTML structures to achieve a richer display effect.

<p>
    <i class="fa fa-calendar"></i> 发布于 <time datetime="{{ stampToDate(archive.CreatedTime, "2006-01-02T15:04:05Z07:00") }}">
        {{ stampToDate(archive.CreatedTime, "2006年01月02日") }}
    </time>
</p>

In the above example,datetimeProperties use the ISO 8601 format, which is more friendly to search engines and accessibility tools, while users see more readable Chinese format.

Summary

BystampToDateLabel, AnQiCMS provides great flexibility to control the display of article publication and update time.Understanding the reference time formatting rules in Go language is the key to mastering its usage.Once familiar, you can easily present various date and time formats that meet design and user requirements, thereby optimizing the display of your website content.


Frequently Asked Questions (FAQ)

1. Why do I followYYYY-MM-DDthe way to set format strings, but the result is2006-01-02?This is usually because you have misunderstood the time formatting rules in the Go language. In Go (and AnQiCMS templates), the format string is not used withYYYY/MMUse a specific reference date instead of a placeholder2006-01-02 15:04:05You need to replace the corresponding part of the reference time with the style you want to output. For example, to display年-月-日you should use"2006-01-02"As a format string, notYYYY-MM-DD.

2. How can I display the current date and time in the template, besides the article's publish and update time?You can use the AnQiCMS providednowLabel to get and display the current date and time. This label also follows the formatting rules of the Go language. For example, to display the current year-month-day, you can use{% now "2006-01-02" %}; To display the current full date and time, use{% now "2006-01-02 15:04:05" %}.

3. Can I display the date as 'today', 'yesterday', or 'N days ago' such relative time?AnQiCMS'stampToDateThe tag is mainly used to format timestamps into a fixed date-time string pattern, and does not directly support relative time displays such as "today", "yesterday", or "N days ago".If you need such a dynamic relative time display, it is usually implemented on the front-end using JavaScript or passed to the template after more complex business logic is calculated on the back-end.A common frontend solution is to first usestampToDateOutput the date in standard format (such as ISO format), then use a JavaScript library (such as Moment.js or date-fns) or a custom script to convert it to relative time after the page is loaded.

Related articles

How to automatically generate and display a table of contents (TOC) on the AnQiCMS article content page?

In AnQiCMS, automatically generating and displaying the table of contents (TOC) for article content pages not only significantly enhances the user reading experience but also helps search engines better understand the article structure, thereby having a positive impact on SEO.With the powerful and flexible template system of AnQiCMS, it is simpler to achieve this function than you imagine.How does AnQiCMS support automatic directory generation?

2025-11-08

How to build multi-condition filtering function for article list and display results in AnQiCMS?

In website operation, providing flexible multi-condition filtering functions for article lists can greatly enhance user experience and the discoverability of content.AnQiCMS (AnQiCMS) leverages its powerful content model and rich template tags to make building such features intuitive and efficient.Below, we will delve into how to step by step implement multi-condition filtering and display results of article lists in AnQiCMS.

2025-11-08

How to display related article recommendations at the bottom of the AnQiCMS article detail page?

In AnQiCMS, adding a related article recommendation feature to the article detail page can not only effectively increase the user's stay time on the website, guide them to discover more interesting content, but also have a positive impact on the website's SEO performance.It is not difficult to achieve this function with the flexible and powerful template tag system provided by AnQiCMS. Understanding AnQiCMS Template Structure In AnQiCMS, the page layout and content display of the website are controlled by template files.

2025-11-08

How to implement website in-site search and display article search results in AnQiCMS?

In website operation, the in-site search function plays a crucial role.It can not only help visitors find the information they need quickly, improve user experience, but also through the analysis of search data to understand user needs, optimize content strategy.The AnQi CMS is an efficient content management system that provides a set of intuitive and powerful mechanisms to implement site search and flexibly display search results.The on-site search function of Anqi CMS is designed to be very practical.

2025-11-08

How to display the reading views of articles on the AnQiCMS article list or detail page?

In website content operation, the number of article views is an important indicator, which helps us understand the popularity of the content and the user's interests.AnQiCMS provides a convenient way for you to visually display this data on the article list page or detail page. ### Understanding AnQiCMS's Page View Mechanism AnQiCMS is a comprehensive content management system that comes with a built-in mechanism for automatically recording article page views.

2025-11-08

How to judge and highlight the first article in the AnQiCMS article list loop?

In website content display, we often encounter the need to handle the first element of the article list specially.In order to achieve visual prominence, to implement unique layout design, or to give the first article more attention, AnQiCMS provides flexible and powerful template tags to help us easily achieve this goal.AnQiCMS uses a syntax similar to the Django template engine, where the `for` loop tag plays a core role in processing content lists.When we use `archiveList`

2025-11-08

How to nested display the list of articles under the category page in AnQiCMS?

In website operation, the content classification page carries the important responsibility of organizing information and guiding users to browse.A well-designed and content-rich category page can not only improve user experience but also optimize search engine crawling efficiency.For users of AnQiCMS, nesting the list of articles under the category page is a very basic and commonly used feature.This article will introduce you in detail on how to use the powerful template tags of Anqi CMS to easily achieve this goal.Understanding the Classification Page and Content Model Before we begin

2025-11-08

How to filter and display a specific list of articles based on recommended properties (Flag) in AnQiCMS?

An QiCMS provides operators with rich tools for organizing and displaying website content with its flexible and efficient content management capabilities.Among the "recommended attributes" (Flag) is a very practical feature that allows you to easily categorize and mark articles, thereby enabling accurate content filtering and dynamic display on the website front end.This article will introduce in detail how to use these recommended attributes in AnQiCMS to filter and display the specific article list you want.### Deep Understanding of AnQiCMS Recommendation Attributes (Flag) AnQiCMS Recommendation Attributes

2025-11-08