In managing website content in AnQi CMS, we often encounter situations where we need to display dates and times.However, these time information stored in the background database is usually in the form of timestamps (timestamp), which is very efficient for machine processing, but difficult for users to read directly.1609470335Such a number represents a specific moment, but we hope it can be displayed in a more user-friendly format like "2021 January 1 12:25:35

幸运的是,安企CMS充分考虑了这种需求,提供了一个非常便捷且强大的模板标签,帮助我们轻松将后台的时间戳数据转换为易于理解的日期和时间格式。

Understanding Core Tools:stampToDatetags

The template system of Anqi CMS is built-in with a namedstampToDatetag, which is specifically used for formatting the display of timestamps. Its basic usage is very intuitive:

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

Here is the “timestamp variable” which is the string of numbers you get from the backend (usually a 10-digit Unix timestamp), and the “format string” is the style you want the date and time to appear in.

An important concept needs to be understood: The format string of AnQi CMS follows the Go language's time formatting standard. Go language uses a fixed reference date2006-01-02 15:04:05auto as a template. This means, if you want to display the year, use2006auto to display the month, use01auto to display the date, use02This is followed by others. At first glance, it may seem counterintuitive, but once you get the hang of it, you will find it very flexible.

Examples of commonly used date and time formats

To help you better understand and apply, let's look at some common formatting examples, assuming our timestamp variable isitem.CreatedTimewith the value1609470335(That is, January 1, 2021, 12:25:35):

  1. Show date only (Year-Month-Day)If you only care about the publication date of the article and do not want to see the specific time, you can use the following format:

    {{ stampToDate(item.CreatedTime, "2006-01-02") }}
    {# 输出: 2021-01-01 #}
    
  2. Display the full date and timeIf you need to be precise to seconds, you can use:

    {{ stampToDate(item.CreatedTime, "2006-01-02 15:04:05") }}
    {# 输出: 2021-01-01 12:25:35 #}
    
  3. Custom Chinese formatFor Chinese websites, we may want to display the words "year, month, day":

    {{ stampToDate(item.CreatedTime, "2006年01月02日 15时04分") }}
    {# 输出: 2021年01月01日 12时25分 #}
    
  4. Other common formatsYou can also combine various formats as needed:

    • Display the month and date:{{ stampToDate(item.CreatedTime, "01-02") }}(Output: 01-01)
    • Show time (hour:minute):{{ stampToDate(item.CreatedTime, "15:04") }}(Output: 12:25)
    • Display the day of the week:{{ stampToDate(item.CreatedTime, "Mon") }}or{{ stampToDate(item.CreatedTime, "Monday") }}(Output: Fri or Friday)
    • Display time with AM/PM:{{ stampToDate(item.CreatedTime, "3:04PM") }}(Output: 12:25PM)

The key is to remember2006-01-02 15:04:05What do each number and symbol represent in this reference date, and then replace it with the actual characters you want to use? For example, if you want to display the year, write2006; to display a hyphen-, write-.

Where is the timestamp used in AnQi CMS?

The AnQi CMS backend stores timestamp data in many places, such as:

  • Article/Document ManagementThe publication time of the article (CreatedTime) And update time (UpdatedTime).
  • Comment management: Comment publish time (CreatedTime).
  • User Management: User's last login time (LastLogin) And VIP expiration time (ExpireTime).

These timestamps are usually used as loop itemsitemThe properties of an object, or as the current object's properties passed to the template in the detail page. For example, in the article list loop, you will use{{item.CreatedTime}}Get the timestamp of each article's publication.

Actual application scenario: Format the publication time of the article list.

Suppose you are editing the article list template (for example)/template/default/archive/list.html),and hope to display the publication date of each article:

{# 假设这里是文章列表循环的开始 #}
{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <li>
        <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
        <div>
            {# 这里是格式化时间戳的关键 #}
            <span>发布日期:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}</span>
            <span>浏览量:{{item.Views}} 次</span>
        </div>
        <p>{{item.Description}}</p>
    </li>
    {% empty %}
    <li>暂无文章内容。</li>
    {% endfor %}
{% endarchiveList %}
{# 假设这里是文章列表循环的结束 #}

Through this simple line of code, all article timestamps will be automatically converted to a readable format of "XXXX year XX month XX day", greatly enhancing the user experience of the website.

Summary

Convert the timestamp data from the background to a readable date and time format is a basic but important task in content operation. The Anqi CMS providedstampToDateLabel, through its flexible Go language formatting mechanism, makes this process simple and efficient. Just remember that magical2006-01-02 15:04:05Reference date, you can freely customize any date and time display format you want, making your website content more closely aligned with users.

Common Questions (FAQ)

1. Why is string formatting2006-01-02 15:04:05such a combination of numbers, rather than commonYYYY-MM-DD?

This is the time formatting convention unique to the Go language (the underlying development language of Anq CMS).It is not just a placeholder, but a specific date and time chosen at the beginning of the birth of the Go language as a 'reference template'.2006-01-02 15:04:05When, the Go language will convert your timestamp to the corresponding date and time parts according to the structure of this template. So, if you want to display the year, you should write2006If you want to display specific month, day, hour, minute, and second, use them separately.01/02/15/04/05As a template. This method may require some adaptation at first, but once you understand its logic, you will find it very flexible and precise.

2. I can use it directly.dateDo you want to filter the timestamp?

It is not recommended to use it directlydateA filter is provided to format the timestamp in the Anqi CMS template.dateFilter, but it expects a Go language type object, not a raw Unix timestamp number.time.Timetype object, not a raw Unix timestamp number if you directly usedateFilter, may cause errors or unexpected results. Therefore, for timestamp data obtained from the background, please make sure to use a dedicatedstampToDatetag for formatting