In website content operation, clearly displaying the publication and update time of articles is crucial for improving user experience, providing timeliness information of content, and optimizing search engine indexing.AnQiCMS (AnQiCMS) provides a powerful and flexible timestamp formatting function in the template, allowing us to convert original timestamp data into user-friendly date and time display according to specific needs.

We need to use a core template tag to display the publication and update time of the article accurately in the AnQiCMS template:stampToDate. This tag can convert the 10-digit timestamp stored by the system into the date and time format we want.

UnderstandingstampToDateThe core usage of tags

stampToDateThe syntax structure of the tag is very intuitive:{{stampToDate(时间戳变量, "格式字符串")}}. Among them:

  • Timestamp variableThis is usually obtained from article or list dataCreatedTime(Publish time) orUpdatedTime(Updated time), they exist in the form of a 10-digit timestamp.
  • Format stringThis is the most critical part, which determines how the timestamp will be displayed. Common with other CMS systemsY-m-d H:i:sDifferent placeholder, AnQiCMS (because it is developed based on Go language) adopts the special date and time formatting reference string of Go language. This reference string is fixed2006-01-02 15:04:05. You do not need to remember the meaning of this string, just use it as a template to adjust it according to the style you want to output.

For example, if you want to display2023年02月09日Then your format string should be"2006年01月02日"If you want to display02/09/2023The format string is then"01/02/2006"Here are the numbers.2006/01/02/15/04/05The elements, such as year, month, day, hour, minute, and second in Go language date format, can be freely combined according to your needs to define the final display format.

Get the timestamp data of the article

Before formatting the timestamp, we first need to obtain the original timestamp from the article data. This can be done byarchiveDetailTags (used for the article detail page) orarchiveListLabel (used to implement on article list page).

  • Get the timestamp on the article detail page:When you are in the article detail template (such as{模型table}/detail.html), you can directly accessarchivethe current article's object.CreatedTimeandUpdatedTimefield.

    <p>发布时间:{{stampToDate(archive.CreatedTime, "2006-01-02 15:04")}}</p>
    <p>更新时间:{{stampToDate(archive.UpdatedTime, "2006年01月02日 下午03时04分")}}</p>
    
  • Retrieve timestamp on article list page:In article list template (such as{模型table}/list.html), you usually usearchiveListLabel loops through multiple articles. Inside the loop, the data of each article is assigned to a variable (for exampleitem), and then you can extract the timestamp from it.

    {% archiveList archives with type="page" limit="10" %}
        {% for item in archives %}
            <div>
                <h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
                <p>发布于:{{stampToDate(item.CreatedTime, "2006-01-02")}}</p>
                <p>最近更新:{{stampToDate(item.UpdatedTime, "01/02 15:04")}}</p>
            </div>
        {% endfor %}
    {% endarchiveList %}
    

Common time formatting examples

To better illustratestampToDateThe flexibility, here are some common formatting requirements and their corresponding format strings:

  • Only display the date (year-month-date)For example:2023-02-09Format string:"2006-01-02"

  • Display the full date and timeFor example:2023-02-09 15:04:05Format string:"2006-01-02 15:04:05"

  • Chinese date formatFor example:2023年02月09日Format string:"2006年01月02日"

  • Short date format (Month/Day)For example:02/09Format string:"01/02"

  • Format with the day of the weekFor example:周四, 2023年02月09日Format string:"Mon, 2006年01月02日"(Note here thatMonrepresents the day of the week, which will be automatically translated.

  • Displays time (hour:minute)For example:15:04Format string:"15:04"

You can freely choose and combine these formats according to the overall design of the website and user habits.

Summary

BystampToDateLabel and flexible date and time formatting reference for Go language, AnQiCMS makes it very simple to accurately control the display of article publication and update times in templates.Whether it is a concise date or detailed to the second-level time, you can easily achieve it by adjusting the format string to ensure the accuracy of the timeliness of the website content.


Frequently Asked Questions (FAQ)

  1. Why does the AnQiCMS time format string look strange and not commonY-m-d?Answer: This is because AnQiCMS is developed based on the Go language. Go language does not use placeholders when formatting time, but uses a fixed reference time.2006-01-02 15:04:05. Just replace the corresponding part of this reference time with the output style you want. For example, if you want to display the year, write the format string in it.2006, to display the month, write01.

  2. Ask: If my article has not been updated,UpdatedTimewhat will be displayed?Answer: If the article has never been updated,UpdatedTimethe field usually goes withCreatedTimeThe value of the field is the same. In actual display, you can optionally only displayCreatedTimeorUpdatedTimewithCreatedTimeThe update time is displayed only when different to avoid unnecessary repeated information.

  3. Ask: Can some localized names of weekdays, such as "Monday", be added when formatting timestamps?Answer: Yes, you can. Go language time formatting refers to the string in,MonAbbreviation for the day of the week (for example, "Monday"),MondayRepresent the full name of the week (for example, "Monday"). The system will automatically localize the display according to the language setting of the current website. For example, on a Chinese site, it may be displayed as"Mon, 2006年01月02日", it may be displayed as周一, 2023年02月09日.