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,