How to dynamically display the current year or current time in a template?

Calendar 👁️ 70

In website operation, we often need to dynamically display the current year or time, such as displaying the latest copyright year at the bottom of the website, or marking the generation time of the page accurately in articles.AnqiCMS provides flexible and powerful template functions, making these operations very simple.Next, we will discuss how to implement this requirement in the AnqiCMS template.

To dynamically display the current year or time: usingnowTag

AnqiCMS provides a very convenient built-in tagnowUsed to dynamically obtain and display the current date or time during template rendering. The key to this tag lies in its time formatting string, which follows the unique date and time format representation of the Go language, rather than the ones commonly used in PHP or JavaScript.Y-m-d H:i:sThis format.

In Go language, formatting strings are actually a specific reference time:2006年01月02日 15时04分05秒. You need to match the "layout" of the time element you want to display with the corresponding part of this reference time.

Here are some commonly usednowSign examples and their output:

  • Only show the current year:If you want to display the copyright year at the footer of the website, you can use it directly:

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

    It will be displayed on the page as:© 2023 版权所有。(Assuming the current year is 2023)

  • Display the full date:If you need to display the current full date on the page, for example年-月-日Format:

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

    The page display may be:今天是:2023-10-26

  • Display the full date and time:If you need to include the time to the second:

    页面加载于:{% now "2006-01-02 15:04:05" %}
    

    The page display may be:页面加载于:2023-10-26 10:30:45

  • Custom format display:You can combine them as needed, for example, displaying "Year/Month/Day Week Hour:Minute":

    更新时间:{% now "2006/01/02 Mon 15:04" %}
    

    The page display may be:更新时间:2023/10/26 Thu 10:30

Furthermore,nowThe tag also has an optional parameterfakeUsed to force a fixed reference time during development and debugging, which is convenient for style debugging or data display, for example:

测试时间:{% now "2006-01-02 15:04:05" fake %}

This will display the reference time of the Go language itself, not the current system time.

Format an existing timestamp: usestampToDatefunction

In addition to displaying the current time, we often need to display information such as the article publication time and update time.These times are usually stored in databases in the form of Unix timestamps.AnqiCMS providedstampToDateA function to help us format these timestamps into readable dates or times.

This function accepts two parameters: a 10-digit timestamp (such as fromarchive.CreatedTimeoritem.UpdatedTimeAccess), and a string formatted with the samenowtag in Go language formatting.

The following are somestampToDatecommon examples of functions:

  • Format the creation date of the article:On the article detail page or list page, you can display the article's publish date like this:

    发布于:{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}
    

    The page display may be:发布于:2023年09月15日

  • Format the article update date and time:If you need to display the exact update time:

    最后更新:{{ stampToDate(item.UpdatedTime, "2006-01-02 15:04") }}
    

    The page display may be:最后更新:2023-10-25 18:00

  • Only display time:You can only extract the time part to display:

    具体时间:{{ stampToDate(archive.CreatedTime, "15:04") }}
    

    The page display may be:具体时间:10:30

archive.CreatedTimeanditem.UpdatedTimeIs a common variable in AnqiCMS templates, representing the timestamp of the creation time and update time of the current document (or an entry in the list).

Application scenarios in practice

  • Website footer copyright information:One of the most common uses, ensuring the copyright year is automatically updated:

    <footer>
        <p>&copy; {% now "2006" %} All Rights Reserved. {{ system.SiteName }}</p>
    </footer>
    
  • Article publishing/update metadata:Display key time information below the article title or in the sidebar:

    <div class="article-meta">
        <span>发布日期:{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}</span>
        <span>更新日期:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04") }}</span>
    </div>
    
  • Dynamic page prompt:For example, displaying the real-time update of data on a list page or tool page:

    <p>数据最后更新于:{% now "2006-01-02 15:04" %}</p>
    

BynowTags andstampToDateFunction, AnqiCMS templates provide powerful flexibility in handling date and time display, whether it is to get the current time or format existing timestamps, it can easily cope with it, keeping your website content timely and professional.

Frequently Asked Questions (FAQ)

1. Why is the date formatting string in the AnqiCMS template2006-01-02 15:04:05in this form instead of the commonYYYY-MM-DD?

This is because the AnqiCMS template engine is based on Go language, and the Go language uses a fixed reference time for date and time formatting function2006-01-02 15:04:05.999999999 -0700 MST(That is, at 3:04:05 PM on January 2, 2006) to define the layout.You need to match the date and time elements you want to display (such as year, month, day, hour, minute, second) with the corresponding numbers in this reference time, rather than using placeholders like other languages.For example, to display the year, you write2006instead ofYYYY.

2. Can I use this syntax?{{ ... }}is used{% now "2006" %}Can I use this syntax?

No. In AnqiCMS templates,{{ ... }}used to output variable values, while{% ... %}used to execute control logic or call tags.nowIt is a tag, so it must be used{% now ... %}to call the syntax. If you want to assignnowthe result of the tag to a variable and output it{% set current_year = now "2006" %}Then{{ current_year }}output it directly but not{% now "2006" %}usually more concise.

3. How can I display the current month, day of the week, or other specific time elements, in addition to showing the year and complete date and time?

You just need to refer to the Go language's formatting layout, find the corresponding time element, and put it into your formatting string. For example:

Related articles

How to safely display rich text content containing HTML tags on the front-end page?

In website operation, we often need to display rich content that includes images, links, bold text, and so on, which is known as rich text content.But it is a practical and careful task to safely display these rich text with HTML tags on the front-end page.AnQi CMS as an efficient content management system, fully considers this point, and provides us with a clear solution.

2025-11-07

How to use logical tags like `if` and `for` in templates to control conditional and repeated display of content?

In website content management, displaying dynamic and diverse content is the key to improving user experience and website attractiveness.AnQiCMS (AnQiCMS) provides a powerful template system, drawing inspiration from the Django template engine syntax, allowing you to easily control the display of content through logical tags.Among them, the `if` tag is used for conditional judgment, while the `for` tag is used for loop iteration, they are the foundation for building flexible and variable web pages.

2025-11-07

How to truncate the title or abstract of an article and display it with an ellipsis?

In website content operation, titles and summaries are important elements to attract visitors to click and learn more details.Especially on list pages, recommendation positions, or the homepage, the limited space requires us to refine and cut these contents.To maintain the neatness and aesthetics of the page while fully conveying the core information, it is a common optimization strategy to truncate overly long article titles or summaries and end them with an ellipsis "..."}AnQiCMS (AnQi CMS) provided us with flexible and powerful tools to meet this requirement.

2025-11-07

How to display the custom parameter list in the article content on the front page?

AnQiCMS as an efficient and customizable content management system provides powerful content model features, allowing us to add various custom fields to articles according to different business needs.These custom parameters not only make the article content more rich and structured, but also allow for personalized display on the front page, greatly enhancing the flexibility and user experience of the website.Today, let's delve into how to flexibly display these custom parameter lists in the AnQiCMS front page.

2025-11-07

How to reflect the high concurrency characteristics of the Go language in the front-end page loading speed?

In today's fast-paced online world, website loading speed has become a core factor in user experience and search engine rankings.Nobody likes to wait for a slow page, and search engines tend to rank websites that respond quickly higher.How can you make your website lightning fast?For users of AnQiCMS, the answer is hidden in its powerful Go language underlying architecture, especially the high concurrency features that Go language boasts.

2025-11-07

How to reference and display external static resources (such as CSS, JS) in the template?

Referencing and displaying external static resources in AnQiCMS templates, such as CSS style sheets and JavaScript scripts, is a key step in building a functional and visually appealing website.Understand the working principle and recommended practices, which can make our website development work more efficient and flexible.

2025-11-07

How to define and use variables for content display in AnQiCMS templates?

AnQiCMS (AnQiCMS) boasts its efficient architecture based on the Go language and flexible template mechanism, making content presentation both powerful and intuitive.In website content operation, we often need to display dynamic information, such as article titles, product prices, contact information, etc., which is inseparable from defining and using variables in templates.Understanding how to flexibly use variables in the AnQiCMS template is the key to building and maintaining an efficient, personalized website.The AnQi CMS template engine has borrowed the syntax from Django, making it very easy to understand the definition and reference of variables. Essentially

2025-11-07

How to categorize image resources and display them on the front end according to categories?

Images are an indispensable part of a website's content, as they can not only beautify the page but also effectively convey information and enhance the user experience.However, with the increase in the number of images on a website, how to efficiently manage these image resources and flexibly display them on the front page as needed has become a challenge for many website operators.AnQiCMS (AnQiCMS) is well-versed in this field, providing a user-friendly and powerful image resource management system that helps us easily manage these visual elements.

2025-11-07