How to format the article publish timestamp into a readable date and time in AnQiCMS template?

Calendar 👁️ 73

In website content operations, the publication time of articles is often one of the focuses of users, as it not only affects the interest of users in reading, but also has potential effects on the practicality and authority of the content.However, the time information stored in the database is usually in a machine-readable timestamp format, such as a string of numbers, which is difficult for ordinary visitors to understand.AnQiCMS fully considered this point, providing a flexible way for you to convert these timestamps into clear and readable date and time formats, greatly enhancing the user experience.

AnQiCMS when managing articles in the background, whether you manually publish, schedule publishing (with the help of the "Time Factor-Scheduled Publishing" feature), or through content collection or import, the creation time of the article (CreatedTimeAnd update time(UpdatedTimeThe information will be stored in the standard timestamp format. These timestamps are the number of seconds elapsed since the Unix epoch (January 1, 1970 00:00:00 UTC), although precise, they are not intuitive to users.

Core Tool:stampToDateTag

To display these timestamps on the front-end page in a familiar and easy-to-read format, AnQiCMS provides a very practical template tag:stampToDate. This tag can convert timestamps to custom date and time strings.

Its basic usage method is very concise:{{stampToDate(时间戳变量, "格式字符串")}}

The key point is here格式字符串. The AnQiCMS template uses the unique Go language date and time formatting rules instead of the ones you might find in other systemsYYYY-MM-DDSuch symbols. Go language uses a fixed reference date and time as a template, and you need to construct the desired format through the components of this reference date.

This reference date is:2006年01月02日15时04分05秒. You can use the numbers in this reference date to represent the date and time units you want to output:

  • Year:2006
  • Month:01(representing the month number, pad with zeros if less than two digits) orJan(representing the English abbreviation of the month)
  • Date:02(representing the date number, with leading zeros if less than two digits)
  • hours:15(24-hour format) or03(12-hour format)
  • minute:04(with leading zeros if less than two digits)
  • seconds:05(with leading zeros if less than two digits)

You can combine these reference numbers with a separator such as-///:Combining with Chinese characters (such as/////) to generate various required date and time formats.

The following are some common format examples:

Format string Example output (for example, at 14:30:00 on October 26, 2023) Meaning
"2006-01-02" 2023-10-26 Year-Month-Date
"2006/01/02" 2023/10/26 Year/Month/Date
"2006年01月02日" 2023年10月26日 Chinese year-month-day
"01-02" 10-26 Month-Day
"15:04" 14:30 24-hour time:minute
"15:04:05" 14:30:00 24-hour format, time:minute:second
"2006-01-02 15:04:05" 2023-10-26 14:30:00 Year-Month-Day Hour:Minute:Second
"Mon Jan 2 15:04:05 MST 2006" Thu Oct 26 14:30:00 CST 2023 Go language standard format, including weekday, month abbreviation and timezone

Practical case: applying in article template

In the AnQiCMS article or product list (viaarchiveListtags), as well as on the article detail page (viaarchiveDetailtags), each article will haveCreatedTime(Article creation/publishing time) andUpdatedTimeThese two fields, (article last updated time), are provided in timestamp format.

Assuming you are looping through articles on a list page and have already assigned the article data toarchivesa variable, then in the loop bodyitemyou can format the time like this:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <article>
        <h3><a href="{{ item.Link }}">{{ item.Title }}</a></h3>
        <p>{{ item.Description }}</p>
        <div class="meta">
            <span>发布于:{{ stampToDate(item.CreatedTime, "2006年01月02日 15:04") }}</span>
            <span>更新于:{{ stampToDate(item.UpdatedTime, "2006-01-02") }}</span>
            <span>阅读量:{{ item.Views }}</span>
        </div>
    </article>
    {% empty %}
    <p>暂时没有文章。</p>
    {% endfor %}
{% endarchiveList %}

In the above example:

  • {{ stampToDate(item.CreatedTime, "2006年01月02日 15:04") }}The article creation time will be formatted as
  • {{ stampToDate(item.UpdatedTime, "2006-01-02") }}The article's update time will be formatted into a standard date format like “2023-10-26”.

Similarly, you can use it on the article detail page.archiveVariable to directly call the timestamp field of the current article:

<section>
    <h1>{{ archive.Title }}</h1>
    <div class="article-info">
        <span>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04") }}</span>
        <span>更新日期:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05") }}</span>
        <span>浏览次数:{{ archive.Views }}</span>
    </div>
    <div class="article-content">
        {{ archive.Content|safe }}
    </div>
</section>

Notes and tips

  1. Uniqueness of Go language formatPlease:

Related articles

How to display the friend link list in the admin panel of the AnQiCMS website?

On the AnQiCMS website, the display of the background management friends link list is a very practical feature. It not only helps to enhance the website's SEO effect, but also provides more valuable external resources for visitors.AnQiCMS provides a simple and efficient mechanism for managing and displaying these links, allowing even beginners to get started easily.

2025-11-07

How to get and display the detailed content and Banner image of a single page in the AnQiCMS template?

In AnQi CMS, it involves understanding the management methods of a single page and mastering the flexible application of template tags to retrieve and display the detailed content of a specific single page and its associated Banner image.AnQiCMS provides an intuitive admin interface and a powerful template engine, making this task simple and efficient. ### In-depth Understanding of AnQi CMS Single Page The "Single Page" feature of AnQi CMS is designed for pages with fixed structures, relatively independent content, and no frequent updates, such as "About Us", "Contact Information", "Service Introduction", and so on.

2025-11-07

How to display all single page lists on AnQiCMS template?

In website operation, we often need to create some independent pages, such as "About Us", "Contact Information", "Terms of Service", or some special introduction pages.These pages are usually called "single-page", their content is fixed, they do not belong to the conventional article or product categories, but they are an indispensable part of the website.When you have accumulated a large number of single pages on your website, you may wish to display a list of these single pages in a specific location, such as the bottom of the website, the sidebar, or even on a dedicated page, to facilitate browsing and searching for visitors.

2025-11-07

How to retrieve and display detailed information about a category, such as the category title, description, and thumbnail, in AnQiCMS?

In AnQi CMS, obtaining and displaying detailed information of a category is an indispensable part of building a dynamic website. Whether it is used to create a category list, a category special page, or display the information of the category in the article detail page, AnQi CMS provides simple and powerful template tags to achieve this. <h3>Deeply understand the `categoryDetail` tag</h3> AnQi CMS template system adopts a syntax similar to Django, where `categoryDetail`

2025-11-07

How to display custom field content (such as author, source) in AnQiCMS templates?

AnQiCMS with its flexible content model and powerful template tag system provides great convenience for personalized display of website content.In website operations, we often need to add some information outside of standard fields for articles, products, or other content, such as the author of the article, source of content, product batch number, or specific SEO information.This non-standardized information, AnQiCMS calls it 'custom fields', they can make your website content more professional, detailed, and meet specific business needs.

2025-11-07

How to implement lazy loading of image content in AnQiCMS templates to optimize performance?

In the digital age, the speed of website access has become one of the key indicators for measuring user experience and search engine rankings.Images are an important part of web page content and often also a major factor affecting page loading speed.When a page contains a large number of high-definition images, the browser needs to load all image resources before rendering the page, which undoubtedly greatly increases the user's waiting time.This problem was solved by the emergence of lazy loading (Lazy Loading) technology.The core idea of lazy loading images is to delay loading images that are not within the current viewport (i.e., the visible area of the user's current screen)

2025-11-07

How to automatically generate and display the content directory (ContentTitles) on the article detail page of AnQiCMS?

## How to automatically generate and display the content directory (ContentTitles) on the AnQiCMS article detail page??For long content, a clear table of contents (also known as "article outline" or "chapter navigation") can greatly enhance the reading experience of users.It not only helps readers quickly understand the structure of the article, but also allows them to directly jump to the chapters of interest, thereby improving the readability and user satisfaction of the content.

2025-11-07

How to get and display related article lists according to Tag ID in AnQiCMS template?

In a content management system, Tag (tag) plays an important role.They can not only help us flexibly organize content, but also associate articles with similar themes across different categories, and can effectively improve the website's internal link structure and user experience.When a user is interested in a specific topic, by clicking on a Tag, they can easily view all related articles.AnQiCMS provides a set of intuitive and powerful template tags that allow you to easily retrieve and display these related article lists based on Tag ID.

2025-11-07