Fine control of time display: Anqi CMSstampToDateThe secret of flexible application

In website operations, the display of time information may seem ordinary, but it contains the art of improving user experience and optimizing content presentation. Anqi CMS understands this point and provides webmasters with a powerful and flexible timestamp formatting tool -stampToDate. Many operators who are new to this may be puzzled, as it seems to always output a fixed date and time format, but in fact, by skillfully using its 'format' parameter, you can easily achieve showing only the date, only the time, and even various customized time styles.Today, let's delve into how to skillfully navigate in the Anqi CMS templatestampToDatePresent time information in the most suitable way according to your needs in front of visitors.

stampToDateThe foundation of time information display.

In the world of AnQi CMS templates, we often encounter things like article publication time (item.CreatedTime)、Update Time(archive.UpdatedTimeData stored in the form of 10-digit Unix timestamps. These original timestamps are not intuitive for visitors, so it is necessary tostampToDateThis 'translator' has appeared.

Its basic usage is very concise and clear:{{stampToDate(时间戳, "格式")}}The key is in the second parameter - the format string.It is not arbitrarily defined, but follows the special date and time formatting rules of the Go language.Understand this rule and you will be unlockedstampToDateThe key to flexibility.

Unveiling the "magic numbers" of time formatting in Go language.

And other programming languages (such as PHP'sY-m-d H:i:s) Different, Go language uses a fixed 'reference time' as a template when formatting date and time. This reference time is: 2006-01-02 15:04:05.

You might find this string of numbers strange, but each part represents the corresponding time element:

  • 2006: Represents the year
  • 01: Represents the month (January)
  • 02: Represents the date (second day)
  • 15Represents hours (24-hour format, 3 PM)
  • 04Represents minutes
  • 05Represents seconds

When you want to format time, just in the "format" stringSelect and combineThese "magic numbers" or their corresponding positions can be used to express the desired output style. For example, if you want to display the year, write it in the format string.2006If you want to display the month, just write it up01.

Flexiblely choose to display the date or time part

Understanding the reference time in Go language, now we can easily implement various time display requirements:

1. Show only the date (Year-Month-Day)

When you only need to display the specific date of content release to the user without going into the exact seconds, you can do this:

{# 假设 item.CreatedTime 是一个时间戳,例如 1609470335 #}
<div>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</div>
{# 输出示例:发布日期:2021-01-01 #}

If you prefer the Chinese format, you can do it like this:

<div>发布日期:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</div>
{# 输出示例:发布日期:2021年01月01日 #}

2. Only show time (hour:minute:second or hour:minute)

Sometimes, we may only be concerned with the time point of an event, such as the time of a comment, or the time an activity starts.

{# 仅显示时分秒 #}
<div>评论时间:{{stampToDate(comment.CreatedTime, "15:04:05")}}</div>
{# 输出示例:评论时间:10:45:30 #}

{# 仅显示时分 #}
<div>更新于:{{stampToDate(archive.UpdatedTime, "15:04")}}</div>
{# 输出示例:更新于:10:45 #}

3. Complete date and time format

This is the most common format, which includes dates and times:

<div>文章发布:{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}}</div>
{# 输出示例:文章发布:2021-01-01 10:45:30 #}

4. Other customized date and time combinations

The flexibility of the Go language lies in the fact that you can use any character to connect these reference time elements, creating a format that matches your brand style or specific scenario.

  • Month/Day format:
    
    <div>生日:{{stampToDate(user.Birthday, "01/02")}}</div>
    {# 输出示例:生日:05/20 #}
    
  • The day of the week and date:
    
    <div>会议时间:{{stampToDate(event.Time, "Mon, 01-02")}}</div>
    {# 输出示例:会议时间:周五, 01-01 #}
    
  • Date without leading zeros:
    
    <div>简短日期:{{stampToDate(item.CreatedTime, "2006-1-2")}}</div>
    {# 输出示例:简短日期:2021-1-1 #}
    
    Please note, in the formatting rules of Go language,_2Used to represent dates without leading zeros,JanAbbreviation for the month,MonAbbreviation for the day of the week. You can check the Go language officialtimepackage documentation to learn more about advanced formatting options.

In the actual application of the Anqi CMS template

In the template development of AnQi CMS,stampToDateusually combined with various data list tags, for examplearchiveList(Document list),archiveDetail(Document Details),commentList(Comment list) and so on.

The following is an example of displaying the publication date flexibly on the document list page(archiveList)

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <article>
        <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
        <p>{{item.Description}}</p>
        <footer>
            <span>分类:{% categoryDetail with name="Title" id=item.CategoryId %}</span>
            {# 只显示日期 #}
            <span>发布于:{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
            {# 也可以同时显示日期和时间 #}
            {# <span>发布于:{{stampToDate(item.CreatedTime, "2006-01-02 15:04")}}</span> #}
            <span>浏览量:{{item.Views}}</span>
        </footer>
    </article>
    {% empty %}
    <p>暂时没有内容。</p>
    {% endfor %}
{% endarchiveList %}

Summary

stampToDateThe tag is a powerful assistant for handling timestamp data in the development of Anqi CMS templates.By deeply understanding the "2006-01-02 15:04:05" reference time format in Go language, you can easily achieve flexible display of the date or time part, meeting various refined needs for website content display.Whether it is a concise release date, or an accurate update time, or even a more creative combination format,stampToDateCan all help you a hand, making your website information more readable and professional.

Frequently Asked Questions (FAQ)

Q1: Why is the time formatting in Go language not likeY-m-d H:i:sthis letter code?A1: The design philosophy of Go language is one of clarity and consistency. It avoids the use of single-letter codes that may be ambiguous (such as,mIt may represent months or minutes, but instead uses a fixed 'reference time' string2006-01-02 15:04:05. When you provide a part of this reference time in the format string, the Go language will use the specific number and its position you providethe positionTo deduce the output format you want. This approach may seem peculiar at first glance, but once you understand its logic, you will find it very intuitive and error-free.

Q2:stampToDateCan it implement a humanized time display such as 'X minutes ago' or 'Today/Tomorrow'?A2:`