How to format a timestamp into a readable date and time format for display in AnQiCMS?

Calendar 👁️ 66

The website content is updated frequently, each article, product, or comment has its creation and update time.However, these times are usually stored in the background in the form of a string of numbers - that is, what we commonly call timestamps.It is not friendly to visitors to directly display these timestamps; we need to convert them into a clear date and time format.

In AnQiCMS, formatting these timestamps into the date and time format we are accustomed to is actually very simple and intuitive.The system has a powerful template tag built-in that can help us easily achieve this.

Core Tool:stampToDateTag

AnQiCMS provides a namedstampToDateA template tag specifically used to convert Unix timestamps (usually 10 or 13-digit integers) into readable date and time strings. This tag is very flexible and requires two key pieces of information:

  1. Formatted timestamp:This is usually what you get from the databaseCreatedTimeorUpdatedTimefields.
  2. Formatting string:This is the key to define the style of date and time display you want.

Its basic syntax is as follows:{{stampToDate(时间戳, "格式化字符串")}}

It needs to be particularly explained that "formatting strings". AnQiCMS is developed based on Go language, therefore it follows the unique time formatting rules of Go language.used with many other programming languagesY-m-d H:i:ssuch placeholders are different, Go language uses a specific oneReference timeto serve as a template:2006-01-02 15:04:05. Just remember this reference time and replace the corresponding part of the reference time with the style you want to display.

For example:

  • If you want to display2023-10-26Then the format string is"2006-01-02".
  • If you want to display2023年10月26日Then the format string is"2006年01月02日".
  • If you want to display2023-10-26 14:30:15Then the format string is"2006-01-02 15:04:05".

Application scenarios in practice

ThisstampToDateThe tag can be used anywhere in the AnQiCMS template.No matter whether you are displaying an article list, article details, the user's recent login time, or the comment posting time, whenever you encounter a timestamp field, you can use it to beautify the display.

For example, on the article detail page (usually correspondingarchiveDetailto the tag data obtained) display the article's publishing and update time:

<article>
    <h1>{{ archive.Title }}</h1>
    <p>
        <span>发布于:{{stampToDate(archive.CreatedTime, "2006年01月02日 15:04")}}</span>
        <span>更新于:{{stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05")}}</span>
    </p>
    <div>
        {{ archive.Content|safe }}
    </div>
</article>

On the article list page (usingarchiveListThe data label is used to display the creation date of each article:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <li>
        <a href="{{item.Link}}">
            <h5>{{item.Title}}</h5>
            <p>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</p>
        </a>
    </li>
    {% empty %}
    <li>暂无文章内容。</li>
    {% endfor %}
{% endarchiveList %}

Display the comment publish time in the comment list:

{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
    {% for item in comments %}
    <div>
        <span>{{item.UserName}}</span> 于 <span>{{stampToDate(item.CreatedTime, "2006年01月02日 15:04")}}</span> 评论道:
        <p>{{item.Content}}</p>
    </div>
    {% endfor %}
{% endcommentList %}

More flexible format customization

The reference time format of the Go language is very powerful, you can combine its elements to meet various complex date and time display needs:

  • Show the day of the week:UseMon(abbreviation for Monday) orMonday(Monday full name).
    • For example:"2006年01月02日 Monday"Will be displayed2023年10月26日 Thursday.
  • Show AM/PM:UsePMorpm.
    • For example:"15:04 PM"Will be displayed02:30 PM(2:30 PM).
  • Only show month and year:
    • For example:"2006年01月"Will be displayed2023年10月.

You can freely combine these reference elements to create a unique date and time format according to your needs.

Other date/time related tags and filters

exceptstampToDate,AnQiCMS also provides some other auxiliary functions related to date and time:“

  • Display the current system time:“If you need to display the current server time on the page, you can use{% now "格式化字符串" %}tags. It also follows the formatting rules of the Go language.
    
    <p>当前时间:{% now "2006年01月02日 15:04:05" %}</p>
    
  • dateFilter:for those who are already using Go languagetime.TimeType variables, you can also usedateFormatter. But please note,dateFormatter only applies totime.TimeThe type, used directly on integer timestamps will cause an error. Most of the time data in AnQiCMS templates are provided in timestamp format, thereforestampToDateIs the preferred method for handling timestamps.

In summary, AnQiCMS is intuitive.stampToDateTags and the flexible reference time format of Go language make the time display of website content simple and expressive.Master this little trick, and it will make your website content more readable and professional.


Frequently Asked Questions (FAQ)

1. Why does AnQiCMS use the number combination '2006-01-02 15:04:05' as the formatting string when formatting dates?

This is the special time formatting method of Go language. It is different from the use of symbols (such asYrepresents the year,mrepresenting the month) in many programming languages, where Go language provides a specific reference time2006-01-02 15:04:05(That is, at 3:04:05 PM on January 2, 2006) as the formatted "template".You just need to write the desired display format according to the meaning of the numbers in this reference time, and the system will automatically apply it to the actual timestamp.For example, to display the full year, use2006.

2. How can I set the format to display the day of the week or AM/PM?

To display the day of the week, you can use the reference time in theMon(abbreviation for Monday) orMondayRepresenting the full name of Monday. For example,"2006年01月02日 Monday"It will display `2023

Related articles

How to use if/for tags in a template to control the conditional display and looping display of content?

When building and operating a website, the dynamic display and flexible management of content are key to improving user experience and operational efficiency.AnQiCMS (AnQiCMS) understands this point, therefore its template engine provides powerful and intuitive conditional judgment (`if`) and loop traversal (`for`) tags, allowing you to easily control the display logic of content and achieve highly customized page layouts.The template syntax of AnQi CMS borrows the concise style of Django template engine, with a very low learning cost.By mastering these core tags

2025-11-08

How to apply AnQiCMS pagination feature to the pagination display of article lists or Tag document lists?

In website content management, whether it is to display a large number of articles, products, or documents under categories, an efficient and user-friendly pagination function is crucial.It not only allows users to easily browse a large amount of content, avoid the slow loading caused by long pages, but also helps search engines better crawl and index website information.AnQiCMS is well-versed in this, providing intuitive and powerful pagination functions in template tag design, making it effortless to implement pagination display in article lists or Tag document lists.Understanding how AnQiCMS implements pagination

2025-11-08

How to display the friend link list at the bottom of the AnQiCMS website?

In website operation, friendship links play an indispensable role.It is not only an effective way for websites to recommend and share traffic with each other, but also an important means to enhance the weight and visibility of websites in search engines.However, how to conveniently manage and display these links on a website is a practical problem faced by many website managers.AnQiCMS as a system focusing on enterprise-level content management and SEO optimization, provides an intuitive link function and a flexible template calling mechanism, making this process extremely simple.###

2025-11-08

How does AnQiCMS display the website message form and customize the form field display?

AnQi CMS: Flexible display and field customization of the website message form In website operation, the message form is an important bridge connecting users and the website.Whether it is to collect customer feedback, provide consulting services, or interact with users, a well-functioning and easy-to-use comment form is particularly important.AnQiCMS (AnQiCMS) is well-versed in this field, providing users with a powerful and flexible feedback form feature, which can be easily displayed on the website front-end and supports in-depth customization of form fields to meet various business needs.### One,

2025-11-08

How to define temporary variables in templates to assist in content display logic?

When using AnQiCMS for website content management, the flexibility of the template is crucial for achieving diverse content display.At times, we need not only to output data directly, but also to perform some temporary processing, calculation, or judgment based on business logic, at which point defining temporary variables in the template becomes particularly useful.This not only makes the template code more tidy, but also avoids repeated retrieval or calculation of the same data, thereby improving rendering efficiency and maintainability.The template engine syntax of AnQi CMS is similar to Django template engine

2025-11-08

How to reference the common template code snippets (such as header and footer) in AnQiCMS to unify the display of the page?

It is crucial to maintain a unified page style in website content operation to enhance user experience and maintenance efficiency.Imagine if your website had hundreds or even thousands of pages, and each page's header, footer, or sidebar needed to be manually modified, that would be an enormous project.Fortunately, AnQiCMS provides a powerful and flexible template mechanism that can help you easily refer to and manage common code snippets, ensuring the consistency of the website display.AnQiCMS's template system borrows the syntax of the Django template engine, making it easy to use and powerful.it passes `

2025-11-08

How to use the template inheritance feature of AnQiCMS to build a consistent page skeleton and display local content?

During the process of website construction and operation, how to efficiently manage page layout, ensure consistency in visual style, and be able to flexibly adjust local content is a challenge faced by many operators.AnQiCMS provides a powerful template inheritance function, which can help us cleverly solve these problems, realize the structural management of website pages and the flexible display of content.Understand the core concept of template inheritance Imagine that template inheritance is like a blueprint or master template in architectural design.We first design a universal page skeleton, which includes the shared elements of all web pages

2025-11-08

How does AnQiCMS use static caching technology to improve page loading speed and content display efficiency?

In today's fast-paced online world, the loading speed and display efficiency of websites have become key indicators of user experience and search engine rankings.A responsive website can not only effectively retain visitors but also stand out in fierce competition.AnQiCMS (AnQiCMS) understands this, providing strong support for users to achieve fast website access and efficient content presentation through its built-in static caching technology.Understand static caching: The core mechanism of website acceleration To understand how AnQi CMS works, you first need to understand what static caching is

2025-11-08