In website operation, the accuracy of time information and the display method have a significant impact on user experience and content professionalism.Whether it is the copyright year of the footer or the publication or update time of the article content, a clear and consistent date format can enhance the overall perception of the website.AnQiCMS provides very flexible and powerful functions in the template to meet these time display requirements.

Display the current year: Use{% now %}Label quick presentation

Many website footers display the current year as part of the copyright statement, for example, “© 2023 AnQiCMS”.In AnQiCMS templates, getting and displaying the current year is a very simple task.{% now %}Label to achieve this.

The power of this label lies in the fact that you can directly specify the output format. For example, to display the current year, simply add it to the template file.{% now "2006" %}.Here “2006” does not refer to a fixed year, but is a special reference value used in Go language for date and time formatting, representing the year.If you want to display '2023' on the page, you can achieve this by writing the tag in this way.

Except for the year,{% now %}Labels can also easily retrieve and format other current time information. For example, if you want to display the current full date, such as “2023-01-02”, you can set the format string to{% now "2006-01-02" %}If you need a more detailed time, such as “2023-01-02 15:04:05”, then the corresponding format string is{% now "2006-01-02 15:04:05" %}This concise and direct way allows you to quickly display the current time in the required format on the page.

Formatting timestamp: with the help of{{ stampToDate() }}function fine control

In handling website content, such as the release or update time of articles or products, this information is usually stored in the database in the form of timestamps (a long number representing the seconds since the Unix epoch). To convert these numbers into user-friendly date and time formats, AnQiCMS provides{{ stampToDate() }}Template function.

stampToDate()The usage of the function is{{ stampToDate(时间戳, "格式") }}.You need to pass a 10-digit timestamp as the first argument and then provide a format string supported by the Go language as the second argument to define the output time style.

For example, when displaying a list of articles or details, you can getitemorarchivefrom the objectCreatedTime(Creation time) andUpdatedTime(Updated time), these are timestamps.

  • To translate the article'sCreatedTimeDisplay as a concise '2023-01-02', you can write it like this:{{ stampToDate(item.CreatedTime, "2006-01-02") }}.
  • If you need to include the hour, minute, and second, such as “2023-01-02 15:04:05”, then the format string can be adjusted to{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05") }}.
  • Further more, you can also customize it to have a format with Chinese, such as “2023年01月02日”, then the format string should be{{ stampToDate(item.CreatedTime, "2006年01月02日") }}.

stampToDate()The flexibility of functions lies in that it allows you to freely combine years, months, dates, hours, minutes, seconds, and weeks, etc. according to the date and time layout rules of the Go language, to create any specific time display format.

Flexible use of time information: enhance website user experience and professionalism.

Properly displaying time information is a key element in enhancing the user experience and professional image of a website.An article clearly marked with the release or update time can help readers better judge the timeliness of the content.The footer copyright year is updated, indicating the active maintenance of the website.

  • The website footer dynamically displays the current year.
  • The article list and detail pages accurately display the publish/update date.
  • The activity page displays the date before or after the start or end.
  • Email subscription or other operations that require time recording.

Whether it is to directly obtain the current time or to store the timestamp in a formatted manner, AnQiCMS provides intuitive and powerful tools to meet your content operation needs, making your website show professionalism in every detail.

Common Questions (FAQ)

Q1:{% now %}Tags and{{ stampToDate() }}What are the differences between functions, and how should I choose?A1:{% now %}The label is mainly used to directly obtain the current time of the server in the template, for example, for the copyright year in the footer or to display "Today's date".It does not accept any timestamp as input, and directly outputs the current time format you specify.{{ stampToDate() }}The function is used to format existing timestamps (usually obtained from databases, such as the publication time and update time of articles).You need to pass a timestamp as its first parameter.{% now %}How to format a timestamp for a specific time point?{{ stampToDate() }}.

Q2: The time formatting string (such as "2006-01-02 15:04:05") in Go language looks very special, how can I understand and use it?A2: The time formatting in Go language is indeed different from that commonly seen in other programming languages.Y-m-d H:i:sThe equal sign. It uses a fixed reference time:2006年01月02日 15时04分05秒 -0700 MST(This corresponds to the birthday of the Go language).You do not need to remember the specific meaning of each number and letter, just remember this complete 'layout', and then replace the time element you want to display with the corresponding part of this reference layout.2006; To display a two-digit month, write01To display two-digit hours (24-hour format), write15In this way, you can "describe" any date and time format you want.

Q3: Besides the creation and update time of the article, what other scenarios can I apply these time formatting functions to?A3: The time formatting feature is very useful in many aspects of the website. In addition to article time, you can find it:

  1. Footer copyright information:Use{% now "2006" %}Dynamically display the current year.
  2. Event page:If there is a timestamp for the start or end time of an event, you can use{{ stampToDate(活动开始时间, "2006年01月02日") }}to display the specific date of the event.
  3. User Center:User's registration time, last login time, etc., if stored in timestamp format, it can also be used forstampToDatebeautiful display.
  4. Data reports or charts:If you need to display a date range or a specific time point in a certain format, you can flexibly use these features. Actually