How to display the current year or a custom formatted datetime in the template?

Calendar 👁️ 84

In website operation and content management, displaying accurate date and time is a basic and important requirement.Whether it is the copyright year at the footer, the publication or update date of the article, or the countdown for a specific event, flexible control over date and time formats can greatly enhance user experience and the professionalism of the website.In AnQiCMS, you can easily implement the display of the current year or a custom date and time format in templates using several simple and practical methods.


1. Display the current system year or date and time by using{% now %}Tag

When you need to display the current year of the website, for example, in the footer copyright information, or need to display the current time to the minute and second,{% now %}The tag is a very convenient choice. This tag will get the current system time of the AnQiCMS server and output it in the specified format.

The basic usage is:

{% now "格式化字符串" %}

Here“格式化字符串”It is crucial. AnQiCMS uses Go language date and time formatting rules, which may be different from the formats you are familiar with such as 'YYYY-MM-DD HH:mm:ss'.Go language uses a specific reference point2006-01-02 15:04:05Come as a formatting template. You need to replace each number in this reference timestamp with the actual date and time components that need to be displayed.

For example, if you want to display the current year, just2006Replace2006:

© {% now "2006" %} 版权所有

This will be displayed in the footer of your website as© 2023 版权所有(assuming the current year is 2023).

If you need to display the current complete date, you can do it like this:

今天是:{% now "2006年01月02日" %}

Or, if you need a more precise time, including hours and minutes:

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

By flexible combination2006(Year),01(Month),02(Day),15(Hour),04(Minute),05(Second) elements, you can customize various date and time formats.


Secondly, format the date and time in the content: usingstampToDatefunction

In AnQiCMS, many content fields, such as the articles'CreatedTime(creation time) andUpdatedTime(Updated time), it is stored in the database in the form of a timestamp (usually a 10-digit or 13-digit number).Directly displaying these timestamps is not user-friendly. At this point, we needstampToDatefunction to convert these timestamps into a readable date and time format.

stampToDateThe usage of the function is:

{{ stampToDate(时间戳变量, "格式化字符串") }}

Similarly, here is“格式化字符串”Also follow the Go language2006-01-02 15:04:05reference format.

Suppose you are looping through the article detail page or article list page anditemIs the variable of the current article, you can display the publication date of the article like this:

<p>发布日期:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}</p>

If you want to display more detailed publication time on the article detail page, including hours, minutes and seconds:

<p>发布时间:{{ stampToDate(archive.CreatedTime, "2006-01-02 15:04:05") }}</p>

Or only show the month and date:

<p>发表于:{{ stampToDate(item.CreatedTime, "01月02日") }}</p>

In addition, if you only want to extract the year from the timestamp, for example, to display a brief year in the article list:

<p>年份:{{ stampToDate(item.CreatedTime, "2006") }}</p>

stampToDateThe power of functions lies in their ability to convert any valid timestamp into the precise format you need, thus meeting the needs of different content displays.


3. Summary and Tips

AnQiCMS uses{% now %}Tags andstampToDateFunctions, providing flexible and diverse date and time display and formatting capabilities for you.

  • {% now %}Tag: Suitable for scenarios that require displaying the current system time (such as copyright year, real-time time).
  • stampToDatefunction: Suitable for scenarios where you need to format timestamps stored in content (such as article publication/update time).

A tip.: The date format string in Go language indeed needs some time to adapt. Remember2006年01月02日 15时04分05秒This reference date, what do you want to display, replace it with2006/01/02corresponding numbers. For example, if you want to display a two-digit year, use06; if you want to display a two-digit month, use01. Practice is the best way to learn, you can try different combinations in your own AnQiCMS template and see the effect.

Master these methods, and you can better control the date and time display of website content, enhancing the overall professionalism and user experience of the website.


Frequently Asked Questions (FAQ)

  1. Ask: Why is the date formatting string of AnQiCMS2006-01-02 15:04:05in this form instead of the commonYYYY-MM-DD?Answer: This is because AnQiCMS is developed based on Go language. Go language does not use likeYYYY-MM-DDThis symbol placeholder is instead used a fixed reference time2006年01月02日15时04分05秒Define the format. You only need to replace the date and time part you want to display with the corresponding number in this reference time (for example, the year is2006, the month is01, the hour is15),The system will output the corresponding date and time based on your template.

  2. Question: I only want to display the current year in the footer, for example2023How should I write it?Answer: You can use{% now "2006" %}Label. This will directly retrieve the current year of the server and display it. For example, the complete copyright information may be&copy; {% now "2006" %} 您的网站名称。

  3. Ask: If I want to display the publication time of the article, the format is2023年6月30日 12点30分How do I do that?Answer: You can combinestampToDatefunction and formatting rules of Go language to implement. Assuming the article publish time is stored initem.CreatedTime, you can write it like this:{{ stampToDate(item.CreatedTime, "2006年1月2日 15点04分") }}. Note that,1represents the month without a leading zero,2Represents a date without a leading zero, which provides greater flexibility.

Related articles

How to define temporary variables in AnQiCMS templates to control display according to requirements?

In website operation and content management, we often need to flexibly display content based on specific conditions, or temporarily process and store some data at a certain stage of the template.AnQiCMS provides a powerful and easy-to-use template engine, which draws on the syntax of Django templates, allowing us to conveniently define and use temporary variables, thereby achieving fine-grained control over page content.AnQiCMS template files use the `.html` suffix and support Django template engine tag syntax.

2025-11-08

How to automatically convert a plain text URL into a clickable hyperlink in content display?

It is crucial to provide users with a smooth and convenient browsing experience in website operation.When our articles, pages, or product descriptions contain URLs in plain text form, if users need to manually copy and paste to access them, it will undoubtedly greatly reduce their user experience.AnQi CMS is well-versed in this, providing a simple and efficient method to automatically convert these plain text URLs into clickable hyperlinks, thereby bringing the content to life.### Let plain text URLs automatically come alive: The hyperlink conversion technique in Anqi CMS Imagine that

2025-11-08

How to truncate the text or HTML content that is too long in the template and add an ellipsis to keep the page tidy?

It is crucial to maintain the neat and consistent layout of the page content in website operation.Especially when it is necessary to display abstracts of articles, product descriptions, or news titles, the length of the original content is often uneven, which is likely to cause page layout to be out of order and affect the reading experience of users.AnQiCMS provides very practical template filters that can help us easily truncate long text or HTML content and automatically add ellipses, keeping the page always elegant.### The Importance of Understanding Text Truncation Imagine, in a list of articles, some of the summaries are very short

2025-11-08

How to safely display user-submitted HTML content or JS code in a template to prevent XSS attacks?

In website operation, we often need to handle various types of content submitted by users, such as comments, messages, article submissions, etc.This content may contain HTML tags and even JavaScript code. If displayed directly on a webpage, it may introduce cross-site scripting attacks (XSS), posing a security risk to the website.AnQiCMS was designed with comprehensive consideration of content security and provides corresponding mechanisms at the template level to help us effectively prevent such risks.AnQi CMS uses a template engine syntax similar to Django

2025-11-08

How to use the AnQiCMS random text generation feature to fill placeholder content during the development stage?

In the process of website project development, we often face a common challenge: how to effectively build and test the layout and style of a website in the absence of actual content?From design prototypes to frontend development, filling in real content is time-consuming and impractical, and simple placeholder text is difficult to simulate the visual effects of real content.

2025-11-08

How to solve the problem of page garbled display caused by incorrect encoding of AnQiCMS template files?

During the process of building a website with AnQiCMS, you may encounter situations where the page display appears garbled, which undoubtedly affects user experience and reduces the value of the carefully crafted page.Generally, such problems are caused by incorrect encoding settings in the template file.This article will detail how to troubleshoot and resolve the problem of garbled display caused by encoding issues in AnQiCMS template files, ensuring that your website content is clear and accurate.### Understanding the Root Cause of Garbled Characters Garbled characters, simply put, are the result of computers using the wrong encoding method to interpret data

2025-11-08

How to ensure that each site's content can be displayed independently in multi-site management?

In today's increasingly complex digital operation, many companies or individuals often need to manage multiple websites, which may be show sites of different brands, or independent content platforms for different markets or business lines.In response to such needs, AnQiCMS's multi-site management feature provides an efficient and powerful solution, allowing users to unify the operation and maintenance of multiple websites in a single system.However, while enjoying the convenience of management, ensuring that each site's content can be displayed independently without interference is one of the most concerns for operators.

2025-11-08

When the custom URL does not work, how should I investigate the content display problem?

In website operation, a clear and easy-to-understand URL structure is crucial for search engine optimization (SEO) and user experience.AnQiCMS (AnQiCMS) offers powerful custom URL functionality, allowing you to flexibly configure links based on content models, categories, articles, etc.However, you may sometimes find that the custom URL set up in the background does not display as expected on the frontend page, or that clicking on it results in a 404 error.When encountering this situation, we can follow the following steps to troubleshoot, locate, and solve the problem step by step.

2025-11-08