How to display the current year or a specific format of time in the template in real-time?

Calendar 👁️ 74

In website operation, the display of time information is everywhere, whether it is the current year in the copyright statement, the time of article publication, or the deadline of the event, the dynamic and accurate display of time is crucial for improving user experience and the timeliness of content.As an enterprise-level CMS dedicated to efficient content management, AnQiCMS (AnQiCMS) fully understands this need, and therefore provides intuitive and powerful tags in the template system, allowing you to easily display the current year or custom formatted time in website templates.

The template engine design of AnQi CMS draws inspiration from Django's syntax style, implementing the output of various dynamic contents through concise tag labels. For time display, we mainly use two core tags:{% now %}and{{stampToDate()}}.

Elegantly display the current year:{% now %}Tag

In the footer copyright statement area of the website, we often see “Copyright ©This text contains the year 2023 and the name of Your Company, which needs to be updated annually.Manually modifying is obviously not an efficient solution. AnQi CMS provides a very convenient tag for this:{% now %}.

This tag can directly retrieve the current date and time of the server and output it according to the specified format. If you just need to display the current year, you can simply write it in the template like this:

<footer>
    &copy; {% now "2006" %} {{ system.SiteName }} 版权所有。
</footer>

Here, "2006"Is a special date and time formatting string in Golang, representing the reference layout of the year. When the system parses to{% now "2006" %}It will intelligently extract the current year and replace it at this position. In this way, the copyright year of your website will be automatically updated every year, without manual intervention, greatly improving operational efficiency.

Precise control of time format:{{stampToDate()}}Magic of tags

If your needs are not just simple years, but more specific and rich date or time formats, such as displaying "2023 October 27 14:35" on an article detail page, or needing to convert timestamps stored in a database into readable date formats, then Anqi CMS provides more powerful features.{{stampToDate()}}.

{{stampToDate()}}The tag is a functional tag that receives two main parameters: the first is the 10-digit Unix timestamp to be formatted, and the second is the string that defines the output format. Its basic usage is as follows:

{{ stampToDate(时间戳, "格式化字符串") }}

For example, assuming your article data objectarchivehas aCreatedTimefield stores the creation timestamp of the article, and if you want to display it in the format of 'Year-Month-Day', you can do it like this:

<p>发布日期:{{ stampToDate(archive.CreatedTime, "2006-01-02") }}</p>

The secret of Golang time formatting strings

It needs to be emphasized here,"2006-01-02"This formatting string is not a random date, but a conventional one in GolangReference layoutIt indicates the position of time elements (year, month, day, hour, minute, second, etc.) in the string. Understanding this reference layout is flexible applicationstampToDateThe key is:

  • 2006: Represents the year (Year)
  • 01: Represents the month (Month), for example 1st month
  • 02: Represents the day (Day), for example 2nd day
  • 15: Represents hour (Hour), 24-hour format, for example 3 PM
  • 04: Represents minute (Minute)
  • 05: Represents second (Second)
  • Mon/MondayRepresents the abbreviation/full name of the day of the week
  • Jan/JanuaryRepresents the abbreviation/full name of the month

By combining these reference elements, you can build almost any date-time format you want.

Here are some common formatting examples:

  • Only display the year: {{ stampToDate(archive.CreatedTime, "2006") }}-u003e2023
  • Display year-month-date: {{ stampToDate(archive.CreatedTime, "2006-01-02") }}-u003e2023-10-27
  • Display year/month/date: {{ stampToDate(archive.CreatedTime, "2006/01/02") }}-u003e2023/10/27
  • Show Chinese year-month-day: {{ stampToDate(archive.CreatedTime, "2006年01月02日") }}-u003e2023年10月27日
  • Show hours:minutes:seconds: {{ stampToDate(archive.CreatedTime, "15:04:05") }}-u003e14:35:01
  • Show full date and time: {{ stampToDate(archive.CreatedTime, "2006-01-02 15:04:05") }}-u003e2023-10-27 14:35:01
  • Show format with weekday: {{ stampToDate(archive.CreatedTime, "Mon, 02 Jan 2006") }}-u003eFri, 27 Oct 2023

Through these flexible formatting options, you can accurately control the presentation of time information according to the design style and content requirements of the website.

Actual application scenarios and combination usage

In AnQi CMS, these tags are used very widely.

  • Website footer:
    
    <p>Copyright &copy; {% now "2006" %} {{ system.SiteName }}. All Rights Reserved.</p>
    
  • Article or product list page:InarchiveListWhen the tag loops articles, it displays the publication date of each article.
    
    {% archiveList archives with type="list" limit="10" %}
        {% for item in archives %}
        <div class="article-item">
            <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
            <p>发布时间:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}</p>
            <p>{{item.Description}}</p>
        </div>
        {% endfor %}
    {% endarchiveList %}
    
  • Custom time display:If you have customized a field in the background to store a timestamp for an event (for example,event.StartDate), you can also usestampToDateto format.
    
    <p>活动开始:{{ stampToDate(event.StartDate, "2006年01月02日 星期一 15:04") }}</p>
    

Whether it is a simple year annotation or complex date and time formatting, Anqi CMS provides intuitive and powerful template tags to meet your needs. By using it flexibly{% now %}and{{stampToDate()}}Label, you can easily add timeliness and professionalism to the website content, thereby enhancing the overall user experience.

Frequently Asked Questions (FAQ)

  1. Q: Why does the Golang time formatting string look like a specific date (e.g. "2006-01-02 15:04:05"), rather than using placeholders like "Y-m-d H:i:s" as in PHP or Java?A: This is a unique feature of Golang language design. Golang's time formatting is not defined through abstract placeholders, but uses a specific "reference time" - that is,2006年1月2日15点04分05秒 -0700 MSTBe used as a layout example. All you need to do is write a string that matches the expected output format, which contains the corresponding numbers or text parts of this reference time.The system will parse and format the actual time according to your layout.For example, if you want to display the year,

Related articles

How to determine if a website is in a shutdown state and display user-friendly prompt information?

## The website operation 'shut down' maintains dignity: AnQi CMS helps you shut down gracefully and provide friendly prompts In the daily operation of the website, we will always encounter times when we need to temporarily 'shut down'.For reasons such as system upgrades, large-scale content adjustments, data migration, or for security maintenance, content filling and review before the launch of a new website, a proper shutdown process is crucial.This concerns not only the security and stability of the website's data, but also directly affects the user experience and the evaluation of the website by search engines.

2025-11-07

How to dynamically obtain the path of the static resources used by the current website template?

As an experienced website operations expert, I know how important efficiency and flexibility are in managing websites, especially content management systems (CMS).AnQiCMS as an enterprise-level content management system based on the Go language has fully considered these requirements from the very beginning, especially in terms of template and resource management, providing a powerful and elegant solution.Today, let's delve deeply into a very practical topic in template development: 'How to dynamically obtain the static resource path of the template currently used by the AnQiCMS website?'}]

2025-11-07

How to obtain the basic configuration information of a specified site when operating multi-site?

AnQiCMS (AnQiCMS) provides great convenience for operators with its excellent multi-site management capabilities.Whether it is multiple brand sites under your company, regional sub-sites, or the need to build independent content portals for different business lines, AnQiCMS can efficiently manage them on a unified backend.However, in actual operation, you may encounter such a scenario: in a site template, it is necessary to obtain the basic configuration information of another site, such as its website name, logo, URL, or contact information.

2025-11-07

How to obtain and display the website's filing number and custom copyright information?

As an experienced website operations expert, I fully understand the importance of website compliance and brand information display for user trust and corporate image.AnQiCMS (AnQiCMS) provides powerful and flexible features in these aspects, allowing you to easily manage and display the filing number and custom copyright information of your website.Next, I will explain in detail how to implement these operations in AnQiCMS. --- ## How to obtain and display the record number and custom copyright information of a website?

2025-11-07

How to customize system-level parameters and flexibly call them in the front-end template?

## Mastering AnQi CMS: Flexible System-Level Parameter Customization and Efficient Front-End Template Calls As an experienced website operations expert, I fully understand the importance of a flexible and customizable content management system for corporate efficient operations.AnQiCMS (AnQiCMS) boasts a high-performance architecture based on the Go language and rich features, performing excellently in content publishing, multi-site management, and SEO optimization. Especially in terms of system parameter customization and invocation, it provides great convenience, allowing operators to easily handle all aspects of the website.

2025-11-07

How to uniformly display contact information, phone numbers, email addresses, and other contact details in the footer of a website?

As an experienced website operations expert, I know that the website footer, although seemingly unimportant, is a crucial area for users to find key information and establish trust.Unified and clearly displaying contact information not only enhances user experience but is also an important embodiment of website professionalism and service capabilities.For enterprises and content operation teams using AnQiCMS, it is easy to achieve this thanks to its flexible template mechanism and convenient backend management features.

2025-11-07

How to display WeChat QR code or social media account links in AnQiCMS templates

In the era where content is king, businesses and self-media operators are all aware of the importance of building an effective connection with users.And social media, especially WeChat, is undoubtedly an efficient channel for connecting users, sharing information, and providing services.AnQiCMS (AnQiCMS) is a system designed for efficient content management, naturally understanding this well, and providing strong and convenient support for users to flexibly display WeChat QR codes or links to various social media accounts in website templates.

2025-11-07

How to display the list of all document tags in AnQiCMS template?

As an experienced website operations expert, I know that the development of content management system templates is the key to website operation efficiency and content display effect.AnQiCMS (AnQiCMS) provides us with great convenience with its efficient and flexible features based on the Go language.Today, let's delve deeply into a very practical requirement in content operation: how to elegantly display the tag list of all documents in the AnQiCMS template.Tags play a crucial role in modern website content management.

2025-11-07