In website operation, it is often necessary to keep the web page content up to date or accurately display the time of specific events, whether it is the copyright year in the footer, the publication date of the article, or the update time of the product, these dynamic time information can make the website look more professional and active.AnQiCMS provides a very flexible and intuitive way, allowing you to easily display the current year or specified format of time in templates.

Next, we will explore two main methods together to allow your AnQiCMS website to flexibly display various time information.

One, display the current year or real-time time in a specified format: use{% now %}Tag

When you need to display the current year at any location on the website, such as in the copyright statement of the website footer, or in some dynamic module to display the current full date and time, AnQiCMS provides a concise{% now %}.

This tag is used to get and format the current system time.

To display the current year, you just need to add this line of code to the template file.

{% now "2006" %}

So, your website footer can automatically update to the current year without manual modification every year.

If you need to display more specific and real-time dates or times, such as:

  • Display the complete current date (year-month-day):

    {% now "2006-01-02" %}
    

    Effect:2023-10-27

  • Display the current time with hours, minutes, and seconds:

    {% now "2006-01-02 15:04:05" %}
    

    Effect:2023-10-27 10:30:15

By adjusting{% now %}The format string after the label, you can make the current time in any style you want.

Format existing timestamps: using{{ stampToDate() }}Tag

In AnQiCMS, articles (archive), comments (comment) or users (user) often store publication or update time information, which is generally stored in the database in the form of a timestamp.When you want to display this data on the front-end page, you need to convert these timestamps into a readable date or time format.

At this time,{{ stampToDate() }}This built-in function comes into play. It can receive a timestamp and a format string, then output the formatted time.

For example, on the article detail page or article list page, you want to display the publication date of the article:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
        <div>
            文章标题:{{ item.Title }}<br>
            发布日期:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}
        </div>
    {% endfor %}
{% endarchiveList %}

Here, item.CreatedTimeThat is the timestamp of the article,"2006年01月02日"which is the output format we hope for. If the article'sCreatedTimeThe corresponding time is2023-10-27 10:30:15thenstampToDateThe function will display it as2023年10月27日.

Similarly, if you need to display the update time of the article, you can useitem.UpdatedTime:

更新时间:{{ stampToDate(item.UpdatedTime, "2006-01-02 15:04") }}

Deep understanding of time formatting: Golang style 'magic numbers'

You may have noticed that either{% now %}Or{{ stampToDate() }}, they have used time format strings like2006-01-02 15:04:05such number combinations instead of commonYYYY-MM-DDThis is actually a feature developed by AnQiCMS based on Go language.The Go language uses a fixed reference time point to define the format when performing time formatting, which is:

2006年1月2日 15点04分05秒 -0700 MST

You can understand this date and time as a set of "templates", by selectively using or combining the various numbers and characters to build the date and time format you want.

Here are some common correspondences and examples to help you master it quickly:

  • Year: 2006(Four-digit year) or06(Two-digit year)
  • Month: 01(Two-digit month),1(Month without leading zero),Jan(Month abbreviation),January(Full name of the month)
  • Date: 02(Two-digit date),2(Date without leading zero)
  • Hour: 15(24-hour format),03(12-hour format with leading zero),3(12-hour format without leading zero)
  • Minute: 04(two-digit minutes),4(minutes without leading zero)
  • Seconds: 05(two-digit seconds),5(seconds without leading zero)
  • AM/PM marker: PMorpm
  • Weekday: Mon(Monday,),Monday(Monday full name)

More formatted examples:

Target format Format string Example output (assuming the time is)}2023年10月27日 10点30分15秒)
Year-Month-Day (hyphen) "2006-01-02" 2023-10-27
Year-Month-Day (slash) "2006/01/02" 2023/10/27
Month-Day-Year "01/02/2006" 10/27/2023
Day-Month-Year "02/01/2006" 27/10/2023
Chinese Year-Month-Day "2006年01月02日" 2023年10月27日
hour, minute, second (24-hour format) "15:04:05" 10:30:15
hour and minute (12-hour format with AM/PM) "03:04 PM" 10:30 AM
day of the week "Monday" Friday
full date and time (including the day of the week) "Monday, 02 Jan 2006 15:04" Friday, 27 Oct 2023 10:30

By proficiently using these 'magic numbers', you will be able to easily handle various date and time display requirements.

Summary

In AnQiCMS, it displays the current year or time in a specified format, mainly relying on{% now %}and{{ stampToDate() }}these powerful tags.{% now %}Suitable for getting the real-time current time, and{{ stampToDate() }}It is used to format existing timestamps in the content. Mastering the Go language style of time formatting rules is the key to flexible use of these tags.By simple code, you can keep the time information on your website accurate and beautiful.


Frequently Asked Questions (FAQ)

  1. {% now %}and{{ stampToDate() }}What is the difference?
    • {% now %}Used to obtain and display the real-time date and time of the current server. It does not depend on any existing data, and it will display the latest time every time the page is loaded.
    • {{ stampToDate() }}Used to format timestamps stored in the database. For example, the publication time of an articlearchive.CreatedTimeor update timearchive.UpdatedTimeare all timestamps that need to bestampToDate()formatted in order to be human-readable