How to format the article publish time in the template to display it in the specific format of 'Year-Month-Day'?

Calendar 👁️ 63

In website operations, a clear and unified time display format is crucial for improving user experience and content professionalism.Especially the publication time of the article, a standardized "year-month-date" format makes it easy for visitors to understand.In AnQiCMS (AnQi CMS), achieving this is actually very simple and flexible.

Understand AnQiCMS's time handling method

When using AnQiCMS, you will find that articles (archive) and products and other content all have two key time fields:CreatedTime(Creation time/Publish time) andUpdatedTime(Update time). In the template, you can directly call these fields, for example{{archive.CreatedTime}}or{{item.CreatedTime}}They are usually presented in the form of Unix timestamps, such as a string of numbers1678886400. Such numbers are not intuitive for visitors and lack beauty.

AnQiCMS knows this need and therefore provides special template tags and filters to help us easily convert these timestamps into various readable date formats.

The core of time formatting:stampToDatewith the tag andformatParameter

The core of AnQiCMS handling time formatting lies in its powerfulstampToDatetags, as well as those that can be used directly in certain specific tagsformatParameter.

The time formatting rules of Go language

Before delving into how to use it, we first need to understand the Go language time formatting rules used by AnQiCMS. Unlike many other systems that useYYYY-MM-DDThis placeholder is different, Go language uses a fixed reference time to define various date and time formats, the reference time is:

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

You just need to build the format string you want according to the corresponding part of this reference time. For example, to achieve the format 'Year-Month-Day', we should write it as'2006年01月02日'.

1. UsearchiveDetailTag retrieval and formatting of the article detail page time

When you display the publish time of a single article on the article detail page (for exampledetail.html), you can directly in thearchiveDetailUsed in tagsformatParameters are used to specify the output format. This is the most convenient way:

<div class="article-meta">
    <span>发布日期:</span>
    {# 直接在 archiveDetail 标签中通过 format 参数指定“年-月-日”格式 #}
    <span>{% archiveDetail with name="CreatedTime" format="2006年01月02日" %}</span>
</div>

In this way, the publication time of the article will be displayed directly in the format "March 15, 2023", without additional conversion steps. Similarly,UpdatedTimeThe field can also use the sameformatParameter for formatting

2. InarchiveListFormatting the article list time in the loop

In the article list page (for examplelist.htmlor the homepageindex.htmlYou would usually go througharchiveListTag loops to display multiple articles. In this case,item.CreatedTimeIt returns an original timestamp, which is when it needs to be usedstampToDateTags as filters to process:

{% archiveList articles with type="page" limit="10" %}
    {% for item in articles %}
    <article class="article-item">
        <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
        <div class="article-info">
            <span>分类:{% categoryDetail with name="Title" id=item.CategoryId %}</span>
            {# 使用 stampToDate 标签格式化循环中的 item.CreatedTime #}
            <span>发布时间:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</span>
            <span>阅读量:{{item.Views}}</span>
        </div>
        <p>{{item.Description}}</p>
        <a href="{{item.Link}}" class="read-more">阅读更多</a>
    </article>
    {% else %}
    <p>暂时没有文章可供显示。</p>
    {% endfor %}
{% endarchiveList %}

Here, stampToDateAccept two parameters: the first is the original timestamp (item.CreatedTime), and the second is the format string you want ("2006年01月02日")

More common time format examples

In addition to the "Year-Month-Day" format, you can also combine various common date and time formats as needed:

  • Year/Month/Date:"2006/01/02"

    {{stampToDate(item.CreatedTime, "2006/01/02")}}
    

    Display effect:2023/03/15

  • Year-Month-Day Hour:Minute:Second:"2006-01-02 15:04:05"

    {{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}}
    

    Display effect:2023-03-15 10:30:00

  • Month-Day:"01-02"

    {{stampToDate(item.CreatedTime, "01-02")}}
    

    Display effect:03-15

By flexibly using the reference time formatting rules of Go language, you can easily achieve any date and time display effect you want in the AnQiCMS template.This unified and powerful formatting capability makes your website content display more professional and user-friendly.

Frequently Asked Questions (FAQ)

1. Why is the date format string "2006-01-02", not "YYYY-MM-DD"?

This is because AnQiCMS is based on the Go language, which uses a unique 'reference time' mode when formatting time. You need to follow2006年01月02日 15时04分05秒This fixed reference time to write your format string. For example,2006Represents the year, `0

Related articles

How to avoid malicious collection of content in AnQiCMS and add watermarks to images to protect copyright?

In today's world where digital content is increasingly rich and spreads rapidly, protecting the copyright of original content is particularly important.Malicious collection and unauthorized use of images not only harms the creators' labor results, but may also have a negative impact on the website's SEO and brand image.AnQiCMS (AnQiCMS) understands these challenges and therefore incorporates multiple features to help users effectively avoid malicious content scraping and add watermarks to images to protect copyright.In the era of information explosion on the Internet, original content is a valuable asset of the website. However

2025-11-09

How to display the copyright information, filing number, and other global settings on the front-end page?

The footer usually carries important content such as copyright information and filing number, which not only relates to the professional image of the website, but also the legal requirements and the foundation of user trust.In AnQiCMS, these global settings can be configured and called very conveniently, making your website information clear at a glance.Next, we will learn in detail how to configure and display these key information in AnQiCMS.### Step 1: Configure Global Information in the Background First, you need to log in to your AnQiCMS admin panel, find the Back-end Settings in the left navigation bar.

2025-11-09

Can AnQiCMS set independent display templates for different articles? How to operate?

## AnQiCMS: Create a dedicated display template for every article of yours In our daily website operations, the diversity of content presentation is the key to improving user experience, enhancing brand image, and optimizing SEO effects.Different types of articles often require different layouts and features, such as a deep technical analysis article that may require a more focused reading interface, while a product introduction page may require richer image displays and purchase buttons.So, can AnQiCMS (AnQiCMS) meet the need for setting independent display templates for different articles?I can clearly tell you

2025-11-09

How to precisely control the display level and sorting order of the navigation menu in AnQiCMS template?

In website operation, a clear and intuitive navigation menu is like a guidepost for visitors to explore the website.A well-designed navigation not only improves user experience but also helps search engines better understand the website structure.AnQiCMS provides a flexible mechanism to help users accurately configure the hierarchy and sorting of navigation menus in the background and implement these controls in the front-end templates. ### Build Navigation Skeleton: Backend Settings Are Key Navigation menu control in AnQiCMS starts from its backend management interface.This is like the blueprint design center of the navigation menu

2025-11-09

How to implement the 'Recommended Attribute' feature of AnQiCMS for displaying article categories on the front end (such as headlines, recommendations)?

AnQiCMS provides rich and flexible tools for the organization and display of website content, among which the "recommendation attribute" feature is a very practical design.It allows us to add special tags to articles for classification display on the website front end, such as highlighting 'headlines' articles or displaying 'recommended' content in specific areas. ### Understanding "Recommended Properties" The "Recommended Properties" in AnQiCMS essentially act as metadata tags for articles, used to describe the characteristics or importance of the articles.In the background content management interface, when you edit or publish an article

2025-11-09

How to customize the shutdown prompt displayed to users when the website is closed?

During the operation of the website, we sometimes encounter situations where we need to temporarily close the website, such as system upgrades, server migrations, major content adjustments, or simply brief technical maintenance.How can you clearly and friendliness convey the current status of the website to the visitors at this time and provide necessary information, which is particularly important.AnQiCMS provides comprehensive shutdown management functions for website administrators, allowing us to easily set the shutdown status of the website and highly customize the display of shutdown prompt information.### Enable website shutdown feature When your website needs to be temporarily offline

2025-11-09

How does AnQiCMS manage and display single-page content, such as 'About Us' or 'Contact Information'?

In website operations, single-page content such as "About Us", "Contact Information", "Service Introduction", and "Privacy Policy" is a core element in building the basic information of the website, enhancing user trust, and facilitating users in quickly obtaining information.These pages usually have fixed content, but their importance is self-evident.AnQiCMS provides an intuitive and powerful single-page management mechanism that allows you to easily create, manage, and display these key contents.### Create and Edit Single Page Content: Starting from the Backend Manage Single Page Content in Anqi CMS

2025-11-09

How to generate a clickable table of contents (TOC) based on the title structure of the article content?

In AnQiCMS (AnQiCMS), generating a clickable table of contents (Table of Contents, TOC) for your article detail page is a very practical feature.It can not only significantly improve the reading experience of users, help readers quickly locate the chapters of interest, which is particularly important for long content, but also optimize the page SEO performance to a certain extent.The Anqi CMS provides very practical mechanisms to help us achieve this, the core lies in the clever use of the title structure of the article content.###

2025-11-09