How to format the display of the article's publish time in the template, for example “2023 June 1st”?

Calendar 👁️ 66

When operating a website, the publication time of articles is often one of the important pieces of information that users pay attention to.A clear and readable date format not only enhances user experience but also helps improve the professionalism of website content.AnQi CMS is an efficient and customizable content management system that provides a flexible way to control the display of various information in templates, including the publication time of articles.

The AnQi CMS template system uses a concise syntax similar to Django, variables are usually referenced through double curly braces{{变量}}and logical control uses{% 标签 %}such a structure. The publication time of the article is usually stored in the system as a timestamp (a numerical sequence representing time).This means we need a method to convert this original timestamp into the date format we are accustomed to reading, such as 'June 1, 2023'.

To achieve this, AnQi CMS provides a very practical built-in tag:stampToDate.

MasterstampToDateTag

stampToDateTags are used to format timestamps into readable date and time strings. Its basic usage is very intuitive:{{stampToDate(时间戳, "格式")}}

The key is the second parameter - "format". Anqi CMS'sstampToDatetags follow the Go language time formatting rules. Used by many systemsYYYY-MM-DDThis placeholder is different, the time formatting in Go language is based on a specific reference point:2006年01月02日 15点04分05秒 -0700 MST. Just replace the year, month, day, hour, minute, and second in this reference time with the display style you want.

For example, if you want to display "June 1, 2023

How to get the article publication time?

In the Anqi CMS template, you can get the original publication time (timestamp) of the article in the following way:

  • Article detail page:directlyarchive.CreatedTime. Here,archiveIt is usually the data object of the current article detail page.
  • Article list page:In{% for item in archives %}Used in loop:item.CreatedTime.

These fields will return a 10-digit timestamp.

Practical formatting example

Now, we combinestampToDateTags and Go language formatting rules to implement various date display effects:

  1. Displayed as "June 1, 2023":

    {{stampToDate(archive.CreatedTime, "2006年01月02日")}}
    

    here,2006represents the year,01represents the month,02Representing a date, we connect them with Chinese characters “year”, “month”, and “day”.

  2. Displayed as “2023-06-01”:

    {{stampToDate(archive.CreatedTime, "2006-01-02")}}
    
  3. Display as “2023/06/01 15:30”:

    {{stampToDate(archive.CreatedTime, "2006/01/02 15:04")}}
    

    15represents the hour (24-hour format),04Represents minutes.

  4. Only display date “June 01”:

    {{stampToDate(archive.CreatedTime, "01月02日")}}
    
  5. Displayed as "June 1, 2023" (common English format):

    {{stampToDate(archive.CreatedTime, "Jan _2, 2006")}}
    

    Note:JanAbbreviation for the month,_2Represents the date (with a space before if it's a single digit).

Integrate into the actual template.

Let's see how these formatting methods are applied in actual template code.

In the article detail page template (detail.html) :

<article class="article-detail">
    <h1>{{archive.Title}}</h1>
    <div class="article-meta">
        <span>发布于:{{stampToDate(archive.CreatedTime, "2006年01月02日")}}</span>
        <span>浏览量:{{archive.Views}}</span>
        <span>分类:<a href="{{archive.Category.Link}}">{{archive.Category.Title}}</a></span>
    </div>
    <div class="article-content">
        {{archive.Content|safe}}
    </div>
</article>

In the article list page template (list.html) :

<ul class="article-list">
    {% archiveList articles with type="page" limit="10" %}
        {% for item in articles %}
        <li>
            <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
            <p class="summary">{{item.Description}}</p>
            <div class="meta">
                <span>发布时间:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</span>
                <span>阅读量:{{item.Views}}</span>
            </div>
        </li>
        {% empty %}
        <li>暂无文章内容。</li>
        {% endfor %}
    {% endarchiveList %}
</ul>
{% pagination pages with show="5" %}
    {# 这里是分页导航的代码 #}
{% endpagination %}

BystampToDateLabel, you can flexibly control the display of article publication time so that it completely conforms to the design style of your website and meets the needs of your users.This design philosophy of AnQi CMS makes content presentation both powerful and easy to use.


Frequently Asked Questions (FAQ)

1. Why does AnQi CMS use this string for date formatting instead of the common2006-01-02 15:04:05one?YYYY-MM-DD?

This is because AnQi CMS is developed based on Go language, and Go language uses a special reference time point for time formatting:Mon Jan 2 15:04:05 MST 2006. You need to replace the reference value of the time element you want to display (for example, 2006 represents the year, 01 represents the month, 02 represents the date) with the format you want.For example, to display 'Year-Month-Day', you would write"2006-01-02". Although it looks a bit unique, once you master the pattern, you will find it very flexible.

2. In addition to the article's publish time (CreatedTime), other time fields can also be usedstampToDateCan I format it?

Of course you can. As long as it is a 10-digit timestamp field obtained from the Anqi CMS template, such as the article's update timearchive.UpdatedTimeoritem.UpdatedTimeOr other custom fields that store timestamps can be processed throughstampToDateTags should be formatted in the same way. You just need to replace the corresponding variables.

3. Can I display different formats of publish time in the same article or list item?

Absolutely.stampToDateThe label can be used multiple times, and each time it can specify a different format. For example, you may want to display a concise date {{stampToDate(archive.CreatedTime, "2006年01月02日")}}and `{{stampToDate(archive.CreatedTime, “

Related articles

How to display the user's submitted comment content and avatar in the website comment section??

Displaying user comments on the website is a key element in enhancing user interaction and content vitality.For websites using AnQiCMS, its powerful template tag system makes this operation intuitive and flexible.This article will thoroughly introduce how to clearly display user-submitted comments in the website's comment section, accompanied by their unique avatars, thereby enriching the website's user experience.--- ### Understanding the Basics of AnQi CMS Comment Function Firstly, we need to understand how comment content operates in AnQi CMS

2025-11-07

How to display the article summary instead of the full text on the search results page?

How to make users quickly understand the content of the article on the search results page without directly displaying the full text, which is not only related to user experience but also a key factor in search engine optimization (SEO).For those using AnQiCMS, achieving this is very flexible and efficient.

2025-11-07

How to display personalized information on the front end based on the custom content model fields in the backend?

## Unveiling AnQi CMS: How to skillfully use custom fields in the backend to create personalized front-end content display In today's rapidly changing digital world, a website is not just a carrier of information, but also a window for brand personality and user experience.Traditional website content management systems (CMS) are often limited by fixed content structures and are difficult to meet the diverse business needs.However, AnQiCMS (AnQiCMS) has opened a new door to personalized content display for website operators with its flexible content model and powerful custom field features.

2025-11-07

How to display a list of friend links at the bottom of a website?

In website operation, friendship links are an important way to enhance website authority, increase external traffic, and enhance user trust.AnQiCMS (AnQiCMS) offers convenient features, allowing you to easily display these valuable external links at the bottom of your website. ### First Step: Add and Configure友情链接 in the Background Management Firstly, we need to add and manage友情链接 in the Anq CMS background management system.This is like the content list you prepare to display on the website.1. **Log in to the backend:** Log in to the AnQi CMS backend with your admin account

2025-11-07

How to enable and configure the website comment form in AnQiCMS and display custom fields?

In AnQiCMS, creating an interactive guest留言表单 form that collects feedback and can be flexibly configured with custom fields is a very practical feature.AnQiCMS provides a simple and efficient way to achieve this goal, whether it is backend settings or frontend display, it can be easily mastered. ### Enable and configure the message function on the back-end Usually, when you want website visitors to contact you or collect their feedback, a message form is the preferred choice.

2025-11-07

How to display the website's logo image on the front page?

The website logo is the core of the brand image, it not only enhances the professionalism of the website, but also deepens users' recognition of the brand.In AnQiCMS, displaying your logo image on the front page is a straightforward and flexible process, mainly involving backend settings and template code adjustments. ### Step 1: Upload and configure your Logo on the AnQiCMS backend Managing the Logo image of the website is very convenient in AnQiCMS.First, you need to upload the Logo image to the system's backend.

2025-11-07

How to use the `tdk` tag in the template to display the current page's canonical URL?

In website operation, we all hope that our content can be accurately understood and efficiently indexed by search engines.Among them, Canonical URL (standard link) is a very important concept.It can help us solve the problem of duplicate content that may arise due to similar content or accessible from multiple URLs, clearly telling search engines which is the main version of the content, thus concentrating the ranking weight of the page and avoiding dispersion.AnQi CMS as an efficient and flexible content management system, fully considers the needs of SEO optimization, and built-in powerful TDK (Title

2025-11-07

How to display website traffic statistics in the background in the form of charts?

In website operation, understanding and analyzing traffic data is a key link in formulating effective strategies and optimizing website performance.AnQiCMS (AnQiCMS) fully understands this, and therefore provides an intuitive and convenient traffic statistics function in the background, allowing operators to easily grasp all the data of the website in the form of charts, thereby better driving content and marketing decisions.The traffic statistics feature of Anqi CMS is designed to provide a comprehensive view of website performance.It not only records the user's visit behavior, but also pays special attention to the activities of search engine spiders, which is crucial for SEO work.Through the background data statistics module

2025-11-07