In website operations, the display of time information is crucial. Whether it is the publication date of articles, the update time of products, or the submission time of comments, a clear and easy-to-read date and time format can greatly improve user experience.The original timestamp is friendly to machines but cryptic to ordinary visitors.Fortunately, AnQiCMS (AnQiCMS) provides a very convenient template tag -stampToDateCan easily format timestamps into the familiar date and time format.
stampToDateLabel: Let timestamps speak for themselves.
stampToDateThe label is a practical feature built into Anqi CMS template engine, whose core function is to convert a string of numbers (usually a 10-digit Unix timestamp) into a specified date and time string format. Through it, the originally boring timestamp becomes intuitive and easy to understand, for example, to convert1609470335This number becomes "2021-01-01 12:25:35".
stampToDateBasic usage
UsestampToDateThe tag is very simple, its basic syntax is:
{{stampToDate(时间戳, "格式")}}
This includes two main parameters:
- timestamp: The number you want to convert, usually a 10-digit Unix timestamp. This can be the article publish time you obtained 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.Aqii CMS is developed based on 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 Go language
The time formatting rules in Go language are very special, it does not use commonY-m-d H:i:sSuch placeholders are instead used, 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 replace the corresponding number or character with the format you want to display according to the date and time part you want to show.
2006Represents the year01Represents the month (January)02representing the date (2nd)15representing the hour (3 PM, 24-hour format)04representing minutes05representing seconds
By combining these numbers and adding separators, we can create 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 to the date, such as “year”, “month”, “day”, simply insert them into the corresponding number positions.
Actual application example
Now, let's look at some examples of how to usestampToDateactual examples of tags. Suppose we have a variablepublishStampIts value is1609470335.
that only displays dates in the format year-month-day:
发布日期:{{stampToDate(publishStamp, "2006-01-02")}} {# 显示结果:发布日期:2021-01-01 #}Display the date in Chinese format (Year-Month-Date):
发布日期:{{stampToDate(publishStamp, "2006年01月02日")}} {# 显示结果:发布日期:2021年01月01日 #}Display the format of Year/Month/Date and Time:
更新时间:{{stampToDate(publishStamp, "2006/01/02 15:04")}} {# 显示结果:更新时间:2021/01/01 12:25 #}Display the full date and time (Year-Month-Date Time:Minute:Second):
创建时间:{{stampToDate(publishStamp, "2006-01-02 15:04:05")}} {# 显示结果:创建时间:2021-01-01 12:25:35 #}Use in document list (for example, article publish time):In
archiveListorarchiveDetailYou often encounter in tag loopsitem.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 %}
Practical tips
- Flexible combination: You can choose to use, according to your needs, the
2006/01/02/15/04/05Combine with other characters (such as slashes/、dots.、spaces, 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 numeric combination of the reference time.
- Keep consistentOn your website, try to maintain consistency in the date and time format, which helps improve the overall professionalism and user experience.
BystampToDateThe label, Anqi CMS has given website operators powerful time formatting capabilities, making the presentation of website content more refined and humanized.Mastery of this tag will bring great convenience to your content operation.
Frequently Asked Questions (FAQ)
Q1:stampToDateTags anddateWhat are the differences between filters?A1:stampToDateTags are used to format 10-digit Unix timestamps into date-time strings.dateFilters are usually used to process in Go language.time.TimeA type of time object. In AnQi CMS templates, if your data source directly provides timestamps, it is recommended to usestampToDate; If it has been processed by the backend and 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 the inputstampToDateThe timestamp variable of the tag is empty (for examplenilor0It usually displays an empty default string or an invalid or default date (such as '0001-01-01') depending on the internal processing logic of the Anqi CMS. To avoid this, you can usestampToDateBefore, using{% if item.CreatedTime %}such conditional judgment to ensure that the timestamp is valid.
Q3: Can I customize the separators and text in the date? For example, I want to display the format 'February 9, 2023'.A3