In website operation, we often need to dynamically display the current year or time, such as showing the latest copyright year at the bottom of the website, or accurately marking the generation time of the page 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.

Dynamic display of the current year or time: usenowtags

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

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

Here are some commonly used ones.nowExample of labels and their output:

  • Display the current year only: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 display on the page may be:今天是:2023-10-26

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

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

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

  • Custom format display:You can combine freely as needed, for example, display “Year/Month/Day Weekday Hour:Minute”:“

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

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

In addition,nowThe tag also has an optional parameterfakeUsed to force display 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: usingstampToDatefunction

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

This function accepts two parameters: a 10-digit timestamp (for example, fromarchive.CreatedTimeoritem.UpdatedTimeGet),as well as a Go language formatting string with the samenowlabel format string.

The following are somestampToDatecommon examples of functions:

  • Formatting the article creation date:In the article detail page or list page, you can display the article's publish date like this:

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

    The display on the page 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 display on the page may be:最后更新:2023-10-25 18:00

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

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

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

archive.CreatedTimeanditem.UpdatedTimeis a common variable in AnqiCMS templates, they represent the timestamp of the creation time and update time of the current document (or some entry in the list).

Actual application scenarios

  • Website footer copyright information:One of the most common uses, ensure that 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 or on the side of the article title:

    <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, display the real-time nature of data updates on a list page or tool page:

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

PassnowTags andstampToDateFunction, AnqiCMS template provides powerful flexibility in handling date and time display, whether it is to obtain the current time or format an existing timestamp, it can handle it easily, keeping your website content timely and professional.

Common Questions (FAQ)

1. Why is the date format string in AnqiCMS templates2006-01-02 15:04:05Instead of the common form,YYYY-MM-DD?

This is because AnqiCMS's template engine is based on Go language, and the Go language uses a fixed reference time for date and time formatting2006-01-02 15:04:05.999999999 -0700 MST(i.e. January 2, 2006, 3:04:05 PM) 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 as in other languages.2006,instead ofYYYY.

2. I can directly in{{ ... }}in{% now "2006" %}such syntax?

No. In AnqiCMS templates,{{ ... }}is used to output the value of variables,{% ... %}and is used to execute control logic or call tags.nowis a label, so it must be used{% now ... %}syntax to call. If you want to assignnowthe result of the label to a variable and output it, you can use{% set current_year = now "2006" %}Then in{{ current_year }}and output it directly,{% now "2006" %}is 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 full date and time?

You only need to refer to the formatting layout of the Go language, find the corresponding time element, and place it in your formatting string. For example: