Fine control of time display: Security CMS instampToDateThe secret manual of flexible application
In the operation of a website, the display of time information seems ordinary, but it actually contains the profound knowledge 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 newcomers to operations might be puzzled, as this function seems to always output a fixed date-time format. However, by cleverly utilizing its 'format' parameter, you can easily achieve displaying only the date, only the time, or various custom time styles.stampToDate[en] Let the time information be presented in the most suitable way for your needs in front of the visitors.
stampToDate[en] : The foundation of time information display.
In the template world of Anqi CMS, we often encounter things like article publishing time (item.CreatedTime)Update time(archive.UpdatedTimeData stored in the form of 10-digit Unix timestamps. These original timestamps are not intuitive for visitors, and that's when this 'translator' comes into play.stampToDateThis 'translator' makes its appearance.
Its basic usage is very concise and clear:{{stampToDate(时间戳, "格式")}}.The key here is the second parameter—the 'format' string.It is not arbitrarily defined but follows the date and time formatting rules unique to the Go language.stampToDateThe key to flexibility.
Unveiling the "magic numbers" of time formatting in the Go language.
With other programming languages (such as PHP's)Y-m-d H:i:sDifferent from other languages, 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 may find this sequence of numbers quite strange, but each part represents the corresponding time element:
2006: represents the year01: represents the month (January)02: represents the date (second day)15:represents hour (24-hour format, 3 PM)04:represents minutes05:represents seconds
When you want to format time, just include the string in the “Format”select and combineThese 'magic numbers' or their corresponding positions are 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.01.
Flexiblely choose to display the date or time part.
Understanding the reference time in the Go language, now we can easily implement various time display requirements:
1. Display only the date (year-month-day)
When you only need to show the specific date of content release to users without going into the exact second, you can do it like this:
{# 假设 item.CreatedTime 是一个时间戳,例如 1609470335 #}
<div>发布日期:{{stampToDate(item.CreatedTime, "2006-01-02")}}</div>
{# 输出示例:发布日期:2021-01-01 #}
If you prefer the Chinese format, it can be like this:
<div>发布日期:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</div>
{# 输出示例:发布日期:2021年01月01日 #}
2. Only display 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 hour and minute a certain 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 both date and time:
<div>文章发布:{{stampToDate(item.CreatedTime, "2006-01-02 15:04:05")}}</div>
{# 输出示例:文章发布:2021-01-01 10:45:30 #}
4. Other custom date and time combinations
The flexibility of the Go language lies in the fact that you can use any character to concatenate 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 #} - Weekday and date:
<div>会议时间:{{stampToDate(event.Time, "Mon, 01-02")}}</div> {# 输出示例:会议时间:周五, 01-01 #} - Date without leading zeros:
Please note, in the formatting rules of the Go language,<div>简短日期:{{stampToDate(item.CreatedTime, "2006-1-2")}}</div> {# 输出示例:简短日期:2021-1-1 #}_2used to represent dates without leading zeros,Janabbreviations for the month,MonAbbreviation for the day of the week. You can learn more about advanced formatting options by consulting the Go language officialtimepackage documentation.
The actual application in the AnQi CMS template
In the template development of Anqi CMS,stampToDateIt is usually used in combination with various data list tags, such asarchiveList(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
stampToDateTags are a powerful assistant for handling timestamp data in Aiqi CMS template development.By deeply understanding the "2006-01-02 15:04:05" reference time format in Go language, you can easily implement flexible display of date or time parts, meeting various refined needs for website content presentation.stampToDateAll of these can help you a hand, making your website information more readable and professional.
Common Questions (FAQ)
Why is the time formatting in the Go language not likeY-m-d H:i:sthis letter code?A1: One of the design philosophies of the Go language is clarity and consistency. It avoids using single-letter codes that may be ambiguous (such as, m[en] May represent months or minutes, but instead uses a fixed 'reference time' string2006-01-02 15:04:05When you provide a part of this reference time in a format string, the Go language will use the specific number and its position you providespecificallyTo infer the output format you want. This method may look peculiar at first glance, but once you understand its logic, you will find it very intuitive and less prone to mistakes.
Q2:stampToDateCan it realize the humanized time display such as 'X minutes ago' or 'Today/Tomorrow'?A2: ``