How to format the timestamp of an article into a readable date and time format for display?

Calendar 👁️ 75

In website content operation, the publication time or update time of the article is an important element in conveying information to visitors.A clear and readable date-time format can not only improve the user experience but also allow visitors to quickly understand the timeliness of the content.AnQi CMS provides a very practical template tag to help us easily achieve this goal.

Core Tool:stampToDateTag

AnQi CMS allows us to go throughstampToDateLabel, format the original timestamp stored in the article (usually a 10-digit number) into any date-time style we need. Its basic usage is very intuitive:

{{ stampToDate(时间戳变量, "格式化字符串") }}

Here时间戳变量Is the field storing the timestamp in the article, such as the creation time of the articleCreatedTime) or update time (UpdatedTime).格式化字符串Is the key that defines how you want the time to be displayed

Master the date and time formatting rules of the Go language

While usingstampToDatetags in the template,格式化字符串The syntax may be familiar to us in everyday lifeYYYY-MM-DDor%Y-%m-%dis different, as it is defined based on the unique 'reference time' of the Go language. The Go language adopts a fixed point in time:January 2, 2006 3:04:05 PMAs a reference for all datetime formatting strings.

This means that when you want to display a date and time element, you need to use the corresponding number of the reference time to construct your format string. The following are some common format comparisons:

  • Year:2006(reference year)
  • Month:01(reference month, with leading zero) or1(without leading zero)
  • Date:02(reference date, with leading zero) or2(without leading zero)
  • hours:15(24-hour format) or03(12-hour format with leading zero) or3(12-hour format without leading zero)
  • minute:04(with leading zero) or4(without leading zero)
  • seconds:05(with leading zero) or5(without leading zero)
  • Week:Mon(abbreviation of the day of the week) orMonday(full name of the day of the week)

For example, if you want to display 'Year-Month-Day Hour:Minute', the corresponding format string is"2006-01-02 15:04".

Practical application: Formatting time in article list and detail pages

In AnQi CMS, the creation time of articles is usually stored inCreatedTimefield, and the update time is stored inUpdatedTimefield. Whether it is an article list page or an article detail page, you can view similaritem.CreatedTime(When looping through the article list) orarchive.CreatedTime(When directly accessing the article detail page) to obtain the timestamp, then combinestampToDateFormat labels.

Here are some examples of formatting article time in templates:

Example one: on the article list page(archiveListin the loop)

Suppose you are usingarchiveListTag循环显示文章列表,and hope each article shows the publish date and update date.

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

Example two: On the article detail page

On the article detail page, we usually display more detailed date and time information, including even the day of the week.

<article>
    <h1>{{ archive.Title }}</h1>
    <div class="article-meta">
        <span>发布日期:{{ stampToDate(archive.CreatedTime, "2006年01月02日 星期一") }}</span>
        <span>最后更新:{{ stampToDate(archive.UpdatedTime, "2006/01/02 15:04:05") }}</span>
    </div>
    <div class="article-content">
        {{ archive.Content|safe }}
    </div>
</article>

By using these simple code snippets, you can make the article's time display more humanized.

It is worth mentioning that if you only need to display directly on the article detail pageCreatedTimeorUpdatedTime,archiveDetailThe tag itself supportsformatParameters are formatted, for example:{% archiveDetail with name="CreatedTime" format="2006-01-02" %}But this is limited to directly calling these fields, for other timestamp variables in the template,stampToDateTags are still a general and powerful choice.

Customization and flexibility

stampToDateThe strength of the tag lies in its flexibility. You can freely combine the reference time elements of the Go language according to the design style of the website and the reading habits of the user group, creating a unique date and time format.Whether it is a concise

By mastering the use ofstampToDateTags and Go language date and time formatting rules, you can make the time information on the website clearer and more professional, thereby enhancing the overall user experience.


Frequently Asked Questions (FAQ)

Why is the format string"2006-01-02"instead of"YYYY-MM-DD"?

Answer: An

Related articles

How to call the cover main image, thumbnail, and group image to enrich the display effect?

In website operation, images are indispensable elements to attract visitors and convey information.Reasonably using the cover image, thumbnail, and group image of the article can greatly enhance the visual appeal of the website, enrich the form of content presentation, and make your content more vivid and attractive.AnQiCMS as a flexible and efficient content management system provides an intuitive way to manage and call these image resources, helping you easily achieve diverse image display effects.Understanding the image types in AnQiCMS

2025-11-08

How to truncate overly long content on the article detail page and add an ellipsis?

In website operation, we often encounter such situations: an article is rich in content, but when it is displayed on the list page, the recommendation area, or some abstract module on the article detail page, we only want to display a part of the content and end it with an ellipsis to maintain the neatness of the page and the fluency of reading.If the entire content is displayed directly, the page will appear lengthy, affecting user experience.Luckyly, AnQiCMS provides us with a very flexible and powerful template mechanism, allowing us to easily truncate long content and elegantly add ellipses to enhance the overall page effect

2025-11-08

How to precisely control which document IDs, titles, and links are displayed on the article detail page?

In website operation, the article detail page is the core touch point for users to obtain information, and its way of information presentation directly affects user experience and the transmission of content value.AnQiCMS as a flexible content management system provides powerful and fine-grained control capabilities, allowing you to accurately display the required document ID, title, link, and other information on the article detail page. To precisely control the content of the article detail page, we mainly rely on the template tag system of AnQiCMS.The core lies in the `archiveDetail` tag

2025-11-08

How to integrate Markdown editor into a website and correctly display mathematical formulas and flowcharts?

In today's increasingly rich digital content, websites are no longer just a pile of text and images.For users who need to display technical documents, academic papers, data analysis reports, and even complex business processes, how to efficiently and beautifully present mathematical formulas and flowcharts has become a key factor in enhancing the professionalism and user experience of the content.AnQi CMS is well-versed in this, providing powerful Markdown editor integration capabilities, with simple frontend configuration, allowing your website to easily support the display of these advanced content.

2025-11-08

How to display associated categories and tags lists on the article detail page?

When managing website content in AnQi CMS, the article detail page is the core page for users to obtain information.In addition to the content of the article itself, displaying the category information and related tag list of the article to the visitor can greatly improve the user experience, help visitors quickly understand the context of the article, and effectively enhance the internal link structure of the website, which is greatly beneficial for search engine optimization (SEO).Next, we will discuss how to flexibly display these related information on the article detail page of Anqi CMS.AnQi CMS uses an intuitive and powerful template tag system

2025-11-08

How to filter tags through document parameters to achieve a combination display of list content with multiple conditions?

In content operation, we often need to display lists of content with different themes and properties, and allow visitors to filter according to their own needs. For example, a real estate website may need to filter listings by "house type", "location", "area range", and other multi-dimensional combinations.If each time is organized manually or developed custom, it is not only inefficient but also difficult to maintain.AnQiCMS (AnQiCMS) provides a flexible and powerful document parameter filtering mechanism that can help us easily achieve this combination display of list content with multiple conditions. Next

2025-11-08

How to implement pagination for article lists and limit the number of items per page?

In website operation, especially when managing a large amount of content, the pagination display of the article list is an indispensable function.It not only makes it easier for users to browse content, avoids the problem of too much data loading on a single page leading to slow speed, but also improves the overall user experience and SEO friendliness of the website.In AnQi CMS, it is very intuitive to implement pagination for the article list and flexibly control the number of items per page. Next, we will explore how to implement pagination for the article list in the Anqi CMS template and limit the number of articles displayed per page.--- ### Efficient Content Management

2025-11-08

How to display related documents or search results on the article list page?

In website operation, how to make visitors find the content they are interested in faster, or seamlessly discover more related information after browsing an article, is the key to improving user experience and website stickiness.AnQiCMS (AnQiCMS) provides powerful and flexible template tags that can help us easily implement displaying related documents or search results on the article list page, thus building a more intelligent and interactive website.### Smartly using content list tags to present dynamic information One of the core advantages of AnQi CMS lies in its highly customizable template system

2025-11-08