How to display the current year or time in a specific format in AnQi CMS template?

Calendar 👁️ 110

In website operation, it is a common requirement to flexibly display the current year or format specific time, whether it is the automatic update of the year in the copyright statement or the clear display of the publication time on the article detail page.AnQiCMS provides two main ways to meet these needs, they are both simple to use and follow the elegant time handling of the Go language.

Method one: Directly obtain and display the current year or time ({% now %}Label)

When you need to directly obtain and display the current year or the current time to the second in the template,{% now %}The tag is your preference. This tag is very intuitive, you just need to specify the Go language style time formatting string you want in it.The system will output according to the current time of the server, in the format you define.

For example, if you only want to display the current year in the website footer for copyright statements, you can write it like this:

© {% now "2006" %} 您的公司名称. All Rights Reserved.

After running, if the current year is 2024, it will automatically display as:© 2024 您的公司名称. All Rights Reserved.

If you need to display the full current date, for example “2024-07-29”:

今天的日期是:{% now "2006-01-02" %}

It will display as:今天的日期是:2024-07-29

If you need to display the current exact time, for example, "10:30:45":

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

It will display as:当前时间是:10:30:45

{% now %}The flexibility of the tag lies in its ability to output various forms of current time information based on your formatted string, which greatly facilitates the display of dynamic content.

Method two: Format the existing timestamp variable ({{ stampToDate() }}function)

More often than not, we are not displaying the current time, but rather need to display the timestamp stored in the database (such as the publication time of an articleCreatedTime, update timeUpdatedTimePresented in a human-readable format. AnQiCMS provides{{ stampToDate() }}Function.

This function accepts two parameters: the first is the timestamp variable you want to format (usually a 10-digit or 13-digit number), and the second is a time formatting string in Go language style.

Assuming you are iterating through a list of articlesarchivesYou would like to display the publish time of each article, formatted as "July 29, 2024 10:30 AM":

{% archiveList archives with type="list" limit="5" %}
    {% for item in archives %}
        <div>
            <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
            <p>发布于:{{ stampToDate(item.CreatedTime, "2006年01月02日 15时04分") }}</p>
        </div>
    {% endfor %}
{% endarchiveList %}

After running, the publication time of each article will be displayed in the specified Chinese format.

If you only want to display the date part, for example, "2024/07/29":

更新时间:{{ stampToDate(archive.UpdatedTime, "2006/01/02") }}

This will format the update timestamp of the article2024/07/29.

Understand time formatting strings (Golang style)

AnQiCMS continued to use the unique time formatting method of the Go language. Unlike many programming languages that use the symbol placeholder 'Y-m-d H:i:s' to define time format, Go language uses a fixed reference time2006-01-02 15:04:05Define the format. The formatting string you write is actually telling the Go language how to combine the time display effect you want according to the reference time.

  • 2006Represents the year (Year)
  • 01Represents the month (Month), a two-digit number with a leading zero
  • 02Represents the day (Day), a two-digit number with a leading zero
  • 15Represents hour (Hour), 24-hour format, two digits with a leading zero
  • 04Represents minute (Minute), two digits with a leading zero
  • 05Representing seconds (Second), two-digit numbers with a leading zero

By combining these reference numbers, you can create various time display formats:

  • "2006": Only shows the year, such as2024
  • "01-02": Display month and day, such as07-29
  • "2006-01-02": Standard date format, such as2024-07-29
  • "2006年01月02日": Chinese date format, such as2024年07月29日
  • "15:04": Hour and minute, such as10:30
  • "2006-01-02 15:04:05": Complete date and time, such as2024-07-29 10:30:45

Mastered this reference time format in Go language, you can freely control the display of time and date in AnQiCMS templates.

Summary

Whether it is necessary to display the current year or time or to format the timestamp stored in the database, AnQiCMS provides intuitive and powerful template tags and functions. Through{% now %}Tags and{{ stampToDate() }}Functions, combined with the unique and flexible time formatting strings of Go language, you can easily implement dynamic display of various time information in website templates, providing users with a better browsing experience.

Frequently Asked Questions (FAQ)

  1. Why is the time format string '2006-01-02 15:04:05' this kind of numeric combination, not 'Y-m-d H:i:s'?AnQiCMS adopts the Go language time formatting standard. In Go language, unlike other languages, no letters are used to represent time units (such as Y representing the year), but a fixed reference time is used2006年1月2日 15点04分05秒Come as a formatting template. Every number you write in the formatting string (such as2006/01/02These, as well as others, are all telling the Go language that you want to display the corresponding time units (years, months, days, etc.) at that position and in the format of that number.

Related articles

Does AnQi CMS provide an integrated JS statistics code calling tag for easy display on the page?

When using AnQiCMS, many users are concerned about how to conveniently integrate third-party JavaScript statistical codes into web pages, such as Baidu Statistics, Google Analytics, and so on.About this, AnQiCMS indeed provides built-in mechanisms and flexible template tags, allowing you to easily call and display these statistics codes on your website.### Using built-in JS code to call tags AnQiCMS to meet the needs of website operators to integrate third-party scripts

2025-11-08

How can AnQi CMS use the 'time factor' feature to display future published content?

How to efficiently and strategically publish content in today's fast-paced content environment is a question that every content operator is thinking about.Maintain the consistency and foresight of content publication, which can not only enhance the user experience but also effectively promote brand communication and search engine optimization.AnQi CMS deeply understands this, and its 'Time Factor' feature was born for this very purpose, providing content creators and operation teams with a precise tool to control the timing of content release.## Precise Planning: The Foundation of Content Operation We have all faced such a situation: writing an精心 prepared article

2025-11-08

How can AnQi CMS templates inherit parent templates and rewrite specific block content to customize display?

In Anqi CMS, the template system provides a powerful and flexible mechanism that allows users to efficiently customize the layout and content display of the website.Among them, "inheriting the parent template and rewriting specific block content" is a key function to achieve personalized design and maintain consistency with the overall style of the website. ### Core Concept: Say goodbye to repetition, embrace inheritance Imagine that the header, footer, sidebar, and other elements of a website are fixed and unchanged on almost every page. If there is no template inheritance mechanism, we may need to repeat this common code in each page template, which increases the difficulty of maintenance

2025-11-08

How to truncate a long string and display an ellipsis in AnQi CMS templates?

The flexibility of content display is crucial in the template making process of Anqi CMS.When it is necessary to display a long text within a limited space, such as an article abstract, product description, or brief introduction, truncating the strings with an ellipsis is a common optimization method. It can effectively maintain the neat layout of the page and guide users to click and view the full content.AnQi CMS is developed based on Go language, its template engine syntax is similar to Django, and it provides powerful filter (filters) functions that can easily meet this requirement.### Skillfully Utilize Filters

2025-11-08

How to add multi-dimensional parameter filtering function for the product list page in Anqi CMS and display the filtering results?

In modern e-commerce and content display websites, providing multi-dimensional parameter filtering functions for product lists has become a key factor in improving user experience and conversion rates.Visitors can quickly locate the products they need based on their own needs, not only saving time but also significantly improving the usability of the website.For users of AnQiCMS, implementing such a feature is not difficult. The built-in powerful functions and flexible template tags make the whole process intuitive and efficient.This article will delve into how to add multi-dimensional parameter filtering functions to the product list page in Anqi CMS

2025-11-08

How does AnQi CMS filter and display content based on the document's Flag attribute (such as headline, recommended)?

In AnQiCMS (AnQiCMS) content management system, effectively organizing and displaying content is the key to enhancing website attractiveness and user experience.Among them, the Flag attribute (recommended attribute) function of the document, as if marking the content with a special 'mark', allows you to flexibly filter and highlight important or specific types of content according to your operational needs.What is the document Flag attribute? The document Flag attribute, also known as the recommendation attribute, is a content classification and display mechanism provided by AnQi CMS.

2025-11-08

How to manage and display custom page Banner carousel in Anqi CMS?

In website operation, a beautiful page banner carousel is a key element to attract visitors' attention, convey important information, and enhance brand image.AnQiCMS provides a flexible and intuitive way to manage and display these visual contents, whether you want to set up a Banner for the homepage, a specific page, or a category page, it can be easily achieved.### Manage Custom Banner Carousel AnQi CMS allows you to set exclusive banners for different content types, mainly divided into banners for specific pages

2025-11-08

How to implement the like function for comments in Anqi CMS and update the display of likes in real time?

In website operation, user interaction is a key link in enhancing community activity.The like comment feature not only encourages user participation, but also helps website administrators identify popular comment content.For websites using AnQiCMS, to implement this feature and update the like count in real time, it can be done through the built-in template tags and a bit of front-end JavaScript code.### Comment Function Basics: How to Display Comment Lists The core of the comment function in AnQi CMS lies in the `commentList` template tag

2025-11-08