How to display the current year or a specified format of time in the template?

Calendar 👁️ 74

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

Related articles

How to obtain the background custom global parameters in the template?

In AnQi CMS, the flexibility and maintainability of the website are one of its core advantages.Content operators and developers often need to dynamically adjust some global information of the website without modifying the template code, such as the company name, contact number, and even some marketing campaign links.These are the background custom global parameters that are the key to achieving this requirement.Today, let's delve into how to easily obtain and display these custom global parameters configured in the Anqi CMS template.### Why do we need global parameters? Imagine that

2025-11-08

How to customize or override the JSON-LD structured data in AnQi CMS?

The AnQi CMS has always been committed to providing efficient and flexible solutions in content operation and search engine optimization.Structured data, especially JSON-LD, is an important means to improve the visibility of website content in search engines.It can help search engines understand the page content more accurately, thereby giving it the opportunity to display richer search results (i.e., rich media summaries) and attract more users to click. AnQi CMS understands the importance of structured data, and has preset some default JSON-LD structured data for you in the system design.

2025-11-08

How to display the Banner slideshow image on the homepage or a specific group?

The homepage and banner carousel images of specific groups, which are key elements in attracting visitors and displaying core content on the website.A convenient and flexible system for management and display, which can greatly improve the operation of your website.AnQiCMS (AnQiCMS) takes advantage of its intuitive design and powerful template tag features, allowing you to effortlessly handle these visual elements, whether it's creating an eye-catching homepage image or customizing exclusive promotional images for specific category pages, you can do so with ease.

2025-11-08

How to implement language switching functionality for multilingual sites in Anqi CMS?

When our website is aimed at serving global customers, providing multilingual support has become an indispensable feature.This can not only help us expand the international market, but also significantly improve the access experience of users in different languages.AnQiCMS (AnQiCMS) fully understands this need, therefore, it has made multilingual support one of its core highlights, allowing us to easily build multilingual websites and switch languages.How to implement language switching for multi-language sites in AnQi CMS

2025-11-08

How to customize URL rewrite rules in AnQi CMS to optimize SEO?

In website operation, the website structure is not just a string of characters for accessing pages, but also a key factor in the website's performance in search engines.A clear, meaningful, and search engine-friendly URL (Uniform Resource Locator) that can significantly improve the SEO effect of the website, helping the content to be discovered and understood more easily.AnQiCMS (AnQiCMS) knows this and therefore integrated powerful pseudo-static and URL customization features into the system design from the beginning, allowing website operators to flexibly configure URL rules to meet different SEO strategy needs.

2025-11-08

How to set custom URL aliases for articles, categories, and single pages?

On the day when content management is increasingly important, a clear and friendly URL address can not only help search engines better understand the content of the page, improve the SEO performance of the website, but also provide users with a better browsing experience, and even help in brand promotion.AnQiCMS understands this and provides a powerful and flexible custom URL alias feature, allowing you to easily set personalized addresses for articles, categories, single pages, and even tags on your website.Next, we will explore how to finely manage these URL aliases in AnQiCMS.### Core Premise

2025-11-08

How to convert string case or capitalize the first letter in SafeCMS?

In website content operation, the way text is presented often affects the user's reading experience and brand image.In order to unify brand style, improve the readability of the title, or highlight text in specific scenarios, it is a common requirement to convert strings to uppercase or capitalize the first letter.AnQiCMS (AnQiCMS) with its flexible Django template engine syntax, provides us with several concise and efficient methods for handling these string formatting tasks.These operations do not require complex backend code modifications, just apply the corresponding filters in the template file, and you can easily achieve it

2025-11-08

How to truncate strings or HTML content in templates and add ellipses?

When operating a website, we often encounter such situations: the summary of an article is too long, causing the page layout to be disordered;The product description is detailed but seems redundant on the list page;Or it might be a title that is too long, taking up space for other elements.These seemingly minor issues can affect the user experience and the overall beauty of the page.Fortunately, AnQiCMS (AnQiCMS) has provided us with very convenient and powerful template tags, which can easily solve the needs of content truncation and adding ellipses, making our website content display more appropriate and professional

2025-11-08