Displaying the date and time in the AnQiCMS template, formatted in a specific way, is a common requirement in website content operation.Whether it is to display the publication time of the article, the deadline of the activity, or show the current year in the footer, accurate and beautiful time information can enhance the user experience.AnQiCMS provides concise and efficient template tags, allowing you to easily implement these features.

Next, we will discuss in detail how to output the current date and time in the AnQiCMS template and specify the format you need.


1. UsestampToDateTag format timestamp

In website content management, the creation time, update time, and so on of articles are usually stored in the database in the form of timestamps.stampToDateLabels are designed to make it convenient to convert these timestamps into readable date and time formats.

When to use:When you need to display the date and time obtained from the database, for example, in timestamp formatarchive.CreatedTime(article publication time) orarchive.UpdatedTime(article update time) when,stampToDatetags are your first choice.

Syntax structure:

{{stampToDate(时间戳, "格式")}}

The core here is two parameters:

  1. Timestamp:A 10-digit Unix timestamp (second level). Provided internally by AnQiCMS.CreatedTimeandUpdatedTimeThe field is default 10-digit timestamp.
  2. Format:This is a string that defines the style of date and time output you wish. It is particularly important to note that the formatting rules of AnQiCMS (developed based on Go language) are different from those commonly used in PHP or Java et al.Y-m-dorYYYY-MM-DDPlaceholders differ, it adoptsFixed reference layout timeTo define the format. This reference layout is:2006年01月02日 15时04分05秒. You need to match the date and time format you expect with this reference layout.

Common format examples:

  • Show the year: "2006"
  • Show the month: "01"
  • Show the date: "02"
  • Display hour (24-hour format): "15"
  • Display minutes: "04"
  • Display seconds: "05"
  • Display year-month-date: "2006-01-02"
  • Display year/month/date: "2006/01/02"
  • Show year.month.day: "2006.01.02"
  • Show Chinese year-month-day: "2006年01月02日"
  • Show hour:minute: "15:04"
  • Show full date and time: "2006-01-02 15:04:05"

Code application example:

Assuming you are on the article detail page(archivethe object is available), you need to display the publication and update time of the article:

<p>
    发布时间:{{stampToDate(archive.CreatedTime, "2006年01月02日 星期一 15:04:05")}}
</p>
<p>
    最近更新:{{stampToDate(archive.UpdatedTime, "2006-01-02 15:04")}}
</p>
<p>
    文章年份:{{stampToDate(archive.CreatedTime, "2006")}}
</p>

This code will format the creation time of the article to include the day of the week in detail, format the update time to year-month-date hour:minute, and extract the published year separately.


Second, usenowTag to get the current system time

In addition to formatting the timestamp, you may also need to display the current system date and time directly in the template.For example, display the current year in the footer of the website or show the current time on some special pages.nowThe tag can meet this requirement.

When to use:When you need to display the current server date and time instead of the specific content time obtained from the database,nowtags are very convenient.

Syntax structure:

{% now "格式" %}

The "format" parameter is withstampToDateThe labels are completely the same, and they follow the Golang reference layout time formatting rules.

Code application example:

Assuming you want to display the current year and the complete date and time when the page was generated in the footer of the website:

<footer>
    <p>版权所有 &copy; {% now "2006" %} 您的公司名称。保留所有权利。</p>
    <p>页面生成于:{% now "2006-01-02 15:04:05" %}</p>
</footer>

This code will display the current year (e.g., "2023") and the exact date and time of page loading in the footer.


BystampToDateandnowThese tags, AnQiCMS provides powerful and flexible support for date and time output in templates.Mastering their usage and the unique formatting rules of Golang, you can easily display date and time information that meets various needs on the website.


Frequently Asked Questions (FAQ)

Q1:stampToDateThe "format" parameter looks like a date, I can write it directlyYYYY-MM-DD?

A1: It is not allowed. AnQiCMS is developed based on Go language, its time formatting mechanism is quite special, not usingYYYY-MM-DDSuch a placeholder. It requires you to use a fixed reference date and time2006年01月02日 15时04分05秒To define the format you want. Simply put, if you want to display "year-month-day", you need to write it as"2006-01-02"If you want to display "hour:minute", it should be written as"15:04". You need to match the output format you expect with the corresponding part of this reference time.

Q2: I tried using it in the template.{{ item.CreatedTime|date:"2006-01-02" }}Format the article creation time, but the page reported an error, why is that?

A2: dateThe filter expects the input to be in Go language in the AnQiCMS templatetime.TimeThe type of object. However, in the AnQiCMS templateitem.CreatedTimeis usually a Unix timestamp (integer). Therefore, it is used directly on the timestamp.dateThe filter will cause a type mismatch error. For timestamp variables, you should use specialstampToDatetags for formatting, such as{{stampToDate(item.CreatedTime, "2006-01-02")}}.

Q3:stampToDateWhat is the timestamp in the label, 10 digits or 13 digits? What if I get a 13-digit timestamp?

A3: stampToDateThe label mainly supports10 digitsThe Unix timestamp (seconds since January 1, 1970). It is used internally by the AnQiCMS system.CreatedTimeandUpdatedTimeThe fields also provide a 10-digit timestamp by default. If you receive a 13-digit millisecond timestamp, you need to divide it by 1000 to convert it to seconds (for example, in the backend controller), then you canstampToDatein the tag.