How to display the current year or a specified format of time in a template, such as "2023-10-26 15:30:00"?

Calendar 👁️ 91

In website operation, it is often necessary to dynamically display the current year or a specific date and time format at different positions on the page, whether it is for the copyright statement in the footer, the publishing and updating time of articles, or the time points of various events.AnQiCMS (AnQiCMS) provides flexible and powerful template tags, allowing you to easily meet these needs.

AnQiCMS's template system borrows the syntax of the Django template engine, which means you can use double curly braces{{变量}}to output variables, and by{% 标签 %}Call the functional tag. It is crucial to understand the core time formatting rules when handling dates and times.

Understand the date and time formatting in Go language.

AnQiCMS time formatting follows the unique conventions of the Go language, it does not use commonY-m-doryyyy-MM-ddthis kind of placeholder, but uses a specific reference time2006-01-02 15:04:05This number refers to a component of the date and time. You just need to replace the format you want to display with the corresponding number.

For example:

  • year:2006
  • month:01(with leading zero) or1(without leading zero)
  • Day:02(with leading zero) or2(without leading zero)
  • hours:15(24-hour format, with leading zero) or03(12-hour format, with leading zero)
  • minute:04(with leading zero)
  • seconds:05(with leading zero)
  • AM/PM:PM(and03Use together)

Mastering this rule, you can freely combine various date and time formats.

Display the current year in the template.

Display the current year at any location on the website page, for example, in the footer for copyright statements, you can use it directly{% now %}The tag is used with Go language's year formatting characters2006.

© {% now "2006" %} 您的网站名称. All Rights Reserved.

This will automatically display the current year in your website footer, for example, without manual updates© 2023 您的网站名称. All Rights Reserved..

Display the current date and time in a specified format

If you need to display the current full date and time on the page, such as showing the access time at the top of the page or providing time-sensitive information in a specific module,{% now %}Tags can also be used. You just need to provide the complete Go language date and time format string.

For example, to display the current date and time to the second, the format is年-月-日 时:分:秒:

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

It will display something similar on the page当前时间:2023-10-26 15:30:00The content.

If you need other common date and time formats, you can combine them like this:

  • Only display the date (e.g., 2023-10-26):
    
    发布日期:{% now "2006-01-02" %}
    
  • Display the month and date without leading zeros (e.g., 10/26/2023):
    
    活动日期:{% now "1/2/2006" %}
    
  • Display time in 12-hour format (e.g., 03:30 PM):
    
    更新时间:{% now "03:04 PM" %}
    

Format the timestamp in the content

In AnQi CMS, content such as articles and products, their publication time or update time is usually stored in the database in the form of a 10-digit timestamp, for exampleitem.CreatedTimeoritem.UpdatedTime. When you iterate over this content in a template and need to display the time, you need to use{{stampToDate()}}This tag is specifically used to format timestamps.

{{stampToDate()}}The tag requires two parameters:

  1. Timestamp variableFor exampleitem.CreatedTime.
  2. Go language format string: with{% now %}The format rules used for tags are the same.

Assuming you are looping through a list of articles and want to display the publication date and detailed update time for each article:

{% archiveList archives with type="list" limit="10" %}
    {% for item in archives %}
    <article>
        <h2><a href="{{item.Link}}">{{item.Title}}</a></h2>
        <p>发布于:{{stampToDate(item.CreatedTime, "2006年01月02日")}}</p>
        <p>最后更新:{{stampToDate(item.UpdatedTime, "2006-01-02 15:04:05")}}</p>
        <p>{{item.Description}}</p>
    </article>
    {% endfor %}
{% endarchiveList %}

By this code, the publish date of each article will be displayed in the form2023年10月26日and the update time will be displayed in the precise format2023-10-26 15:30:00like this.

Summary

The Anqi CMS template system passed{% now %}Tags and{{stampToDate()}}The filter, combined with the concise and unique date and time formatting rules of the Go language, provides you with great flexibility, whether you need to display the current time or format timestamps stored in content, it can be easily achieved.Mastering these basic skills will help you build a website with rich information and a better user experience.


Frequently Asked Questions (FAQ)

Q1: Why did my date formatting not work, or did it display strange numbers?A1: This is likely because you did not strictly follow the reference time in string formatting in Go language2006-01-02 15:04:05Make sure that the year, month, and date are all used with the corresponding numbers in the reference time (for example, the year must be2006instead ofYYYYoryy)。Another possible reason is that you are trying to use{{stampToDate()}}The formatted variable itself is not a valid timestamp.

Q2: Can I display the current time while adding some custom text?A2: Of course you can.{% now %}The label will only output the formatted time string, you can embed it in any text content, for example:欢迎访问,今天是:{% now "2006年01月02日" %},祝您心情愉快!

Q3: How to display the year and month of the content's publication on an article or product detail page?A3: On an article or product detail page, you can usearchive.CreatedTimeGet the timestamp of the content published. Then combine{{stampToDate()}}Tags and Go language formatting strings to display, for example:{{stampToDate(archive.CreatedTime, "2006年01月")}}It can be displayed as2023年10月The content.

Related articles

How to call the embedded method of Go structure in AnQiCMS template to get and display data?

AnQiCMS with its efficient architecture based on the Go language and flexible template system, provides strong support for website content display.For users who want to implement more intelligent and dynamic content display in templates, it is particularly important to understand how to call the built-in methods of Go structures in AnQiCMS templates.This can make your template more tidy, and it can encapsulate complex logic processing on the backend, allowing the frontend to focus more on visual presentation.

2025-11-08

How to integrate image captcha functionality into the comment or message form to improve security?

When a website's comment section or message board is flooded with spam, the captcha is undoubtedly a simple and effective defense measure.It can effectively identify whether it is a human user or an automated program, thereby preventing malicious screen scraping or spam comments.AnQi CMS understands the importance of website security, therefore it has integrated the graphic captcha function into the system, allowing us to easily add this layer of protection to the form and enhance the overall security of the website.

2025-11-08

How to build a comment form in a template and dynamically display custom fields from the backend?

In website operation, the feedback form is an important bridge for interaction with users, collecting feedback, and establishing contact.AnQiCMS (AnQiCMS) provides a powerful and flexible message feature, which not only allows users to quickly build basic forms but also supports custom fields through the backend to make forms dynamically adapt to various business needs.This article will delve into how to build such a comment form in Anqi CMS templates and dynamically display these custom fields from the backend.

2025-11-08

How to display a comment list on the website front end and support pagination, replies, and review status distinction?

The website comment section is an important battlefield for user communication and content feedback. A well-designed and functional comment system can greatly enhance the activity and user stickiness of the website.AnQiCMS provides us with a flexible and powerful comment management feature, whether you want to display comments, implement reply interaction, or differentiate according to the review status, you can easily achieve this through simple template tags.This article will introduce in detail how to efficiently display comment lists on the website front-end, and implement features such as pagination, reply, and review status distinction.### One, Basic Display of Comment List

2025-11-08

How to retrieve and display custom contact information from the background in a template (such as phone, email, WeChat)?

In website operation, clearly displaying contact information is crucial for improving user experience and promoting business communication.Whether it is customer consultation, seeking support, or business cooperation, visitors hope to find and contact you quickly.AnQiCMS provides a convenient and efficient mechanism for you to centrally manage these contact information in the background, and dynamically display them in the website template without frequently modifying the code, which greatly improves the efficiency of website maintenance.### Contact Management in AnQiCMS On AnQiCMS backend

2025-11-08

How to customize Json-LD structured data to enhance search engines' understanding and display of page content?

In today's highly competitive online environment, making website content stand out, being accurately understood by search engines, and presented in an attractive manner is a goal that every website operator diligently seeks.Among them, the structured data of Json-LD plays a vital role.

2025-11-08

How to display user group details, user avatar, username, and other personal information in the template?

In website operations, providing users with personalized experiences, enhancing community interaction, and clearly showcasing the rights and interests of different user groups is the key to improving user stickiness and website value.AnQiCMS as a powerful content management system, provides flexible template tags, allowing us to easily display users' personal information (such as username, avatar) and details of their user group on the front-end page.

2025-11-08

How to customize the overall layout and display structure of the website front-end page in AnQi CMS?

## Flexible Mastery: How Anqi CMS Creates Personalized Website Front-end Layout and Display Structure In the digital age, the front-end design of a website is crucial for user experience and brand image.An efficient content management system that not only can easily manage content but also can flexibly customize the overall layout and display structure of the front-end page.AnQi CMS is well-versed in this, providing us with a powerful and intuitive toolkit that makes website customization accessible.

2025-11-08