In website content operation, the way dates and times are presented often directly affects user experience and the clarity of information.An accurate and readable date format can not only enhance the credibility of the content, but also enable visitors to obtain key information more quickly.AnQiCMS as a powerful content management system naturally also provides a flexible way to handle and format date and time.

When we need to display the article publish time, update time, or any other date information in the AnQiCMS template, we may encounter a problem: how to convert these original data (usually timestamps) into the format we want年-月-日/时:分:秒or more customized format? This article will delve into the processing of AnQiCMS templatestime.TimeType (or more commonly timestamp) object and strategies and methods for formatting it into a specified date string.

Understand the date data in AnQiCMS.

AnQiCMS in storing article publish time (such asarchive.CreatedTime)Update time(archive.UpdatedTime)et al. such date data, it is usually adopted that the Unix timestamp format.This is a representation of the number of seconds elapsed since the Unix epoch (00:00:00 UTC on January 1, 1970).Therefore, directly displaying these timestamps in the template often results in a string of difficult-to-understand numbers, which we need to format into a human-readable date string.

Core Tools:stampToDatetags

AnQiCMS provides a very practical template tag for this:stampToDateThis tag is specifically used to convert timestamps to date strings in a specified format.

Its basic usage syntax is:

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

The 'timestamp variable' here is usually a timestamp field available in the template, for exampleitem.CreatedTime. And the 'format string' is the key to defining the style in which the date is displayed.

Understanding the format string in Go language

This isstampToDateThe label is the most unique and also the most needing to be understood. It is different from what we usually see.YYYY-MM-DDThe placeholder differs, the Go language (the development language of AnQiCMS) uses a fixed reference date to define the date format:

2006年1月2日 15点04分05秒

You need to use each part of this date to represent the format you want. For example:

  • 2006Represents the year
  • 01Represents the month (with leading zero)
  • 02Represents the date (with leading zero)
  • 15Represents the hour in 24-hour format
  • 04Represents minutes
  • 05Represents seconds

If you want to display the day of the week, you can also refer to the more complex reference date part provided in the official Go language documentation, such asMonrepresenting the abbreviation Monday,Mondayrepresenting the full name of the week, etc.

How to usestampToDatePerform date formatting

Let's see through several practical examplesstampToDateHow do tags work. Suppose you are dealing with a list of articles and you need to display the publication date and time next to each article.

  1. Only show the year, month, and day (e.g.:2023-10-26)

    This is the most common date display format.

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

    Whenitem.CreatedTimeThe value of1698292800At (representing October 26, 2023, 12:00:00 UTC), the output will be2023-10-26.

  2. Display the full date and time (for example:2023-10-26 14:30:00)

    If you need the full time to the second, you can format it like this:

    <span>发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02 15:04:05") }}</span>
    

    The output will be2023-10-26 14:30:00(assuming the timestamp corresponds to this local time).

  3. Custom format (for example:)10月26日, 星期四)

    The formatting power of Go is very strong, and can be combined into various styles:

    <span>文章日期:{{ stampToDate(item.CreatedTime, "01月02日, 星期一") }}</span>
    

    Here星期一It is in the Go language reference date ofMonThe corresponding localization format, which will be converted to the correct day of the week according to the timestamp when displayed. For example, the output might be10月26日, 星期四.

  4. InarchiveDetailused directly in the

    InarchiveDetailto obtainCreatedTimeorUpdatedTimeat, you can also directly use itformatproperty for formatting, which makes the code more concise.

    {# 显示文章发布日期 #}
    <div>发布日期:{% archiveDetail with name="CreatedTime" format="2006年01月02日" %}</div>
    {# 显示文章更新日期和时间 #}
    <div>更新时间:{% archiveDetail with name="UpdatedTime" format="2006-01-02 15:04" %}</div>
    

other date and time processing scenarios

  1. Display the current date and time:{% now %}tags

    If you want to display the current date and time in the template instead of the article's publication time, you can use{% now %}The format string rules for the tag are the same asstampToDatethe same.

    <span>当前日期:{% now "2006-01-02" %}</span>
    <span>当前完整时间:{% now "2006-01-02 15:04:05" %}</span>
    
  2. AboutdateFilter

    AnQiCMS template still contains a filter nameddate(for example:{{ someGoTimeObject|date:"2006-01-02" }})。It is usually used to process the native Go language,time.TimeAn object of type, rather than the common Unix timestamp. In the common applications of AnQiCMS templates (such asarchive.CreatedTime),These fields are directly exposed as timestamps, so please use them firststampToDatetags or within tagsformatattributes to ensure correctness and simplicity.

Tips and **Practice

  • Remember the reference date of the Go language: 2006-01-02 15:04:05Is the foundation to master all date formatting. By remembering this date, you can flexibly combine any format you want.
  • Maintain uniform formatting:On your website, try to maintain uniformity in the date and time format, which helps improve user experience and the professionalism of the website.
  • Test different formats:Before using it in practical applications, make sure to test your formatted strings in the development environment to ensure they display as expected.
  • Take advantage of the flexibility of AnQiCMS:CombinearchiveList/archiveDetailEnglish tags, you can easily display formatted date information in any location such as article lists, detail pages, sidebars, etc.

Summary

PassstampToDateTags and their formatting rules based on the reference date of Go language, AnQiCMS makes the presentation of date and time in templates intuitive and powerful.Whether it is a simple date, or a detailed time to the second, or a custom format with the day of the week, AnQiCMS can help you easily achieve it, thereby adding professionalism and readability to your website content.


Common Questions (FAQ)

**Q1: Why do I use `YYYY