In website content operation, the way dates and times are presented often directly affects user experience and the clarity of information.A professional and readable date format not only enhances the credibility of the content but also allows visitors to quickly obtain key information.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 this original data (usually timestamps) into the desired年-月-日/时:分:秒Or a more customized format? This article will delve into the handling of AnQiCMS templatestime.TimeA strategy and method for formatting a type (or more common timestamp) object into a specified date string.
Understanding date data in AnQiCMS.
AnQiCMS stores the article publishing time (such asarchive.CreatedTime)、Update Time(archive.UpdatedTimeWhen dealing with such date data, the Unix timestamp format is usually used.This represents the number of seconds elapsed since the Unix epoch (January 1, 1970, 00:00:00 UTC).Therefore, directly displaying these timestamps in the template often results in a string of digits that is difficult to understand, and we need to format them into human-readable date strings.
Core Tool:stampToDateTag
AnQiCMS provides a very practical template tag for this:stampToDate. This tag is specifically used to convert timestamps into date strings in a specified format.
The basic syntax of its use is:
{{ stampToDate(时间戳变量, "格式字符串") }}
The "timestamp variable" here is usually a timestamp field available in the template, such asitem.CreatedTime. And the format string is the key to defining the style in which you want the date to be displayed.
Understand the formatting strings in Go language.
This isstampToDateThe label is the most unique and the most important to understand. It is different from what we are accustomed to.YYYY-MM-DDThe placeholder is different, 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 year01Represents the month (with leading zero)02Represents the day (with leading zero)15Represents the hour in 24-hour format04representing minutes05representing seconds
If you want to display the day of the week, you can also refer to the more complex reference date section provided in the official Go language documentation, such asMonrepresenting the abbreviation Monday,Mondayrepresenting the full week, etc.
How to usestampToDateFormat the date
Let's see a few actual examplesstampToDateHow tags work. Suppose you are dealing with a list of articles and need to display the publication date and time next to each article.
Only show the year, month, and day (for example:
2023-10-26)This is the most common way to display dates.
<span>发布日期:{{ stampToDate(item.CreatedTime, "2006-01-02") }}</span>When
item.CreatedTimehas a value of1698292800At the time, the output will be2023-10-26.Display the full date and time (for example:')
2023-10-26 14:30:00)If you need to format the full time down to the second
<span>发布时间:{{ stampToDate(item.CreatedTime, "2006-01-02 15:04:05") }}</span>The output will be
2023-10-26 14:30:00(Assuming the timestamp corresponds to this local time).Custom format (for example:
10月26日, 星期四)Go's formatting capabilities are very powerful and can combine various styles:
<span>文章日期:{{ stampToDate(item.CreatedTime, "01月02日, 星期一") }}</span>Here
星期一Is the reference date in Go languageMonThe localized format, which will be converted to the correct day of the week based on the timestamp. For example, the output might be10月26日, 星期四.In
archiveDetailused directly in tagsIn
archiveDetailretrieved from tagsCreatedTimeorUpdatedTimewhen, you can also directly use itformatproperty to format, 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
Display the current date and time:
{% now %}TagIf you want to display the current date and time in the template instead of the article's publication time, you can use
{% now %}tags. Its format string rules are withstampToDateSame.<span>当前日期:{% now "2006-01-02" %}</span> <span>当前完整时间:{% now "2006-01-02 15:04:05" %}</span>about
dateFilterAnQiCMS template still contains a filter named
date(for example:{{ someGoTimeObject|date:"2006-01-02" }}). It is usually used to process Go language primitivestime.TimeAn object of type, rather than the common Unix timestamp. In common applications of AnQiCMS templates (such asarchive.CreatedTimeThese fields are directly exposed as timestamps, so please use them firststampToDateTag or within the tagformatProperty to ensure correctness and simplicity
Tips and **Practice
- Keep in mind the reference date of Go language:
2006-01-02 15:04:05Is the foundation for mastering all date formatting. By memorizing this date, you can flexibly combine any format you want. - Maintain uniform formatting:In your website, try to maintain a uniform date and time format, which helps improve user experience and the professionalism of the website.
- Test different formats:Before applying in actual use, be sure to test your formatted strings in the development environment to ensure they display as expected.
- Utilize the flexibility of AnQiCMS:Combine
archiveList/archiveDetailTags such as this allow you to easily display formatted date information in article lists, detail pages, sidebars, and any other location.
Summary
BystampToDateThe tag and its formatting rules based on the Go language reference date, AnQiCMS makes the presentation of dates and times in templates intuitive and powerful.Whether it is a concise date, 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.
Frequently Asked Questions (FAQ)
**Q1: Why do I use `YYYY