It is crucial to display time information in website operation.Whether it's the publication date of the article, the update time of the product, or the submission moment of the comments, a clear and easy-to-read date and time format can greatly enhance user experience.The original timestamp is machine-friendly, but cryptic to the general visitor.stampToDateCan easily format timestamps into the familiar date and time format.
stampToDateTag: Let timestamps speak.
stampToDateTags are a practical feature built into the AnQi CMS template engine. Their core function is to convert a string of numbers (usually a 10-digit Unix timestamp) into a date and time string in a specified format. With it, the originally dry and tasteless timestamp becomes intuitive and easy to understand, for example, converting to1609470335This number becomes “2021-01-01 12:25:35”.
stampToDateBasic Usage
UsestampToDateThe tag is very simple, its basic syntax is:
{{stampToDate(时间戳, "格式")}}
This contains two main parameters:
- TimestampThe number you want to convert, usually a 10-digit Unix timestamp. This can be the article publish time you get from the database (such as
item.CreatedTime) or any other timestamp variable. - FormatThis is a string used to define the display style of date and time.The Anqi CMS is developed in Go language, so its time formatting follows the unique rules of the Go language, which we will explain in detail later.
Understand the time formatting rules of the Go language
The time formatting rules of the Go language are very special, as it does not use commonY-m-d H:i:s[This placeholder is not used, instead, a fixed "reference time" is used as a template. This reference time is:]
2006-01-02 15:04:05
You just need to remember this time, and then replace the corresponding number or character with the desired format according to the date and time part you want to display.
2006Represents the year01Represents the month (January)02Represents date (2nd day of the month)15Represents hour (3 PM, 24-hour format)04Represents minutes05Represents seconds
By combining these numbers, and adding separators, we can construct various time display formats. For example:
- If you only want to display the year, just write
2006. - If you want to display the month and date, write
01-02. - If you want to display the full time, write
15:04:05. - If you want to add Chinese characters such as 'year', 'month', and 'day' to the date, simply insert them into the corresponding number positions.
Actual application example
Now, let's look at some examples of how to use in the Anqi CMS templatestampToDateactual examples of tags. Suppose we have a variablepublishStampwith the value1609470335.
that only displays dates in the format of year-month-date:
发布日期:{{stampToDate(publishStamp, "2006-01-02")}} {# 显示结果:发布日期:2021-01-01 #}Display the date in Chinese format of year-month-date:
发布日期:{{stampToDate(publishStamp, "2006年01月02日")}} {# 显示结果:发布日期:2021年01月01日 #}Display the format of year/month/day and time:minute:
更新时间:{{stampToDate(publishStamp, "2006/01/02 15:04")}} {# 显示结果:更新时间:2021/01/01 12:25 #}Display the complete date and time (year-month-day time:minute:second):
创建时间:{{stampToDate(publishStamp, "2006-01-02 15:04:05")}} {# 显示结果:创建时间:2021-01-01 12:25:35 #}In the document list, use (for example, article publish time):In
archiveListorarchiveDetailIn the tag loop, you often encounteritem.CreatedTimeoritem.UpdatedTimesuch variables, which are usually timestamps.{% archiveList archives with type="list" limit="10" %} {% for item in archives %} <div> <h3><a href="{{item.Link}}">{{item.Title}}</a></h3> <p>发布于:{{stampToDate(item.CreatedTime, "2006-01-02")}}</p> </div> {% endfor %} {% endarchiveList %}
Useful tips
- Flexible combinationYou can combine as needed, such as:
2006/01/02/15/04/05with other characters (such as the slash)/, dot., space, etc.) to create various formats. - Preview and debugAfter modifying the template, be sure to preview in the background or directly access the page to ensure that the date and time display meets expectations. If the format is incorrect, it is usually due to an error in the numerical combination of the reference time.
- Maintain consistencyOn your website, try to maintain consistency in the date and time format, which helps to enhance overall professionalism and user experience.
PassstampToDateTags, AnQi CMS grants website operators powerful time formatting capabilities, making the presentation of website content more refined and user-friendly.Mastery of this tag will bring great convenience to your content operation.
Common Questions (FAQ)
Q1:stampToDateTags anddateWhat are the differences between filters?A1:stampToDateTags are specifically used to format a 10-digit Unix timestamp into a date and time string.dateFilters are usually used to process in the Go language.time.TimeA type time object. In the templates of AnQi CMS, if your data source directly provides timestamps, it is recommended to usestampToDate; if it has been converted to Go'stime.TimeAn object can be useddatea filter (althoughstampToDateit is usually more common and direct).
Q2: If the timestamp I provide is empty or inaccurate,stampToDatewhat will the tag display?A2: If passedstampToDateThe timestamp variable of the label is empty (for examplenilor0It usually displays an empty string by default, or an invalid or default date (such as "0001-01-01") depending on the internal processing logic of the safety CMS. To avoid this situation, you can usestampToDateBefore, using{% if item.CreatedTime %}such conditional judgment to ensure that the timestamp is valid.
Q3: Can I customize the separator and text in dates? For example, do I want to display the format 'February 9, 2023'?A3