In content operation, clear and intuitive date information is crucial.Whether it is the release date of a news article or a reminder of the deadline for an event, a concise and easy-to-understand date format can greatly improve the user experience.AnQi CMS is an efficient content management system that provides powerful template tag functions, allowing us to flexibly control content display.Today, let's delve into how to make full use of the powerful template tags of AnQi CMSstampToDateFormat the original timestamp cleverly to only display a concise "month-date".

Time processing art in Anqi CMS template

In Anqi CMS, the creation and update time of articles or products and other content is usually stored in the database in the form of a 10-digit Unix timestamp, for example1609470335. Such original timestamps are very convenient for system processing, but they are quite difficult for ordinary website visitors to understand and not very beautiful.To convert these cold numbers into user-friendly date formats, Anqi CMS providesstampToDateThis is the formatted timestamp label.

stampToDateThe basic usage of labels is very intuitive, it accepts two parameters: the first is the timestamp you want to format, and the second is the date-time format string you want to output. Its syntax is usually presented as:{{stampToDate(时间戳, "格式")}}.

It should be emphasized here that the format parameter's writing may be different from what you are accustomed to in other systems (such as PHP or JavaScript)Y-m-doryyyy-MM-ddThe placeholder pattern is different. Anqicms is developed based on Go language, its date formatting follows the Go language'stime.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.

Understand this is to use it flexiblystampToDateThe key: You should use this reference time to spell out what you want to display in the final output. For example, if you want to display a four-digit year, write2006If you want to display two-digit months, write01If you want to display a two-digit date, write02and so on.

Easily implement the concise date format "month-day"

Now, let's focus on the core goal of the article: how to format a timestamp to display only the 'month-day' concise date.

According to the reference time agreement of the Go language, to display two-digit months, we should use01; to display two-digit dates, we use02Therefore, if we want to display the date in the format "month-day", such as "06-30", then the corresponding format string should be"01-02".

Suppose we are editing a template for a list page of articles, where we need to display the creation time of each article. The creation time of the articles is usually accessed throughitem.CreatedTime(InarchiveLista loop) orarchive.CreatedTime(InarchiveDetailSuch variables can be accessed, they all store 10-digit Unix timestamps. We can use them in the template like thisstampToDateTags:

<span>发布日期:{{ stampToDate(item.CreatedTime, "01-02") }}</span>

In this code block,item.CreatedTimeRepresents the creation timestamp of the current looping article. When the template engine parses this line of code,stampToDatethe tag will receive this timestamp and follow the specifications we have indicated"01-02"Format it. Finally, on the web page, you will see something similar06-30/11-25This concise and clear date display perfectly meets the need to only display "month-day".

Further customization of date format

stampToDateThe power of labels goes beyond this, its flexibility can help you meet various date display needs.Once you master 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") }}The output is as follows2023-06-30.

If you want to display a 'Year-Month-Day' format with Chinese, you can write it like this:{{ stampToDate(item.CreatedTime, "2006年01月02日") }}The output is as follows2023年06月30日.

If you only need to display time to the 'hour:minute' precision, you can do it like this:{{ stampToDate(item.CreatedTime, "15:04") }}The output is as follows12:30.

It is not difficult to see from these examples that no matter what date formatting needs you have, you just need to refer to the reference time of the Go language and write the desired format string with its corresponding parts.

Cautionary notes and **practice

While usingstampToDateWhen labeling, there are several small details worth your attention to ensure that the date display is always accurate.

  • The accuracy of the timestamp: stampToDateThe label expects to receive a standard 10-digit Unix timestamp (second level).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 knowing this helps in troubleshooting.
  • The accuracy of the format string:Be sure to refer to the Go language'sMon Jan 2 15:04:05 MST 2006This reference time to write the format string. Any characters that do not match this (unless they are separators or plain text) may cause the formatting result to be incorrect.
  • Make good use of the document field:In AnQi CMS,archive.CreatedTimeoritem.CreatedTimeIt is the most commonly used method to obtain the content creation timestamp, whilearchive.UpdatedTimeoritem.UpdatedTimeis used to obtain the last update time of the content. Choose the appropriate field according to your needs.
  • Maintain the date format of the website consistently:To enhance user experience and website professionalism, it is recommended to use a unified date display format throughout the 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

BystampToDateLabels, Anqi 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, and it will make your Anqi CMS website more refined and professional in detail.

Frequently Asked Questions (FAQ)

Q1: I inputtedCreatedTimeSometimes errors or unexpected results may occur, what are the reasons?A1: This usually has several reasons: first, make sureCreatedTimeIt is indeed a 10-digit Unix timestamp (in seconds). If it is in another format or length