In website operation, the accuracy and display of time information has a significant impact on user experience and content professionalism.Whether it is the copyright year at 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 needs.
Display the current year: Use{% now %}Tags to quickly present
Many websites display the current year as part of the copyright notice in the footer, for example “© 2023 AnQiCMS”.In the AnQiCMS template, obtaining and displaying the current year is a very simple task.You can use built-in{% now %}To achieve this with a tag.
The power of this tag 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 label in this way.
Except for the year,{% now %}Labels can 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.
Format timestamp: with the help of{{ stampToDate() }}function for fine control
When dealing with website content, such as the publication 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 parameter, and then provide a Go language-supported format string as the second parameter to define the output time style.
For example, when displaying a list of articles or details, you can get fromitemorarchivethe objectCreatedTime(creation time) andUpdatedTime(update time), these are timestamps.
- To set the article's
CreatedTimeDisplay 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, for example “2023-01-02 15:04:05”, you can adjust the format string to
{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04:05") }}. - Further, you can also customize it to be formatted with Chinese, for example, '2023/01/02', then the format string should be
{{ stampToDate(item.CreatedTime, "2006年01月02日") }}.
stampToDate()The flexibility of the function lies in its ability to freely combine year, month, day, hour, minute, second, and week elements according to the Go language date-time format rules to create a time display format that meets any specific requirements.
Use time information flexibly: improve the user experience and professionalism of the website.
Properly displaying time information is a key factor in enhancing the user experience and professional image of a website.An article clearly marked with the publication or update time can help readers better judge the timeliness of the content.The copyright year in the footer is updated, which reflects the active maintenance of the website.Through these time processing features provided by AnQiCMS, you can easily achieve:
- The footer of the website dynamically displays the current year.
- The article list and detail page accurately display the publication/update date.
- The activity page displays the date from the start or end.
- Email subscription or other operations that require time recording.
Whether it is directly obtaining the current time or formatting the timestamp, AnQiCMS provides intuitive and powerful tools to meet your content operation needs, allowing your website to show professionalism in every detail.
Frequently Asked Questions (FAQ)
Q1:{% now %}Tags and{{ stampToDate() }}What are the differences between functions, and how should I choose?A1:{% now %}The tag is mainly used to directly obtain the current time of the server in the template, for example, for displaying the copyright year in the footer or showing the "Today's date".It does not accept any timestamp as input, instead it 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 or update time of articles).You need to pass a timestamp as the first parameter.In short, to get the 'current' time using{% now %}How to format the timestamp of a specific time point?{{ stampToDate() }}.
Q2: The time formatting string in Go language (such as "2006-01-02 15:04:05") looks very special, how can I understand and use it?A2: The time formatting in Go language is indeed different from that commonly used in other programming languagesY-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 meanings of each number and letter, just remember the complete "layout", and then replace the time element you want to display with the corresponding part of this reference layout.For example, to display a four-digit year, write2006; To display a two-digit month, write01; To display hours with two digits (24-hour format), you write15. In this way, you can 'depict' any date and time format you wish.
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 the article time, you can:
- Footer copyright information:Use
{% now "2006" %}Dynamically display the current year. - 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. - User center:User's registration time, last login time, etc., if stored in timestamp format, it can also be used
stampToDatefor beautification display. - Data reports or charts:If you need to display a time range or time point in a specific format, you can flexibly use these features. In fact