How to flexibly choose to display the date or time part in `stampToDate` instead of a fixed format?

Calendar 👁️ 73

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:`

Related articles

Does the `stampToDate` tag have a default output format when no format string is provided?

As an experienced website operations expert, I fully understand the importance of handling time data in content management systems, especially ensuring that they are presented in the way we expect.AnQiCMS (AnQiCMS) has fully considered flexibility and efficiency in the design of template tags, and the `stampToDate` tag is a typical representative.Today, let's delve into a common question about this tag: When no format string is provided, will the `stampToDate` tag have a default output format?###

2025-11-07

How to use `stampToDate` to output the countdown of 'X days until an event' in the template?

## Tips for Anqi CMS Template: Creating an Eye-Catching Countdown Effect for "X Days Until an Event" In today's fast-paced online environment, effective content operation is inseparable from dynamic interaction with users.A well-designed website that not only provides rich information but also enhances user engagement and conversion rates through some clever dynamic elements, such as countdowns.No matter if it's a new product launch, event preview, or an important milestone reminder, a striking countdown can inject vitality into your website and create a sense of urgency.

2025-11-07

How does the performance of `stampToDate` in Anqi CMS template look, will it affect the page loading speed?

## The `stampToDate` in Anqi CMS template: Will it slow down the page loading speed?As an experienced website operations expert, I am well aware that page loading speed is crucial for user experience and search engine optimization.When a template includes some data processing tags, many operators will inevitably have doubts: will this function affect website performance?Today, let's talk about a commonly used and practical tool in Anqi CMS template - the `stampToDate` tag, and delve into its performance in page loading speed

2025-11-07

Can `stampToDate` format a date to `3:04 PM` in 12-hour format?

## AnQi CMS `stampToDate` tag: Flexibly display 12-hour time, from 3:04 PM to more possibilities In the daily operation of AnQi CMS, we fully understand that every detail of content presentation is crucial, especially the display of time information.Whether it is the release time of the article, the deadline of the event, or the update time of the product, a clear and user-friendly date and time format can greatly enhance the user experience.Recently, a friend mentioned a common question: "The "stampToDate" method of Anqi CMS"

2025-11-07

How to use the `stampToDate` formatted date in the HTML `datetime` attribute to improve SEO?

## The AnQi CMS Practical Guide: How to Fly Your Website's SEO Wings with `stampToDate` and HTML `datetime`?As an experienced website operations expert, I know that in the increasingly fierce internet competition, every detail may become the key to a website standing out.For SEO, content quality is indeed the core, but optimization at the technical level should not be ignored either.

2025-11-07

In AnQiCMS, can the `stampToDate` tag handle date display habits of different regions (such as China and the United States)?

Unlock AnQiCMS `stampToDate`: Easily Master Global Date Display Habits In the wave of global content operations, the subtle differences in date and time formats often become key factors affecting user experience.A website that can intelligently adjust the date display according to the visitor's regional habits will undoubtedly greatly enhance its professionalism and user-friendliness.

2025-11-07

What is the main difference between the `now` tag and the `stampToDate` tag in the time display in `tag-system.md`?

As an experienced website operations expert, I am well aware that how to flexibly and accurately display time information in a content management system is crucial for the vitality of website content and user experience.AnQiCMS (AnQiCMS) takes this point into full consideration in the design of template tags, providing various methods to handle time.Today, let's delve into two commonly used and easily confused tags - the `now` tag and the `stampToDate` tag, analyzing their main differences in time display.

2025-11-07

How to perform security filtering on the date string generated by the `stampToDate` tag to prevent XSS attacks?

As an experienced website operation expert, I am happy to give you a detailed explanation of how to ensure the security of the date string generated by the `stampToDate` tag in AnQiCMS, effectively preventing XSS attacks. --- ### Ensure Security: How to effectively prevent XSS attacks in the date string generated by the `stampToDate` tag in AnQiCMS In the template development of AnQiCMS, the `stampToDate` tag is a powerful helper for us to process timestamps and format them into readable date strings.

2025-11-07