In content operations, clear and intuitive date information is crucial.Whether it is the release date of news articles or reminders for the deadline of events, a concise and easy-to-understand date format can greatly enhance user experience.安企CMS as an efficient content management system, provides powerful template tag functions, allowing us to flexibly control content display.stampToDateThe original timestamp is cleverly formatted to only display the concise 'Month-Day'.
The art of time processing in the Anqi CMS template
In the AnQi CMS, the creation and update time of articles, products, and other content is usually stored in the database in the form of a 10-digit Unix timestamp, for example1609470335.Such an original timestamp is very convenient for system processing, but it is quite difficult for ordinary website visitors to understand and is not very aesthetically pleasing.stampToDateThis is the label for formatting the timestamp.
stampToDateThe basic usage of the label is very intuitive, it accepts two parameters: the first is the timestamp you want to format, and the second is the date and time format string you want to output. Its syntax is usually presented as follows:{{stampToDate(时间戳, "格式")}}.
It is especially important to emphasize the writing style of the "format" parameter, which may differ from what you are accustomed to in other systems (such as PHP or JavaScript)Y-m-doryyyy-MM-ddThe placeholder pattern is different. Anqie CMS is developed in Go language, and its date formatting follows the Go language format.time.FormatStandard, which means it does not use abstract placeholders, but defines the format with a fixed "reference time". This reference time is2006年01月02日15时04分05秒also commonly referred to by Go language developers asMon Jan 2 15:04:05 MST 2006.
Understanding this is to use it flexiblystampToDateEnglish:You hope to display what you see in the final output, use the corresponding part of this reference time to spell it. For example, if you want to display a four-digit year, write2006; If you want to display two-digit months, write01; If you want to display two-digit dates, write02. Continue in this manner.
Easily achieve a concise date format of "month-day"
Now, let's focus on the core goal of the article: how to format timestamps to display only the 'month-day' concise date.
According to the reference time agreement of the Go language, we should use two digits for the month01; To display two digits for the date, we use02Therefore, if we want the date to be displayed in the format of "month-day"01-02".
Assuming we are editing a template for a list page of articles, we need to display the creation time of each article. The creation time of the article is usually throughitem.CreatedTime(InarchiveListin the loop) orarchive.CreatedTime(InarchiveDetailsuch variables are retrieved, all of which store 10-digit Unix timestamps. We can use them in the template like thisstampToDateTags:
<span>发布日期:{{ stampToDate(item.CreatedTime, "01-02") }}</span>
In this code,item.CreatedTimerepresented the creation timestamp of the current article in the loop. When the template engine parses this line of code,stampToDatethe tag will receive this timestamp and follow the specifications we have set"01-02"Processed in format. Ultimately, on the web page, you will see something similar06-30/11-25This concise and clear date display perfectly meets the need to only show "month-day".
Further: Flexible date format customization
stampToDateThe power of tags goes beyond this, its flexibility can help you meet various date display needs.Just by mastering the reference time principle of the Go language, you can freely combine date formats that match the style of your website.
For example, if you need to display the full "year-month-day" format, you can write it like this:{{ stampToDate(item.CreatedTime, "2006-01-02") }}Output like2023-06-30.
If you want to display the "year-month-day" format with Chinese, you can write it like this:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}Output like2023年06月30日.
If you only need to display the time accurate to "hour:minute", it can be written as:{{ stampToDate(item.CreatedTime, "15:04") }}Output like12:30.
It is not difficult to see from these examples that for any date formatting requirement you have, you just need to refer to the reference time in the Go language and use the corresponding part to write the format string you want.
Attention Points and **Practice
When usingstampToDateWhen labeling, there are several small details to pay attention to to ensure that the date is always displayed accurately and correctly:
- Timestamp accuracy:
stampToDate标签期望接收的是标准的10位Unix时间戳(秒级别)。If your timestamp is in milliseconds (13 digits), you may need to process it first, such as dividing by 1000, although Anqi CMS usually handles it internally, but understanding this is helpful for troubleshooting. - Accuracy of format string:Be sure to refer to the Go language:
Mon Jan 2 15:04:05 MST 2006This reference time is used to write the format string. Any character that does not conform to this (unless it is used as a separator or plain text) may cause the formatting result to be incorrect. - Make good use of the document field:In the Anqi CMS,
archive.CreatedTimeoritem.CreatedTimeThis is the most commonly used method to obtain the content creation timestamp, andarchive.UpdatedTimeoritem.UpdatedTimeis used to obtain the last update time of the content. Choose the appropriate field according to your needs. - Maintain the consistency of the website date format:To enhance user experience and website professionalism, it is recommended to use a unified date display format throughout the entire website.For example, once it is decided that both the article list and detail pages use the "month-day" format, try to maintain consistency.
Summary
PassstampToDateLabel, the Anei CMS makes date formatting intuitive and powerful.Just understand the reference time formatting rules of the Go language, and you can easily convert the original timestamp into various user-friendly date displays, including the concise "month-day" format that we focus on today.Master this little trick, it will enable your security CMS website to be more delicate and professional in details.
Common Questions (FAQ)
Q1:I input theCreatedTimeSometimes errors or unexpected results may occur, what is the cause?A1:There are usually several reasons: first, please make sureCreatedTimeThis is indeed a 10-digit Unix timestamp (in seconds). If it is in a different format or length