In website content operations, clearly displaying the publication and update time of articles is crucial for improving user experience, providing timeliness information about content, and optimizing search engine indexing.AnQiCMS (AnQiCMS) provides a powerful and flexible timestamp formatting feature in the template, allowing us to convert the original timestamp data into user-friendly date and time display according to specific requirements.
To accurately format and display the publication and update time of articles in the AnQiCMS template, we need to use a core template tag:stampToDateThis 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 data
CreatedTime(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. It is common in other CMS systems.
Y-m-d H:i:sThe placeholder format differs, AnQiCMS (because it is developed based on Go language) uses the date and time formatting reference string unique to Go language. This reference string is fixed2006-01-02 15:04:05English translation: . You do not need to remember the meaning of this string, just use it as a template and 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"01/02/2006"Here is the number2006/01/02/15/04/05等,represent the year, month, day, hour, minute, second, and other elements of the time format in the Go language. You can freely combine these elements to define the final display format as needed.
Retrieve 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 for article list page) to implement.
Get timestamp in article detail page:When you are in the article detail template (such as)
{模型table}/detail.html) you can directly access the current article'sarchiveobject to access the current article'sCreatedTimeandUpdatedTimefield.<p>发布时间:{{stampToDate(archive.CreatedTime, "2006-01-02 15:04")}}</p> <p>更新时间:{{stampToDate(archive.UpdatedTime, "2006年01月02日 下午03时04分")}}</p>Get the timestamp on the article list page:In the article list template (such as
{模型table}/list.html), you usually usearchiveListLabel loop to traverse 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:
Show date only (Year-Month-Day)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日"(注意这里的English)MonRepresents the day of the week, will be automatically translated)Displays the 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
PassstampToDateTags and flexible date and time formatting reference in Go language, AnQiCMS makes it very simple to accurately control the display of article publication and update times in templates.Whether you need a concise date or detailed to the second time, you can easily achieve it by adjusting the format string, ensuring the accurate transmission of the timeliness of the website content.
Common Questions (FAQ)
问:Why does the time formatting string of AnQiCMS look strange, not common?
Y-m-d?答:这是因为AnQiCMS基于Go语言开发。Go语言在格式化时间时,不使用占位符,而是使用一个固定的参考时间2006-01-02 15:04:05。You only need to replace the corresponding part with the output style you want according to this reference time. For example, if you want to display the year, write it in the format string.2006,想要显示月份,就写 English01.问:如果我的文章没有更新过,
UpdatedTimeWhat will be displayed?答:如果文章从未被更新,UpdatedTime字段通常会和 EnglishCreatedTimeThe value of the field is the same. In actual display, you can optionally only displayCreatedTimeor inUpdatedTimeWithCreatedTimeThe update time is not displayed at the same time to avoid unnecessary duplicate information.问:能否在格式化时间戳时,增加一些本地化的星期几名称,比如“星期一”、“Monday”?答:可以的。Go语言的时间格式化参考字符串中,
Mon代表星期几的简写(例如“周一”),Monday代表星期几的全称(例如“Monday”)。系统会根据当前网站的语言设置,自动进行本地化显示。例如,在英文站点使用"Mon, 2006年01月02日",可能会显示为周一, 2023年02月09日.