The `stampToDate` filter: How to format Unix timestamp in AnQiCMS template?

Calendar 👁️ 69

In AnQiCMS template design, the way data is displayed is crucial to user experience.Especially information such as dates and times, if presented in the original Unix timestamp format, is difficult for ordinary visitors to understand.Fortunately, AnQiCMS provides a very practical filter -stampToDateIt can help us easily convert these machine-readable number sequences into clear and friendly date and time formats.

Understanding Unix timestamp withstampToDateFilter

First, get a basic understanding of what a Unix timestamp is.It is essentially the number of seconds elapsed since 00:00:00 UTC on January 1, 1970.In the database, for storage and calculation efficiency, dates and times are often saved in this format.For example, the release time of the document (CreatedTime) or update time(UpdatedTimeIt is usually in the form of a Unix timestamp.

In the AnQiCMS template, if you output these timestamps directly, you will see a long string of numbers, such as1609470335HoweverstampToDateThe filter acts like a translator, its task is to 'translate' this string of numbers into the date and time format we are accustomed to reading.

UsestampToDateThe basic syntax of the filter is very intuitive:{{stampToDate(时间戳, "格式")}}

There are two key parts:

  1. timestamp: This is the Unix timestamp you want to format, usually a variable in the template (such asitem.CreatedTime). Please note that it needs to be a 10-digit number representing seconds.
  2. Format: This is a string used to tell the filter the specific format you want the date and time to be displayed in.

Master the date formatting magic of the Go language.

stampToDateThe filter uses the date formatting rules specific to the Go language, which is different from PHP'sY-m-dor Python's%Y-%m-%dDifferent. The formatting of Go language is not represented by letter codes for year, month, and day, but by a fixed reference time2006年01月02日15时04分05秒With time zone and milliseconds. Just remember the parts of this 'magic number' and then replace them with the format you want.

Let us understand this format through some common examples:

  • Only show the year: The year in the reference time is2006Replace2006. For example:{{stampToDate(publishStamp, "2006")}}-u003e2021
  • Show year-month-date: The reference time is2006-01-02Replace2006-01-02. For example:{{stampToDate(publishStamp, "2006-01-02")}}-u003e2021-06-30
  • Show year/month/day: The reference time is2006-01-02Replace2006/01/02. For example:{{stampToDate(publishStamp, "2006/01/02")}}-u003e2021/06/30
  • Show a date with Chinese: The reference time is2006-01-02Replace2006年01月02日. For example:{{stampToDate(publishStamp, "2006年01月02日")}}-u003e2021年06月30日
  • Show hour:minute:second: The reference time is15:04:05Replace15:04:05. For example:{{stampToDate(publishStamp, "15:04:05")}}-u003e12:00:00
  • Show the full date and time: The reference time is2006-01-02 15:04:05Replace2006-01-02 15:04:05. For example:{{stampToDate(publishStamp, "2006-01-02 15:04:05")}}-u003e2021-06-30 12:00:00

The key is that you need to remember2006-01-02 15:04:05This skeleton, then according to the format you need to “assemble” a new format string. For example, if you want月/日/年the format, you write01/02/2006.

stampToDateactual application

In AnQiCMS, you will often use date and time display in places like article lists, article details, comment lists, etc. For example, when you traverse a document list (archiveListWhen each document item (itemusually includesCreatedTime(creation time) andUpdatedTimeThe (update time) field. These fields usually store Unix timestamps.

Suppose you are designing an article list page, hoping to display the publication date of each article:

{# 假设 publishStamp 是一个Unix时间戳变量,例如 1609470335 #}
{% set publishStamp = 1609470335 %}

<p>格式化为 2021年06月30日: {{stampToDate(publishStamp, "2006年01月02日")}}</p>
<p>格式化为 2021-06-30: {{stampToDate(publishStamp, "2006-01-02")}}</p>
<p>格式化为 2021/06/30 12:00:00: {{stampToDate(publishStamp, "2006/01/02 15:04:05")}}</p>
<p>格式化为 30/06/2021: {{stampToDate(publishStamp, "02/01/2006")}}</p>
<p>格式化为 12:00:00: {{stampToDate(publishStamp, "15:04:05")}}</p>

It will be like this in a loop of article lists:

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

In this code block,item.CreatedTimeIt is a Unix timestamp,stampToDateThe filter converts it into年-月-日 时:分which makes the date display of the article list more friendly and readable.

Points to note

  • The reference time in Go language is crucial: Always in2006-01-02 15:04:05Use it as a baseline to construct your format string. Remember that this is not a custom placeholder, but a specific date value in the Go language.
  • The number of bits in a Unix timestamp.:stampToDateThe filter expects to receive a 10-digit Unix timestamp (second level). If your timestamp is 13 digits (millisecond level), you need to divide it by 1000 first, for example{{stampToDate(item.CreatedTime / 1000, "格式")}}.
  • Avoid usingdateFilter confusion: AnQiCMS template may still contain a nameddate.dateFilters are typically used to format the native Go language'stime.TimeThe type is an object, not the raw Unix timestamp. If you pass a Unix timestamp

Related articles

`date` filter: How to format a GoLang `time.Time` type into a specific date string?

In AnQi CMS template development, flexibly displaying dates and times is an indispensable part of content presentation.You may often need to present date information in the system or content in a specific format, such as "YYYY-MM-DD" or "MM/DD HH:MM" and the like.At this point, the `date` filter has become your powerful assistant.

2025-11-08

How to convert a numeric string to an integer or floating-point number type in AnQiCMS template?

In AnQiCMS template development, we often encounter situations where we need to handle various types of data.Sometimes, data obtained from a database or through user input, even if they represent numbers, may be treated as strings at the template layer.In this case, if direct mathematical operations or numerical comparisons are performed on these 'number strings', unexpected results or even errors may occur.Therefore, understanding how to convert a numeric string to an integer or floating-point number type in the AnQiCMS template is crucial for ensuring the accuracy of data processing and the correctness of logic

2025-11-08

The role of `escape` and `escapejs` filters in preventing XSS attacks or handling special characters?

In website content operation, the display method and security of the content are equally important.AnQi CMS is an efficient content management system that provides comprehensive considerations for website security, among which the `escape` and `escapejs` filters are important tools for us to combat network attacks and ensure the correct display of content.Understanding their role can help us better manage and publish content.

2025-11-08

The `safe` filter: when to use, what potential security risks need to be paid special attention to?

During the template development process of Anqi CMS, the way content is displayed is flexible and diverse, and one of the very important filters is `safe`. Understanding the working principle of the `safe` filter, when to use it, and the security considerations it may bring, is crucial for building a website that is both functional and secure. ### The core function of the `safe` filter First, let's understand a basic security mechanism of the Anqi CMS template engine: the default automatic escaping.

2025-11-08

The `floatformat` filter: how to precisely control the decimal places of floating-point number display?

In website content management, we often need to display various numbers, especially floating-point numbers with decimal points.The way numbers are displayed, whether it is product prices, statistics, or calculation results, directly affects user experience and the accuracy of information.Inconsistent or inaccurate decimal places may cause the page to look cluttered, and even lead to misunderstandings in financial or precise measurement situations.

2025-11-08

How `forloop.Counter` and `forloop.Revcounter` assist in indexing operations during template loops?

In AnQiCMS template development, we often need to handle various data lists, such as article lists, product displays, or navigation menus.The `for` loop is the core tool for traversing these lists.However, simply listing data items often does not meet all needs, we may also need to know the current item being traversed in the list is which item, or how many items are left until the end of the list.

2025-11-08

`capfirst`, `lower`, `upper`, `title` filters: how to handle the need for English text case conversion?

In website content operation, the uniformity and beauty of text format are crucial for improving user experience.Especially when dealing with English text, flexibly controlling the case can help us better present information, whether it is used for headings, keywords, or body content.AnQiCMS provides a series of practical template filters to help you easily deal with various English case conversion needs.

2025-11-08

How to control the alignment of a string within a specified width using `center`, `ljust`, and `rjust` filters?

In website content operation, the way content is presented often determines the first impression and reading experience of users.How to ensure that the text information displayed in the website template is uniform, beautiful, and professional, which is a concern for many operators.AnQiCMS as a content management system that focuses on user experience and highly customizable, deeply understands this, and its powerful template engine provides a variety of practical filters to help us easily align strings.

2025-11-08