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