Time Wizard: Anqi CMSstampToDateHow to easily handle 10-digit timestamps with tags

In the world of content operation, the way time is presented often directly affects users' understanding and perception of the content.A clear and easy-to-read date format that allows users to quickly obtain key information while browsing articles, products, or comments, thereby enhancing the overall browsing experience.However, at the database and system level, time is often represented by a string of "mysterious" numbers - a 10-digit timestamp (Unix timestamp) - that exists.This is efficient and standardized for machines, but it is like a book of heaven for ordinary users.

AnQi CMS, as an enterprise-level content management system developed based on the Go language, deeply understands this. In order to enable content operators to easily handle these timestamps and convert them into a warm and easy-to-understand date format, AnQi CMS provides a powerful and flexible template tag:stampToDate. It's like a time wizard, able to transform those boring 10-digit numbers into the various date and time expressions you want.

stampToDate: Convert machine time to human-readable time

Imagine when you need to display the publish time in a list of website articles or mark the update date on a product detail page, and you see something like1609470335When this string of numbers is used instead of stampToDateTags are exactly born to solve this pain point.

Its basic usage is very intuitive:{{stampToDate(时间戳, "格式")}}

The 'timestamp' here is what we call the 10-digit number, usually in the documents of Anqi CMS (such asCreatedTimeorUpdatedTimeFields are stored in this form. And "format", it is the key to the magic of the tag. It follows the unique and highly flexible time formatting rules of the Go language.stampToDate标签发挥魔法的关键所在。它遵循Go语言独特且高度灵活的时间格式化规则。

Unveiling Go language's time formatting: the unique "reference time"

May be used by some other content management systemsY-m-dorH:i:sThese symbols are different, the date formatting in Go language uses a unique "reference time"-2006-01-02 15:04:05. This does not mean that the timestamp will be formatted as 2006 or 15 o'clock, but rather with each number in this datePositionas the reference for formatting.

For example, if you want to convert a 10-digit timestamp1609470335(which represents2021 January 1 07:05:35 UTCFormat into different common date formats, you can do this:

  • Display only year, month, and day (separated by hyphens): {{stampToDate(1609470335, "2006-01-02")}}It will be displayed as2021-01-01
  • Display only hours, minutes, and seconds: {{stampToDate(1609470335, "15:04:05")}}It will be displayed as07:05:35
  • Display the full year, month, day, hours, minutes, and seconds: {{stampToDate(1609470335, "2006-01-02 15:04:05")}}It will be displayed as2021-01-01 07:05:35
  • Custom Chinese format: {{stampToDate(1609470335, "2006年01月02日 15时04分")}}It will be displayed as2021年01月01日 07时05分
  • Change the date order (e.g., day/month/year): {{stampToDate(1609470335, "02/01/2006")}}It will be displayed as01/01/2021

This "reference time" setting makes formatting extremely flexible. You just need to match the output format you expect,2006-01-02 15:04:05This refers to the time, write the corresponding number and separator. For example, if you want to display the abbreviation of the month, you can refer to the Go language rules, usingJanto replace01, like this{{stampToDate(1609470335, "02 Jan 2006")}}it will be displayed as01 Jan 2021.

Practice in the Anqi CMS template.stampToDate

In the actual development of Anqi CMS templates,stampToDateTags are usually witharchiveList/archiveDetailUse these tags together to format timestamp fields retrieved from the database.

For example, on an article list page, you might use it to display the publication date of each article:

{% archiveList latestArticles with type="list" limit="5" %}
    {% for article in latestArticles %}
        <article>
            <h2><a href="{{ article.Link }}">{{ article.Title }}</a></h2>
            <p>发布于:{{ stampToDate(article.CreatedTime, "2006年01月02日") }}</p>
            <p>{{ article.Description }}</p>
        </article>
    {% endfor %}
{% endarchiveList %}

Or on the article detail page, display the update time of the article:

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

No matter if you need an update log precise to the second or a concise release date,stampToDateThe labels can be adjusted with simple format parameters to meet all your needs.This has greatly improved the readability of the content and also made the overall presentation of the website more professional and friendly.The Anqi CMS is dedicated to providing such practical and efficient features, making content operations more skillful.


Frequently Asked Questions (FAQ)

1. The timestamp I got from the database is not 10 digits,stampToDateCan the tag still be used? stampToDateTags are designed for the standard 10-digit Unix timestamp.If your timestamp is 13 digits (millisecond level), you need to divide it by 1000 first to convert it to 10 digits, or handle it on the front end using JavaScript and other methods.Directly pass in a 13-digit timestampstampToDateMay get incorrect results.

2. Besides the examples in the article, what are some common date formatting references in the Go language? Where can I find more?The date formatting of Go language is very rich, including2006-01-02 15:04:05There are also many combinations, such as:

  • Mon Jan _2 15:04:05 MST 2006(Standard CST format, such as:周一 1月 01 07:05:35 UTC 2021)
  • 2006-01-02T15:04:05Z07:00(ISO 8601 standard format)
  • 3:04PM(AM/PM time format) You can find the detailed instructions for 'format timestamp tag' in the 'Template Creation' document of Anqi CMS (i.e.tag-stampToDate.md), among more detailed formatting examples.

**3.