How can you dynamically obtain and display the current year or other time information in the AnqiCMS template?

Calendar 👁️ 75

In website operation, we often need to dynamically display the current year, date, or time on the page, such as the copyright information in the footer, or the publication or update time of articles.This information, if manually updated, is not only time-consuming and labor-intensive, but also prone to errors.Fortunately, AnqiCMS provides a concise and efficient method for easily obtaining and displaying these dynamic time information in templates.

Dynamically retrieve and display the current year or other time information

In the AnqiCMS template, to retrieve and display the current year or any format of the current time, we can use the built-in{% now %}The label is very direct. It will output the current date and time of the server according to the specified format.

For example, if you want to display the current year in the footer of the website, you can use it like this:

© {% now "2006" %} 您的网站名称。保留所有权利。

Here"2006"It is not an arbitrary year, but a special reference to the time format defined in Golang. For example, if you want to display the current full date,2023-10-26It can be written like this:

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

For example, if you need to display more detailed time information,2023-10-26 15:30:00:

当前时间:{% now "2006-01-02 15:04:05" %}

{% now %}The flexibility of the label lies in its ability to output any current time representation you want, based on the Golang time format string you provide.

Format an existing timestamp

In addition to displaying the current time, a more common need is to format the timestamps stored in the website content (such as articles, products). AnqiCMS will provide in document details, lists, and other tags.CreatedTimeandUpdatedTimeSuch timestamp fields. These fields are typically 10-digit Unix timestamps, so that they can be displayed in a more readable format for users, we can use{{ stampToDate(时间戳, "格式") }}This template function.

For example, on the detail page of a blog post, you may want to display its publication date and time:

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

This willarchive.CreatedTimeThis timestamp is converted to2023年10月26日 15:30This format. Similarly, if you want to display the update time:

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

Understand Golang time formatting rules deeply

The date and time formatting in AnqiCMS templates follows the specific rules of the Golang language. This is different from what we usually useYYYY-MM-DDThe pattern is different, and it may seem a bit special at first contact.

Golang uses a fixed reference time.2006-01-02 15:04:05.999999999 -0700 MSTDefine the time format. You need to replace the parts of this reference time with the format you want, rather than using placeholders.

Here are some commonly used reference mappings:

  • Year:2006-u003eYYYY(Four-digit year)
  • Month:01-u003eMM(Two-digit month, leading zero if necessary) orJan(Abbreviation in English) orJanuary(Full name in English)
  • Date:02-u003eDD(Two-digit date, leading zero if necessary)
  • hours:15-u003eHH(24-hour format) or03(12-hour format) or3(12-hour format without leading zeros)
  • minute:04-u003eMM(two-digit minutes, pad with zeros if necessary)
  • seconds:05-u003eSS(two-digit seconds, pad with zeros if necessary)
  • Week:Mon(Abbreviation in English) orMonday(Full name in English)
  • AM/PM:PM(and3used with the 12-hour clock)

Once you have mastered this mapping relationship, you can very flexibly combine various time formats. For example:

  • "2006/01/02"-u003e2023/10/26
  • "01-02"-u003e10-26(Display only month and day)
  • "15:04"-u003e15:30(Display only hour and minute)
  • "Monday, Jan 2, 2006"-u003eThursday, Oct 26, 2023(English weekdays, months)

Application scenarios in practice

  • Footer copyright informationEnsure that it updates automatically every year, without manually modifying the template file.
    
    <p>&copy; {% now "2006" %} {{ system.SiteName }} 版权所有。</p>
    
  • Timestamp on the article or product detail pageClearly display the publishing or latest update date of the content, enhancing user experience and content timeliness.
    
    <p>发布时间:{{ stampToDate(archive.CreatedTime, "2006年01月02日") }}</p>
    {% if archive.UpdatedTime > archive.CreatedTime %}
    <p>最后更新:{{ stampToDate(archive.UpdatedTime, "2006-01-02 15:04") }}</p>
    {% endif %}
    
  • Any display that requires dynamic date information.For example, display the start date of the activity, system running time, etc.

By{% now %}Tags and{{ stampToDate() }}A function combined with Golang's flexible time formatting rules makes AnqiCMS easy and powerful to display dynamic time information.No matter whether it is displaying the copyright year at every corner of the website or the precise content release time down to the second, you can easily achieve it and ensure that this information is always kept up-to-date and accurate.

Frequently Asked Questions (FAQ)

1. Why do I use{% now "YYYY" %}Unable to display the current year?

This is because AnqiCMS's time formatting follows the rules of the Golang language instead of the commonYYYY-MM-DDorYYYYPlaceholder pattern. In Golang, you need to use a fixed reference time2006-01-02 15:04:05To define your format. So, to display the current year, you should use{% now "2006" %}.

2.{{ stampToDate(item.CreatedTime, "...") }}Error, what is the reason?

It is usually possible to encounter this error,item.CreatedTimeThe variable itself is not a valid timestamp (for example, it is a null value, a string type, or another non-numeric type), or the first argument you are trying to pass is not a timestamp. Make surestampToDateThe first parameter of the function is a valid 10-digit or 13-digit Unix timestamp.

3. Can I display English months or weekdays?

Of course you can. Golang supports various English representations for time formatting. For example, to display the English abbreviation of the month, you can useJan; To display the English name of the weekday, you can useMondayorMon. For example:{{ stampToDate(archive.CreatedTime, "Monday, Jan 02, 2006") }}It will display similarThursday, Oct 26, 2023format.

Related articles

How to display traffic or crawler access data charts on the front end of AnqiCMS's background data statistics function?

Understanding website traffic and user behavior is crucial for operators.AnqiCMS as an efficient enterprise-level content management system provides detailed data statistics and crawling monitoring functions in the background.This data can not only help us analyze the performance of the website and optimize the content strategy, but also has the potential to provide more intuitive information to users or visitors in specific scenarios through the display of charts on the front end.

2025-11-09

How to automatically parse a URL string into a clickable a tag in AnqiCMS template for convenient browsing?

When operating a website, we often add some URLs in articles, product descriptions, or single-page content, which may be recommended external resources or references to other content within the site.But if these URLs are just displayed in plain text, users will have to copy and paste to access them, which is inconvenient and also affects the reading experience.AnqiCMS has fully considered this point and provided us with a very convenient way to automatically convert plain text URLs in the content into clickable links, greatly enhancing the convenience of user browsing and the professionalism of the website.

2025-11-09

How can I truncate a string or HTML content in AnqiCMS template and add an ellipsis to control the display length?

In website operation, the way content is presented has a crucial impact on user experience and information transmission efficiency.Whether it is the introduction of the article list, the abstract of the product description, or other text content that needs to be previewed, reasonable control of the display length and the use of ellipses can not only maintain the page's neat and beautiful appearance, but also guide users to click and view more details.AnqiCMS as an efficient and flexible content management system, provides a variety of powerful template tags and filters, making the control of content length extremely simple and intelligent.Why do we need to truncate content and add an ellipsis

2025-11-09

How to determine whether a string, array, or object in AnqiCMS template contains a specific keyword and display content dynamically based on this?

In AnqiCMS, flexibly judging and dynamically displaying information based on content is the key to improving website user experience and SEO effects.Imagine that your website can automatically display related purchase links based on whether an article mentions a specific product; or recommend different sidebar content based on the category the user is in.This not only makes your website smarter, but also greatly improves the efficiency of content operation.The AnqiCMS template engine provides powerful features to help you easily implement these dynamic logic.

2025-11-09

How can AnqiCMS efficiently display the latest published article list on the homepage?

How can Anqi CMS efficiently display the latest published article list on the homepage?In website operation, the homepage as the first stop for user access, the activity of content updates often determines whether users are willing to stay and delve deeper into browsing.Especially for content-driven websites or corporate blogs, the homepage can dynamically display the latest list of published articles, not only presenting fresh content to visitors in the first place but also a key factor in improving user experience and search engine friendliness.So, how do we achieve this goal efficiently in AnQiCMS (AnQiCMS)?

2025-11-09

How to use AnQi CMS template tags to call the article content under a specific category and perform pagination display?

In website content operation, we often need to display a list of articles under a specific category on a single page, and in order to provide a better user experience and content structure, pagination functions are usually used in conjunction.Strong and easy-to-use template tags are provided by AnQi CMS, which can help us easily meet this requirement. ### The core of article content call: `archiveList` tag To call the article content under a specific category, AnQi CMS provides the core tag `archiveList`. This label is very flexible

2025-11-09

In AnQi CMS, how should the article detail page correctly display the article title, content, and related information?

AnQiCMS (AnQiCMS) provides powerful and flexible content management features, especially when building article detail pages, which can help us efficiently present high-quality content.A well-designed article detail page can not only clearly display the core information of the article, but also effectively improve the user's reading experience and the stickiness of the website through the recommendation of related content. ### Article Title and Core Content: The Soul of the Page The core of the article detail page in Anqi CMS is the article title and main content.

2025-11-09

Under multi-site management, how to ensure that the independent content of each site is displayed correctly?

The practice of independent content display for multiple sites of AnQi CMS: Ensuring the unique style of each site In today's ever-changing digital world, many enterprises and content operators may need to manage multiple websites at the same time, whether it is to distinguish brands, sub-divide markets, or meet different business needs.AnQiCMS (AnQiCMS) provides an efficient and convenient solution for such needs with its powerful multi-site management function.

2025-11-09