In website operation, it is a common requirement to flexibly display the current year or format specific time, whether it is the automatic update of the year in the copyright statement or the clear display of the publication time on the article detail page.AnQiCMS provides two main ways to meet these needs, and they are both simple and easy to use, adhering to the elegant time handling style of the Go language.

Method one: Directly obtain and display the current year or time ({% now %}Tag)

When you need to directly access and display the current year or the current time accurate to seconds in a template,{% now %}The label is your preference.This tag is very intuitive, you just need to specify the Go language style time formatting string you want in it.The system will output according to the format you define, based on the current time of the server.

For example, if you only want to display the current year at the footer of the website for copyright statements, you can write it like this:

© {% now "2006" %} 您的公司名称. All Rights Reserved.

After running, if the current year is 2024, it will automatically display as:© 2024 您的公司名称. All Rights Reserved.

If you need to display the full current date, for example “2024-07-29”:

今天的日期是:{% now "2006-01-02" %}

After running, it will display as:今天的日期是:2024-07-29

If you need to display the current exact time, for example “10:30:45”:

当前时间是:{% now "15:04:05" %}

After running, it will display as:当前时间是:10:30:45

{% now %}The flexibility of the tag lies in its ability to output various forms of current time information according to your formatted string, which greatly facilitates the display of dynamic content.

方法二:格式化已有时间戳变量 (English){{ stampToDate() }}函数(English)

More often than not, we are not trying to display the current time, but rather need to store timestamps in the database (such as the publication time of an articleCreatedTime, and update timeUpdatedTime)to be presented in a human-readable format. AnQiCMS provides{{ stampToDate() }}function.

This function accepts two parameters: the first is the timestamp variable you want to format (usually a 10-digit or 13-digit number), and the second is a time format string in the Go language style.

Assume you are looping through a list of articlesarchivesand you want to display the publication time of each article in the format '2024/07/29 10:30':

{% archiveList archives with type="list" limit="5" %}
    {% for item in archives %}
        <div>
            <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
            <p>发布于:{{ stampToDate(item.CreatedTime, "2006年01月02日 15时04分") }}</p>
        </div>
    {% endfor %}
{% endarchiveList %}

After running, the publication time of each article will be displayed in the specified Chinese format.

If you only want to display the date part, for example, "2024/07/29":

更新时间:{{ stampToDate(archive.UpdatedTime, "2006/01/02") }}

This will format the article's update timestamp2024/07/29.

Deep understanding of time formatting strings (Golang style)

AnQiCMS continued to use the unique time formatting method of the Go language. Unlike many programming languages that use the symbol placeholder 'Y-m-d H:i:s' to define the time format, the Go language uses a fixed reference time2006-01-02 15:04:05auto

  • 2006Represents the year (Year)
  • 01Represents the month (Month), a two-digit number with a leading zero
  • 02Represents the day (Day), a two-digit number with a leading zero
  • 15Represents hour (Hour), 24-hour format, two-digit number with a leading zero
  • 04Represents minute (Minute), two-digit number with a leading zero
  • 05Representing seconds (Second), two-digit numbers with a leading zero.

By combining these reference numbers, you can create various time display formats:

  • "2006": Only displays the year,2024
  • "01-02":Display month and day, such as07-29
  • "2006-01-02":Standard date format, such as2024-07-29
  • "2006年01月02日":Chinese date format, such as2024年07月29日
  • "15:04":Hour and minute, such as10:30
  • "2006-01-02 15:04:05":Full date and time, such as2024-07-29 10:30:45

Mastered this reference time format of the Go language, you can freely control the display of time and date in AnQiCMS templates.

Summary

Whether it is to display the current year or time, or to format the timestamp stored in the database, AnQiCMS provides intuitive and powerful template tags and functions. By{% now %}Tags and{{ stampToDate() }}Functions, with the unique but flexible time formatting strings of the Go language, you can easily implement various dynamic time information display in website templates, providing users with a better browsing experience.

Common Questions (FAQ)

  1. Why is the time format string '2006-01-02 15:04:05' this kind of number combination, not 'Y-m-d H:i:s'?AnQiCMS uses the time formatting standard of the Go language. In Go, unlike other languages, there is no use of letters to represent time units (such as Y representing a year), but rather a fixed reference time2006年1月2日 15点04分05秒come as a formatting template. Each number you enter in the formatted string (such as2006/01/02auto