When operating a website, the publishing time of articles is often one of the important pieces of information that users pay attention to.An clear and readable date format can enhance user experience and also help improve the professionalism of the website content.An efficient and customizable content management system, AnQi CMS provides a flexible way to control the display of various information in templates, including the publication time of articles.

The template system of Anqi CMS adopts a concise syntax similar to Django, variables are usually referenced through double curly braces{{变量}}and logical control is used{% 标签 %}Such a structure.The posting time of the article is usually stored in the system in the form of a timestamp (a sequence of numbers representing time).This means we need a method to convert this original timestamp into the date format we are accustomed to reading, such as “June 1, 2023”.

To achieve this, SafeCMS provides a very practical built-in tag:stampToDate.

MasterstampToDatetags

stampToDateThe tag is used specifically to format timestamps into readable date and time strings. Its basic usage is very intuitive:{{stampToDate(时间戳, "格式")}}

The key here is the second parameter - "format". The security CMS usesstampToDatethe tag follows the Go language time formatting rules. Many systems useYYYY-MM-DDThis placeholder is different, the time formatting in Go language is based on a specific reference time point:2006年01月02日 15点04分05秒 -0700 MST.You just need to replace the year, month, day, hour, minute, and second in this reference time with the display style you want.

例如,如果您想显示“2023年6月1日”,那么参考时间中的“2006”对应年份,“01”对应月份,“02”对应日期,您只需要将这些部分替换成英文文字或您希望的顺序。

How to obtain the article publish time?

In the Anqi CMS template, you can obtain the original publish time (timestamp) of the article in the following way:

  • Article detail page:Directly use:archive.CreatedTimeHere,archiveIt is usually the data object of the current article detail page.
  • Article list page:In{% for item in archives %}in the loop, usingitem.CreatedTime.

Both fields will return a 10-digit timestamp.

Practical formatting example

Now, let's combinestampToDateLabels and formatting rules in Go language, to implement various date display effects:

  1. Displayed as “June 1, 2023”:

    {{stampToDate(archive.CreatedTime, "2006年01月02日")}}
    

    Here,2006Represents the year,01Represents the month,02Representing date, we use Chinese characters “year”, “month”, “day” to connect them.

  2. Displayed as “2023-06-01”:“

    {{stampToDate(archive.CreatedTime, "2006-01-02")}}
    
  3. 显示为“2023/06/01 15:30”:

    {{stampToDate(archive.CreatedTime, "2006/01/02 15:04")}}
    

    15represents the hour (24-hour clock),04代表分钟。

  4. 只显示日期“06月01日”:

    {{stampToDate(archive.CreatedTime, "01月02日")}}
    
  5. 显示为“6月1日, 2023” (English common format):

    {{stampToDate(archive.CreatedTime, "Jan _2, 2006")}}
    

    Note:JanAbbreviation for the month,_2Represents the date (with a leading space if it is a single digit)。

Integrated into the actual template

Let's see how these formatting methods are applied in actual template code.

In the article detail page template (detail.html) in:

<article class="article-detail">
    <h1>{{archive.Title}}</h1>
    <div class="article-meta">
        <span>发布于:{{stampToDate(archive.CreatedTime, "2006年01月02日")}}</span>
        <span>浏览量:{{archive.Views}}</span>
        <span>分类:<a href="{{archive.Category.Link}}">{{archive.Category.Title}}</a></span>
    </div>
    <div class="article-content">
        {{archive.Content|safe}}
    </div>
</article>

In the article list page template (list.html) in:

<ul class="article-list">
    {% archiveList articles with type="page" limit="10" %}
        {% for item in articles %}
        <li>
            <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
            <p class="summary">{{item.Description}}</p>
            <div class="meta">
                <span>发布时间:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</span>
                <span>阅读量:{{item.Views}}</span>
            </div>
        </li>
        {% empty %}
        <li>暂无文章内容。</li>
        {% endfor %}
    {% endarchiveList %}
</ul>
{% pagination pages with show="5" %}
    {# 这里是分页导航的代码 #}
{% endpagination %}

PassstampToDateLabels, you can flexibly control the display of article publishing time to make it fully comply with your website's design style and user needs.This design philosophy of AnQi CMS makes content presentation both powerful and easy to get started with.


Common Questions (FAQ)

1. Why does the Anqi CMS use such a date format2006-01-02 15:04:05instead of the commonYYYY-MM-DD?

This is because the AnQi CMS is developed based on Go language, and the time formatting in Go language uses a special reference time point:Mon Jan 2 15:04:05 MST 2006.You need to replace the reference value of the time element you want to display (for example, 2006 represents the year, 01 represents the month, 02 represents the day) with the format you want."2006-01-02"Although it looks a bit unique, once you master this rule, you will find it very flexible.

2. In addition to the article's publication time (CreatedTime), other time fields can also be usedstampToDateto format?

Of course you can. As long as it is a 10-digit timestamp field obtained from the Anqi CMS template, such as the article update timearchive.UpdatedTimeoritem.UpdatedTime, or other custom fields storing timestamps can also be formatted in the same way. Just replace the corresponding variables.stampToDateTags can be formatted in the same way. You just need to replace the corresponding variables.

3. Can I display different formats of publication time in the same article or list item?

Absolutely.stampToDateThe label can be used multiple times, and each time a different format can be specified. For example, you may want to display a concise date '2023 June 1' at the top of the article, and a specific time '2023-06-01 15:30:00' at the bottom of the article. You just need to call them separately in the template.{{stampToDate(archive.CreatedTime, "2006年01月02日")}}和`{{stampToDate(archive.CreatedTime, “English”)} ]