How to correctly format timestamps in AnQiCMS templates so that content is displayed as needed?

Calendar 👁️ 75

In daily website content operation, time information plays a crucial role.Whether it is the publication date of the article, the launch time of the product, or the latest update of the content, these time points all directly affect the user's perception and trust in the information.However, in the templates of content management systems, we often encounter dates stored in the form of "timestamps", which are precise but not intuitive.How should we correctly format these timestamps in the AnQiCMS template so that the content can be displayed in the way we expect?

Understanding how AnQiCMS handles time information

In AnQiCMS, the content creation time (CreatedTimeAnd update time(UpdatedTime)are usually stored in databases in the form of standard Unix timestamps.These timestamps are typically 10 or 13 digits, representing the number of seconds or milliseconds since January 1, 1970, 0:0:0 (UTC) to the present.For website visitors, directly display1678886400Such numbers are obviously meaningless and unfriendly. To improve user experience, we need to convert them into the date and time format we are accustomed to reading in our daily lives, such as "March 15, 2023

AnQiCMS provides a very practical template tag for this——stampToDateIt can help us easily convert the original timestamp into various readable date and time formats.

stampToDateThe core usage of tags

stampToDateThe label usage is very concise and clear, its basic syntax is: {{stampToDate(时间戳, "格式")}}.

This is especially noteworthy, the date format string used by AnQiCMS is based on the special reference time of the Go language——2006-01-02 15:04:05This means that you cannot use placeholders as you would in other systemsYYYY-MM-DDInstead, you should use the corresponding numbers in the reference time to represent year, month, day, hour, minute, and second.

Let us understand this format through some specific examples:

  • Year (Year): Use2006Represent a four-digit year.
  • Month (Month): Use01orJanTo represent two-digit months or month abbreviations.
  • Date (Day): Use02or_2To represent a two-digit date or a single-digit date.
  • Hour (Hour): Use15(24-hour format) or03To represent an hour in 12-hour format.
  • Minute (Minute): Use04To represent a minute.
  • Seconds (Second): Use05to represent seconds.

The following are some common formatting examples and their expected outputs:

  • Display the full year, month, and day:
    • {{stampToDate(时间戳, "2006-01-02")}}2023-03-15
    • {{stampToDate(时间戳, "2006年01月02日")}}2023年03月15日
  • Display the year, month, and day with time:
    • {{stampToDate(时间戳, "2006-01-02 15:04")}}2023-03-15 14:30
    • {{stampToDate(时间戳, "2006/01/02 15:04:05")}}2023/03/15 14:30:00
  • Show only the month and date:
    • {{stampToDate(时间戳, "01-02")}}03-15
  • Only show the time:
    • {{stampToDate(时间戳, "15:04")}}14:30
    • {{stampToDate(时间戳, "03:04 PM")}}02:30 PM(12-hour AM/PM notation)

Having mastered this Go language reference time format, you can flexibly combine it according to your needs to display various complex date and time effects.

Apply in content template

Now we know how to usestampToDateNext, it is how to get the timestamp and format it in the actual content template of AnQiCMS.

Suppose we are developing a template for an article detail page that needs to display the article's publication time. In AnQiCMS, the context of the article detail page usually has aarchivean object that containsCreatedTimea field. We can format it like this:

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

here,archive.CreatedTimewill pass the timestamp of the article creation,"2006年01月02日 15:04"Then it defines the format we want it to display as "Year-Month-Day Hour:Minute".

If we need to traverse multiple articles and display their publish time on an article list page, we can do it like this:

<ul>
{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <li>
        <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
        <p>
            <span>分类:{% categoryDetail with name="Title" id=item.CategoryId %}</span>
            <span>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
            <span>浏览量:{{item.Views}}</span>
        </p>
        <p>{{item.Description}}</p>
    </li>
    {% empty %}
    <li>目前还没有内容发布。</li>
    {% endfor %}
{% endarchiveList %}
</ul>

In this example,item.CreatedTimeisforLoop to obtain the timestamp of each article and format it as年-月-日in a concise form.

Some practical skills and considerations

  • Maintain consistencyIn different pages and different types of content on the website, try to maintain consistency in the date and time format to avoid confusing users.
  • Consider multilingual support: If your website supports multilingual, you may need to use date formats that are in line with local customs in different language versions. AlthoughstampToDateIt does not directly provide multilingual localization, but you can combine conditional judgment or the multilingual support feature of AnQiCMS in the template logic.
  • Processing without time setting: If a content's time field may be empty, it may cause problems during template rendering. In actual development, it can be combined withifDetermine to ensure that formatting and display only occur when the timestamp exists. For example:
    
    {% if archive.CreatedTime %}
        <p>发布于:{{stampToDate(archive.CreatedTime, "2006年01月02日")}}</p>
    {% else %}
        <p>发布时间未知</p>
    {% endif %}
    

By flexible applicationstampToDateTags and Go language date formatting rules, you can easily implement various date and time display as needed in the AnQiCMS template, thus providing your website users with a more friendly and professional browsing experience.


Frequently Asked Questions (FAQ)

Q1: Why is the time formatting string of AnQiCMS not common?Y-m-doryyyy-MM-dd?A1: AnQiCMS is developed based on Go language, it follows the unique specification of Go language in date and time formatting. Go language does not use symbol placeholders, but uses a specific reference date2006-01-02 15:04:05Also known as "Go time" or "reference time" to define various formats.This means that you need to provide the numbers or text in the format string that correspond to the reference time you want to output.For example, to display a four-digit year, use the year from the reference time2006.

Q2: How can I display the publication year of the content only?A2: If you want to display only the publication year, you can setstampToDatethe format parameter of the tag to `"

Related articles

How to use the (for) loop tag in AnQiCMS template to traverse and display the content list?

Efficiently using the `for` loop tag in AnQiCMS templates to display content lists is an indispensable core skill for building dynamic websites.By flexibly using the `for` loop, we can easily traverse various types of data collections and display them in a customized style on the website page, whether it is a news list, product display, category navigation, or any other repetitive content block, the `for` loop can help you a lot.

2025-11-07

How to use conditional judgment (if/else) tags to control the display logic of content in AnQiCMS templates?

In the development and maintenance of website templates, we often encounter the need to display or hide different content based on specific conditions.This dynamic content display logic is crucial for improving user experience, realizing personalized functions, and optimizing the website structure.AnQiCMS (AnQiCMS) is a powerful content management system that provides a flexible template engine, among which the condition judgment (`if/else`) tags are the core tools to achieve this goal.

2025-11-07

How to implement pagination for document lists in AnQiCMS and support custom page number quantities?

AnQiCMS document list pagination and custom page number settings: Make your content management more efficient Imagine, when your website content becomes rich, with thousands of articles, products, or news lists neatly arranged, if you cannot organize and display them effectively, users may feel at a loss.A good pagination mechanism not only improves user experience and helps visitors quickly find the information they need, but is also an important aspect of SEO optimization.

2025-11-07

How to call and display friend links in AnQiCMS templates?

Friendship links, as part of the website content ecosystem, not only help to improve search engine rankings and optimize website weight, but also provide users with more navigation to related resources.In AnQiCMS, managing and displaying friend links is a direct and flexible task, even if you do not have a deep programming background, you can easily achieve it. ### Backend Management: Easily Configure Friend Links In the AnQiCMS backend management interface, you will find the friend link settings under the "Function Management" menu.

2025-11-07

How to define and use variables in AnQiCMS templates to optimize the display and management of content?

In AnQi CMS, flexibly using template variables is the key to optimizing website content display and management efficiency.It not only makes your website content more dynamic, but also greatly improves the maintainability and reusability of the template.This article will delve into how to define and use variables in AnQiCMS templates, as well as how to better present content through these techniques. ### Variables: The Foundation of Living Content Imagine how cumbersome it would be if every title, every paragraph on a website had to be manually modified.The introduction of variables is to solve this pain point

2025-11-07

How to reuse common code snippets through the `include` tag in AnQiCMS templates to optimize the display structure?

When building and managing websites, improving efficiency and maintaining code neatness is the goal pursued by every operator.AnQiCMS (AnQiCMS) as an efficient content management system, provides powerful template functions, among which the `include` tag is the tool to achieve this goal.It allows us to abstract out repeated code snippets from the website, forming independent modules, and then referencing them where needed, greatly optimizing the template structure and subsequent maintenance work.

2025-11-07

How to implement page layout inheritance and rewriting using the `extends` tag in AnQiCMS templates?

When building a website, maintaining the uniformity of the page style, improving development efficiency, and reducing maintenance costs is the goal pursued by every website operator.AnQiCMS (AnQiCMS) deeply understands this, and its template system design draws on the excellent ideas of the Django template engine, among which the `extends` tag is one of the core functions to achieve this goal. ### The Core Concept of Template Inheritance Imagine you are designing a series of posters.Each poster has a common brand logo, top title, and bottom contact information, with only the promotional content in the middle being different

2025-11-07

How to set the automatic compression and thumbnail processing method in AnQiCMS, which affects the image display on the front end?

The quality of website content is often not just reflected in text, but images as visual elements also play a crucial role.In website operation, how to efficiently handle images, ensuring that the images are clear and beautiful while also considering the loading speed, this is the core value of AnQiCMS image processing function.Today, let's delve deeper into how AnQiCMS helps us manage the automatic compression and thumbnail processing of large images, as well as how these settings will affect the display effect of images on the website's frontend.### Say goodbye to slow image loading

2025-11-07