How can you format a timestamp to display the publication date in a friendly date and time format?

Calendar 78

In website operation, the content release date is not just a time mark, but also an important basis for visitors to obtain information timeliness.A clear and readable date format that can significantly improve user experience and make your website content appear more professional and considerate.

However, in such a powerful content management system as AnQiCMS, the publish date is usually stored in the database in the form of a 'timestamp', which is usually a string of numbers that is difficult to understand directly. For example, you might see1609470335Such numbers, rather than the ones we are accustomed to2021年1月1日At this point, we need to format these timestamps into a human-friendly date and time format.

Luckyly, Anqi CMS provides an extremely convenient and flexible built-in template tag for this, making the process effortless.

Say goodbye to the ocean of numbers: to understandstampToDateTag

The Anqi CMS template system adopts a syntax similar to the Django template engine, one of the core highlights beingstampToDateThe label is specifically designed to convert timestamps into the familiar date and time format.

Its basic usage is very intuitive:

{{ stampToDate(时间戳, "格式") }}

There are two key parts:

  1. Timestamp:This is the original timestamp you obtain from the content data, which is usually a 10-digit or 13-digit number. In the Anqi CMS template, the publication time of articles or products is usually throughitem.CreatedTimeObtain, the update time isitem.UpdatedTime. These fields will directly provide timestamp values.

  2. Format:This is a string used to inform the system of the specific format you want the date and time to be displayed in.The AnQi CMS is developed based on the Go language, therefore, the format strings here follow the unique date and time formatting conventions of the Go language.

Understand the "magic numbers" of date formatting in Go language

The Go language has a very clever convention in date and time formatting: it does not use placeholders similar toY-m-dbut instead uses a fixed reference date and time"2006-01-02 15:04:05"—To construct the format. You just need to replace the year, month, day, hour, minute, second, and other elements in this reference date with the display method you want, and the system will automatically recognize and apply it.

Let's understand the usage of this 'magic number' with some examples:

  • Display the year, month and date (e.g., 2023-10-26)If you wish to display年-月-日The format, it can be written like this:"2006-01-02".

    • {{ stampToDate(item.CreatedTime, "2006-01-02") }}It may output2023-10-26
  • Chinese-friendly format (e.g., October 26, 2023)If you want a format that is more in line with Chinese reading habits, just replace the hyphen in the reference date with Chinese:"2006年01月02日".

    • {{ stampToDate(item.CreatedTime, "2006年01月02日") }}It may output2023年10月26日
  • Contain time (for example: 2023-10-26 14:30)Do you want to be precise to hours and minutes? Add the 'magic number' to the time part of the format string:"2006-01-02 15:04".

    • {{ stampToDate(item.CreatedTime, "2006-01-02 15:04") }}It may output2023-10-26 14:30
  • Show only the time (e.g., 14:30:55)If you only need to display the time, the format can be simplified to:"15:04:05".

    • {{ stampToDate(item.CreatedTime, "15:04:05") }}It may output14:30:55
  • Custom delimiter (e.g., 2023/10/26)You can freely change the separator between date elements according to your needs:"2006/01/02".

    • {{ stampToDate(item.CreatedTime, "2006/01/02") }}It may output2023/10/26

The mystery of this 'magic number' lies in,2006represents the year,01represents the month,02represents the date,15Represents hours in a 24-hour clock system,04represents the minute,05Represents seconds. Just remember this date and time and use it to "build" any output format you want.

Hands-on practice: Apply it to your Anqi CMS template.

Now, let's see how to apply in actual Anqi CMS templatesstampToDate.

Suppose you are editing a template for an article detail page (for examplearticle/detail.htmlorarchive/detail.html), and hope to display the publication date and update date of the article:

{# 假设当前页面的文章数据可以通过 archive 变量直接访问 #}
<article>
    <h1>{{ archive.Title }}</h1>
    <div class="post-meta">
        <span>发布于:{{ stampToDate(archive.CreatedTime, "2006年01月02日 15:04") }}</span>
        {# 您可能希望更新时间只显示日期,或者只在有更新时才显示 #}
        {% if archive.UpdatedTime and archive.UpdatedTime != archive.CreatedTime %}
        <span>最近更新:{{ stampToDate(archive.UpdatedTime, "2006-01-02") }}</span>
        {% endif %}
    </div>
    <div class="post-content">
        {{ archive.Content|safe }}
    </div>
</article>

If you are on the article list page (for examplearticle/list.htmlorarchive/list.html),need to iterate through multiple articles and display their publication dates, the situation is also simple:

`twig {% archiveList archives with type=“page” limit=“10” %}

{% for item in archives %}
    <div class="archive-item">
        <h2><a href="{{ item.Link }}">{{ item.Title }}</a></h2>
        <p class="summary">{{ item.Description }}</p>
        <div class="info">
            <span>分类:{% categoryDetail with name="Title" id=item.CategoryId %}

Related articles

How to control the display of different styles for odd and even rows in a table or list loop?

In web design and content presentation, in order to enhance the user reading experience and make the information presentation clearer, we often need to apply different styles to the odd and even rows in tables or lists.This visual distinction not only breaks the monotony of the content, but also significantly improves the readability of the data.In AnQiCMS, with its flexible and powerful template engine, achieving this effect is quite direct and convenient.

2025-11-09

How can you determine if a variable exists in a template and dynamically display content based on the result?

Build an energetic website on AnQiCMS, content management is not just about publishing information, but also about how to intelligently present these information.Imagine if your website could flexibly adjust the display style based on the actual data, for example, showing an image if there is one, and displaying alternative text if there isn't;If there is relevant content, recommend it; if not, kindly prompt The key to realizing this intelligent dynamic display lies in how to determine whether a variable exists in the template

2025-11-09

How to implement showing or hiding part of the content based on user group permissions (such as VIP exclusive content)?

In website operation, providing differentiated content services based on user identity is a common strategy to enhance user experience and achieve content monetization.AnQiCMS (AnQiCMS) with its flexible user group management and VIP system, can help us easily implement content display or hide based on user permissions.This article will delve into how to use this feature of Anqi CMS to create exclusive VIP content for your website or display different information based on user level.

2025-11-09

How to use the `archiveList` tag to sort and display the article list by views or publication time?

How to efficiently display and organize content in website operation often directly relates to user experience and the effectiveness of content dissemination.AnQiCMS provides a series of powerful and flexible template tags that allow content managers to easily control the display of articles.Today, let's talk about how to use the `archiveList` tag to sort and display the article list by views or publishing time.### Understanding the `archiveList` tag The `archiveList` tag is AnQiCMS

2025-11-09

How to customize the display of Banner Carousel images on the website homepage?

In website operation, the homepage Banner carousel image is an important component to attract visitors' attention and convey core information.AnQiCMS (AnQiCMS) understands this need and provides a flexible and convenient way for you to easily customize and display these key visual elements on the homepage of your website. ### Prepare: Manage Your Banner Image in the Background First, we need to make sure that the images used for the carousel have been uploaded and properly managed.The AnQi CMS usually provides a special "Banner Management" area in the background

2025-11-09

How to display a list of friendship links on the front page of the website?

In website operation, friendship links have always been an important means to enhance website authority, increase external traffic, and enhance user trust.If you are using AnQiCMS to manage your website and want to display a list of friend links on the frontend page, you will find that this process is surprisingly simple and efficient.AnQi CMS, with its simple design and powerful features, makes content management easy, and the display of friendship links is no exception.Next, let's take a look at how to display friend links in Anqi CMS.

2025-11-09

How to use breadcrumb navigation tags to display the current page path at the top of the page?

In website operation, a clear navigation path is crucial for user experience and search engine optimization.When a user is browsing deep pages on a website, a straightforward 'You are here' prompt can help them quickly locate and easily return to the parent directory.This is a kind of auxiliary navigation method, which we call breadcrumb navigation.AnQiCMS (AnQiCMS) is built-in with powerful breadcrumb navigation tags, allowing developers and content operators to easily display the current location path at the top of the page.What is breadcrumb navigation and why is it important?

2025-11-09

How to display the content, title, and image of a single page in a custom page?

In website operation, we often need to create unique display methods for specific content, such as designing a unique landing page for product features, service introductions, or company profiles.AnQiCMS (AnQiCMS) with its flexible content model and highly customizable features, makes everything very simple.Today, let's talk about how to easily display the content, title, and images of a custom page, making your website content more personalized.

2025-11-09