Let time speak: How to format a timestamp into a readable date in Anqi CMS

When operating a website, the timeliness of content is often a point of great concern for users.Whether it is the publication time of an article, the update time of product information, or the specific moment of user comments, a clear and intuitive date and time can greatly enhance user experience, and even have a positive impact on search engine optimization (SEO).1609470335This is what is called a timestamp, it is machine language, and it is not intuitive for ordinary users.

AnQi CMS is an efficient and easy-to-use content management system that fully considers the actual needs of users. It provides us with a very convenient and powerful tool to solve this problem, that isstampToDateThe label makes it easy to convert those cold timestamps into a date and time format that everyone can understand, with a touch of warmth.

Get to knowstampToDateTag

stampToDateThe tag is a built-in function in Anqi CMS template engine, specifically used for timestamp formatting. Its basic usage is very intuitive:

{{stampToDate(时间戳, "格式")}}

Here时间戳It is usually a 10-digit Unix timestamp (representing the number of seconds from 1970-01-01 00:00:00 UTC to the present), and格式The specific style you want the date and time to display.

The key is in how to define this 'format'.The template engine of AnQi CMS is based on Go language, therefore it follows the unique time formatting rules of Go language.YYYY-MM-DDInstead of such placeholders, use oneA specific reference time:2006-01-02 15:04:05. You need to map the date and time format you want to this reference time. Sounds a bit abstract? Don't worry, we'll understand through some examples.

List of common formatting modes

MasterstampToDateThe key is to understand this reference time in Go language. The following table will show some common date and time display requirements, as well as the corresponding format strings:

Display requirements Format string (Go language reference time) Sample output (assuming timestamp is)1609470335Corresponding to January 1, 2021, 11:05:35
Date
Year-Month-Day (e.g., 2021-01-01) 2006-01-02 2021-01-01
Year Month Day (e.g., 2021 January 01) 2006年01月02日 2021年01月01日
Month/Day/Year (e.g., 01/01/2021) 01/02/2006 01/01/2021
Time
Time:Hour:Minute:Second (e.g., 11:05:35) 15:04:05 11:05:35
Time:Hour:Minute (e.g., 11:05) 15:04 11:05
Complete Date and Time
Year-Month-Day Hour:Minute:Second 2006-01-02 15:04:05 2021-01-01 11:05:35
Year-Month-Day Hour:Minute:Second 2006年01月02日 15时04分05秒 2021年01月01日 11时05分35秒
Day, Month Date Year Time:Minutes:Seconds Mon, 02 Jan 2006 15:04:05 Fri, 01 Jan 2021 11:05:35

With this reference table, you can flexibly combine almost any date and time format you want

Apply in your Anqi CMS template

Now, let's see how to use it in an actual Anqi CMS template filestampToDatetags. Whether it's an article detail (archiveDetail) or an article list (archiveList) Or comment list (commentList) Many time fields such asCreatedTime/UpdatedTime) are provided in the form of 10-digit timestamps.

Example one: Display the publish date on the article detail page

Assuming you want to display the publication date of the article on the ({模型table}/detail.htmlor a custom article template) page. The article object'sCreatedTimefield stores the publication timestamp.

{# 默认获取当前页面的文档对象 archive #}
<div>
    发布日期:{{stampToDate(archive.CreatedTime, "2006年01月02日")}}
</div>
<div>
    更新时间:{{stampToDate(archive.UpdatedTime, "2006-01-02 15:04")}}
</div>

Example two: Display the publication time of each article on the article list page

When you are on an article list page ({模型table}/list.htmlor a custom list template) and loop through multiple articles, you can useforin the loopitem.CreatedTime.

{% 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 15:04")}}</span>
            浏览量:<span>{{item.Views}}</span>
        </p>
        <p>{{item.Description}}</p>
    </li>
    {% endfor %}
{% endarchiveList %}

Example 3: Display time in comments or messages

When a user submits a comment or message, the system will also record the timestamp. In the comment list (comment/list.html) you can format the time like this:

{% commentList comments with archiveId=archive.Id type="list" limit="6" %}
    {% for item in comments %}
    <div>
        <strong>{{item.UserName}}</strong> 于 <span>{{stampToDate(item.CreatedTime, "2006年01月02日 15:04")}}</span> 评论道:
        <p>{{item.Content}}</p>
    </div>
    {% endfor %}
{% endcommentList %}

Tip

  • Try flexibly:The time formatting in Go language is very flexible, you can try different combinations to achieve the display effect you want. For example, you can only display the year2006or only display the month01.
  • Field Name:Please note,CreatedTimeandUpdatedTimeThese fields themselves store timestamp values, which can be directly passed tostampToDateFunction.
  • Background timed release:Safe CMS supports the scheduled publishing of content.If you set the publish time to a future date, the front-end page usually will not display the content until the specified time arrives.CreatedTimeand will be correctly parsed and displayed.

BystampToDateLabel, you can easily present time information on the website more clearly and friendly. This not only improves the user reading experience, but also enhances the content of your website