In the daily operation of website content, 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 perception and trust of users.However, in the templates of the content management system, we often encounter dates stored in the form of "timestamp", these numbers are accurate, 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 timeUpdatedTime)et cetera are typically stored in databases in the standard Unix timestamp format.These timestamps are usually 10 or 13 digits, representing the number of seconds or milliseconds since 00:00:00 UTC on January 1, 1970.1678886400Such numbers are obviously meaningless and unfriendly.To enhance the user experience, we need to convert it into the date and time format we are accustomed to reading in our daily lives, such as

AnQiCMS provides a very practical template tag -stampToDateIt can help us easily convert original timestamps into various readable date and time formats.

stampToDateThe core usage of tags

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

Here it is especially important to note that the date formatting 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 like in other systems directlyYYYY-MM-DDInstead, you need to use the corresponding numbers in this reference time to represent year, month, day, hour, minute, and second.

Let us understand this format through some specific examples:

  • Year: Use2006to represent a four-digit year.
  • Month: Use01orJanto represent a two-digit month or month abbreviation.
  • 日期(Day): Use02or_2来表示两位日期或单数日期。
  • 小时(Hour): Use15(24小时制)或03(12小时制)来表示小时。
  • Minute: Use04to represent minutes.
  • Seconds: Use05to represent seconds.

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

  • Show the complete year, month, and day:
    • {{stampToDate(时间戳, "2006-01-02")}}2023-03-15
    • {{stampToDate(时间戳, "2006年01月02日")}}2023年03月15日
  • Show 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 month and date:
    • {{stampToDate(时间戳, "01-02")}}03-15
  • Only show time:
    • {{stampToDate(时间戳, "15:04")}}14:30
    • {{stampToDate(时间戳, "03:04 PM")}}02:30 PM(12-hour format with AM/PM)

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

Apply in content template

We now know how to usestampToDate, and the next step is how to obtain and format timestamps in the actual content template of AnQiCMS.

Suppose we are developing a template for an article detail page that needs to display the publication time. In AnQiCMS, the context of the article detail page usually has aarchiveObject, which containsCreatedTimeFields. 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's creation,"2006年01月02日 15:04"Then we define the format we want it to display as “year-month-day hour:minute”.

If we need to traverse multiple articles and display their publication time on a list page of articles, 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.CreatedTimeisforRetrieving the timestamp of each article in the loop and formatting it.年-月-日In a concise form.

Some practical tips and considerations.

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

By using flexibilitystampToDateTags and date formatting rules in Go language, you can easily implement various date and time displays as needed in AnQiCMS templates, thereby 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 in Go language, it follows the unique specification of Go language in date and time formatting. Go language does not use symbolic placeholders, but uses a specific reference date2006-01-02 15:04:05(Also commonly referred to as “Go's time” or “reference time”)to define various formats.This means that you need to provide the numbers or text in the format string corresponding to the reference time you wish to output.2006.

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