In website content operation, the presentation of time is often overlooked, but it is an important detail for improving user experience and content professionalism.The original timestamp number lacks meaning for ordinary users. Formatting it into a clear and readable date and time format makes the content more engaging.AutoCMS understands this point, providing a simple yet powerful way to handle such needs.

Understanding the timestamp in AnQi CMS

In the Anqi CMS, whether it is the publication time of the article (CreatedTime) or the update time (UpdatedTime1609470335The number, which is the unformatted timestamp. Displaying this number directly makes it difficult for users to understand the actual date and time it represents.

Core Function:stampToDatetags

To solve the readability problem of timestamps, Anqi CMS provides a feature namedstampToDateThe template tag. The core function of this tag is to convert the original timestamp into a human-friendly date and time string according to the format you specify.

Its basic usage is very intuitive:{{stampToDate(时间戳变量, "格式字符串")}}

There are two key parts:

  1. Timestamp variableThis is usually obtained fromarchiveDetail/archiveListor other content tagsCreatedTimeorUpdatedTimeand other fields.
  2. Format stringThis is the key to how date and time are displayed. It is used by many CMS systemsY-m-d H:i:sSuch placeholders are different, the format strings of AnQi CMS follow the special conventions of the Go language itself, that is, using oneFixed reference date and timeThis is used as a format template. This reference date and time is2006-01-02 15:04:05. Just write this reference date in the output format you want, and the system will intelligently format the actual timestamp.

For example:

  • If you wish to display年-月-日, the format string should be written as"2006-01-02".
  • If you wish to display年/月/日, the format string should be written as"2006/01/02".
  • If you wish to display月日, the format string should be written as"01-02".
  • If you wish to display年-月-日 时:分:秒, the format string should be written as"2006-01-02 15:04:05".
  • If you wish to display年时分, the format string should be written as"2006年15时04分".

By this method, you can almost flexibly combine any date and time format you want.

Actual application example

Assuming you are creating a list page or detail page of articles and want to display the publication time of each article.

Scenario one: Displaying the publication date in the article list.

InarchiveListIn the loop, you can use it like thisstampToDateTags to formatCreatedTime:

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <li>
        <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
        <p>发布时间:<span>{{stampToDate(item.CreatedTime, "2006年01月02日")}}</span></p>
        <p>{{item.Description}}</p>
    </li>
    {% endfor %}
{% endarchiveList %}

This code will iterate over the article list and display the title, link, publish time (for example2023年10月27日) and summary of each article.

Scene two: Display the update time on the article detail page

In the article detail page, you may need to display the update time of the article, precise to minutes:

<article>
    <h1>{% archiveDetail with name="Title" %}</h1>
    <p>
        发布于:<span>{{stampToDate(archive.CreatedTime, "2006-01-02 15:04")}}</span>
        更新于:<span>{{stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05")}}</span>
    </p>
    <div>
        {%- archiveDetail articleContent with name="Content" %}
        {{articleContent|safe}}
    </div>
</article>

Here we usedarchive.CreatedTimeandarchive.UpdatedTime(In the document detail page,archiveThe variable usually represents the current document object), and they are used separately2006-01-02 15:04and2006-01-02 15:04:05Has been formatted.

Useful Tips and Considerations

  • Maintain consistencyIn the entire website, try to maintain consistency in the date and time format, which helps enhance user experience and the professional image of the website.
  • Consider the target audience:If your website has an international audience, consider using more generic date formats (such asYYYY-MM-DD) or switching to different formats based on language packs.
  • Memory of Go language format stringsAt first you may think2006-01-02 15:04:05difficult to remember, you can consider it a special 'magic date', just remember the meaning of each number and symbol. For example:
    • 2006-> Year
    • 01-> Month
    • 02-> Day
    • 15-> Hour (Hour, 24-hour format)
    • 04-> Minutes (Minute)
    • 05-> Seconds (Second)

By masteringstampToDateThe use of tags, you can easily convert timestamp data in the Aiqi CMS into user-friendly date and time information, making your website content more professional and easier to read.


Common Questions and Answers (FAQ)

1. Why should format strings use2006-01-02 15:04:05such specific number combinationsYYYY-MM-DDinstead of such general placeholders?

This is because the Anqi CMS is developed in Go language, and the Go language uses a fixed reference time for formatting date and time2006-01-02 15:04:05 -0700 MST(常被称为“魔法时间”),其中每个数字和缩写都对应着特定的时间单位(例如2006Represents the year,01representing months, etc.).You only need to apply the output format you expect to this reference time, and the system will automatically complete the conversion. This is a concise and efficient way of defining format definitions that is unique to the Go language.

2. 如果我只想显示时间,例如10:30How should the format string be set?

If you only want to display the time, you can directly use the time part of the reference time to construct the format string. For example, if you want to display时:分Format string can be set to"15:04"If you want to display时:分:秒Use"15:04:05".

3.stampToDateDoes the tag support all timezone conversions?

stampToDateThe label is mainly responsible for converting timestamps to strings in a specified format.By default, it will convert according to the time zone set by the server.If you need to display different time zones for different users, this usually requires handling time zone conversions of timestamps in the backend logic, or ensuring that the server's time zone settings meet the needs of your primary audience.The template tag itself does not provide direct time zone conversion parameters, it focuses on formatting timestamps with existing time zones.