How to display the current time and formatted timestamp in AnQiCMS template?

Calendar 👁️ 67

Displaying time on the website content, whether it is the article publishing date, product update time, or real-time page loading time, can greatly enhance user experience and the timeliness of information.AnQiCMS as a content management system based on the Go language is very intuitive and flexible in handling time and dates in templates.This article will introduce you to how to display the current time and formatted stored timestamp in the AnQiCMS template.


1. Display the current real-time time on the page

If you need to display the real-time time of user visits on the page, AnQiCMS provides a concise{% now %}Label to help you implement. The role of this label is to obtain and display the current date and time of the server.

Its usage is very direct, just follow a string after the tag, which is the format you want the time to display.

Basic usage example:

<p>页面加载时间:{% now "2006年01月02日 15:04:05" %}</p>

You may notice that the time format string here “2006年01月02日 15:04:05” looks a bit special, it is not the common one we usually useY-m-d H:i:sIt is the special date and time formatting method of Go language. You just need to remember this base time。“2006-01-02 15:04:05The template, then replace the numbers and symbols according to the part you need to display, you can build the format you want.

For example, if you only want to display the current year, you can write it like this:

<p>当前年份:{% now "2006" %}</p>

If you want to display the complete year, month, day, hour, minute, and second:

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

This{% now %}The tag is very suitable for displaying copyright years in the footer or showing a dynamic welcome time in the header and other scenarios.


2. Format the stored timestamp display.

In AnQiCMS, when articles, products, or other content are published or updated, their time information (such asCreatedTimeandUpdatedTimeIt is usually stored in the database in the form of a 10-digit Unix timestamp. To display these times clearly and legibly on the frontend page, we need to use{{stampToDate}}function for formatting.

This function takes two parameters: the first is the 10-digit timestamp to be formatted, and the second is the Go language time format string with the same tag.{% now %}The Go language time format string with the same tag.

Basic usage example (for example, article publish time):

Suppose you are usingarchiveListorarchiveDetailtags to retrieve article data, wherearchive.CreatedTimeis the article's creation timestamp.

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

If you want to display the update time of the article and only display it when the update time is different from the creation time, you can do it like this:

{% if archive.UpdatedTime != archive.CreatedTime %}
<p>最近更新于:{{ stampToDate(archive.UpdatedTime, "2006年1月2日 15:04") }}</p>
{% endif %}

Here, we first judgeUpdatedTimewhether it is withCreatedTimeDifferent to avoid displaying the same time repeatedly. If there are differences, use it again{{stampToDate}}The function formats it into the style we need


3. Master the Go language time formatting template

Whether it is{% now %}whether it is{{stampToDate}}In the function, the core of time formatting lies in the Go language's base time template. Remember that2006-01-02 15:04:05This specific date and time, and use it as your 'reference' to construct the format you need.

Here are some commonly used format strings and their corresponding output effects to help you quickly understand and apply them:

  • “2006-01-02”Output the full date, for example “2023-10-26”
  • “15:04:05”Output the time, for example “10:30:45”
  • “January 2, 2006”:Output Chinese date in the format, for example “October 26, 2023”
  • “01/02 15:04”Output the month, day, and time, for example “102610:30”
  • “Mon Jan _2 15:04:05 2006”Print the complete English date and time, for example 'Thu Oct 26 10:30:45 2023'
  • “Monday, 02-Jan-06 15:04:05 MST”Another English date-time format, for example, 'Thursday, 26-Oct-23 10:30:45 CST'
  • '3:04PM'12-hour time format, for example '10:30AM'
  • '2': A single date, for example '26'

You can freely combine these elements according to the page design and user needs to create various time display effects.


Example 4, Comprehensive Application

Let us go through a simple template snippet to see how these time display and formatting methods are combined in practice on a web page:

<main>
    <header>
        <h1>AnQiCMS 网站首页</h1>
        <p>当前服务器时间:{% now "2006年01月02日 星期一 15:04:05" %}</p>
    </header>

    <section class="latest-articles">
        <h2>最新文章</h2>
        {% archiveList articles with type="list" limit="3" %}
            {% for article in articles %}
                <article class="article-card">
                    <h3><a href="{{ article.Link }}">{{ article.Title }}</a></h3>
                    <p class="article-meta">
                        发布于:{{ stampToDate(article.CreatedTime, "2006-01-02 15:04") }}
                        {% if article.UpdatedTime != article.CreatedTime %}
                        <span>(更新于:{{ stampToDate(article.UpdatedTime, "2006年1月2日") }})</span>
                        {% endif %}
                    </p>
                    <p>{{ article.Description|truncatechars:100 }}</p>
                    <a href="{{ article.Link }}">阅读更多</a>
                </article>
            {% endfor %}
            {% empty %}
            <p>暂时没有文章可供显示。</p>
        {% endarchiveList %}
    </section>

    <footer>
        <p>&copy; {% now "2006" %} AnQiCMS. 版权所有。</p>
    </footer>
</main>

In this example, we demonstrate how to display the real-time accurate time in the header, how to display the publication date and conditional update date in the article list, and how to dynamically display the current year in the footer.These can be easily implemented with the flexible tags and functions provided by AnQiCMS.

By{% now %}tag to obtain real-time time, as well as{{stampToDate}}The timestamp formatted by the function, AnQiCMS provides a powerful and easy-to-use time display feature for your website.Master these skills and it will help you build dynamic and informative websites better.


Frequently Asked Questions (FAQ)

1. Why is the time formatting rule of Go language so special, I always can't remember '2006-01-02 15:04:05'??

Go language's time formatting is indeed

Related articles

What are the application scenarios of AnQiCMS template macro functions (macro) and include tags?

In AnQiCMS template development, in order to improve code reusability and reduce maintenance costs, we often use some powerful template functions, among which 'macro function' and 'Include tag' are two great assistants.They each have unique advantages and applicable scenarios. Understanding and appropriately applying them can make template development twice as efficient and build efficient, easy-to-maintain websites.

2025-11-07

How to implement page skeleton reuse through inheritance in AnQiCMS templates?

In website construction and operation, we often face a challenge: how to ensure the consistency of the overall style of the website while being able to flexibly modify the specific content of each page, and at the same time, taking into account the development efficiency and the convenience of later maintenance?AnQiCMS provides a powerful and intuitive template inheritance mechanism, which is the core of solving these problems.It's like creating a "template" for your website, allowing you to easily reuse common structures and focus on content creation.

2025-11-07

How to include external HTML fragments in AnQiCMS templates?

When building website templates in AnQiCMS, we often encounter some HTML code blocks that need to be reused, such as the website header, footer, sidebar navigation, advertisement slots, or some general content modules.If each time you repeat writing these codes in different template files, it is not only inefficient but also very麻烦 to find and update them one by one when modifications are needed.

2025-11-07

What is the difference between the variable output and logic control tags in AnQiCMS templates?

In AnQiCMS template development, we often deal with two core template elements: variable output tags and logical control tags.They all serve to present dynamic content to the user, but there are essential differences in their functional positioning, syntax form, and actual effects.Understanding and mastering their similarities and differences is the key to efficiently building AnQiCMS website templates.### Variable output label (`{{ ... }}`): The direct window for data display Imagine that your website is a beautiful showcase, and the variable output label is like the merchandise displayed in the showcase

2025-11-07

How to customize variables and assign values using (with/set) in AnQiCMS templates?

In AnQiCMS templates, flexibly customizing and using variables is the key to achieving dynamic content and efficient template reuse.AnQiCMS's template syntax borrows the characteristics of popular template engines such as Django and Blade, making variable definition and assignment intuitive and powerful.This article will deeply explore the `with` and `set` tags, helping you to better customize variables and utilize them in AnQiCMS templates.### One, the foundation of variable customization: Understanding the core of template syntax In AnQiCMS template

2025-11-07

How to use the `archiveList` tag to display the latest article list on the AnQiCMS homepage?

On AnQiCMS, the homepage is often the first window visitors use to understand the website's content.How to efficiently and dynamically display the latest articles to attract visitors to continue browsing is the key to content operation.AnQiCMS provides a flexible and easy-to-use template tag system, where the `archiveList` tag is the core tool to achieve this goal.

2025-11-07

How to retrieve the list of articles under a specified category and implement pagination in AnQiCMS?

In website content operation, we often need to display articles under specific categories and provide pagination for these lists to enhance user experience and website loading efficiency.AnQiCMS (AnQiCMS) with its flexible template engine and powerful tag system can easily meet this requirement. AnQiCMS template system adopts syntax similar to Django template engine, using double curly braces `{{variable}}` to reference variables, as well as single curly braces with a percent sign `{% tag %}` to call tags.This makes the customization of content display very intuitive

2025-11-07

What are the commonly used fields that can be called with the `archiveDetail` tag on the article detail page?

AnQiCMS (AnQiCMS) provides great convenience for website operators with its flexibility and powerful content management capabilities.When we carefully craft an article or product page and hope to accurately display its colorful content to visitors, the `archiveDetail` tag is an indispensable tool at our disposal.It is like a treasure trove of information, allowing us to easily call up all related data of the current content on the article detail page.

2025-11-07