Unveiling Anqi CMSstampToDateLabel: The mysteries of Go language time formatting strings
In the template development of AnQi CMS,stampToDateThe label is undoubtedly a powerful assistant for processing timestamp data, it can help us convert the original 10-digit timestamp into a user-friendly date and time format. However, when you first encounter its formatting parameters, you may be confused by a seemingly strange string, such as the following2006-01-02 15:04:05.999999999 -0700 MSTWhat do these numbers and letters represent? Why not something more familiar?YYYY-MM-DD HH:mm:ssWhat kind of placeholder is this?
Today, as an experienced veteran of Anqing CMS content operation, I will come to unravel the mystery of Go language's unique time formatting mechanism for you, so that you can use it freely in template creation.
The unique philosophy of Go Language time formatting: not placeholders, but time references
Such as the placeholders commonly used by other programming languages or template engines (likeYYYYrepresents the year,MMRepresenting different months, the Go language adopts a unique method known as "reference time" or "magic time" in its date and time formatting features. This means that the format string you provide is not composed of abstract letter symbols, but rather a specific date and time -2006年1月2日 15点04分05秒, with time zone information.
You might ask, why on this specific date and time?In fact, this is a 'hypothetical' benchmark set by Go language developers for convenience.
- 1represents the month (January)
- 2represents the date (the second day)
- 3represents the hour (3 PM, i.e., 15 o'clock)
- 4represents the minutes (04 minutes)
- 5represents seconds (05 seconds)
- 6represents the year (2006 years)
- 7represents the time zone offset (-0700)
Understood this 'number charm', let's take a look at the one you mentioned2006-01-02 15:04:05.999999999 -0700 MSTThis complete format string corresponds to a specific element in Go language time formatting:
2006: Represents the year. When you include in the format string2006When, Go will output the actual year in a four-digit year format.01It represents the month (January). Similarly, it indicates that Go outputs the actual month's date in a two-digit format (with a leading zero).02: Represents the date (second number). It indicates that Go outputs the actual date in a two-digit format (with a leading zero).15: Represents hours (24-hour format).This tells Go to output the actual hour number in 24-hour format (with leading zeros).3(i.e.),03) orPMto indicate AM/PM.04: represents minutes. It indicates that Go outputs the actual minutes of time in a two-digit format (with a leading zero).05It represents seconds. It indicates that Go outputs the actual number of seconds in a two-digit format (with a leading zero)..999999999This represents nanoseconds (up to 9 decimal places). This means you can ask Go to output the nanosecond part of seconds with up to 9 decimal places.-0700: represents the time zone offset. It indicates that Go uses±HHMMto output the actual time offset from UTC in the format.MSTThe colon represents the time zone abbreviation (Mountain Standard Time, Mountain Standard Time).This indicates the time zone abbreviation name you want Go to output.
In short, you are not telling Go language to 'Please display January 2, 2006', but rather to 'Please display it according to2006the format of the year, according to01The format to display the month, according to02The format to display the date... The string you provided isA template, a reference standard, not a literal value.
In the actual application of the Anqi CMS template
Understood this mechanism, you can flexibly combine any date and time format you want according to your actual needs. In Anqi CMS template files, for example, if you want to display the publication time of an article, you usually useitem.CreatedTimeThis timestamp variable. CombinestampToDateLabel, you can operate like this:
- Only show year, month, and day:
{{ stampToDate(item.CreatedTime, "2006-01-02") }} {# 输出: 2023-10-26 #} - Show year, month, day, and hour with Chinese:
{{ stampToDate(item.CreatedTime, "2006年01月02日 15时04分") }} {# 输出: 2023年10月26日 10时30分 #} - Show year, month, day, hour, and minute in 12-hour AM/PM format:
{{ stampToDate(item.CreatedTime, "2006-01-02 03:04 PM") }} {# 输出: 2023-10-26 10:30 AM #} - Show the day of the week:In the reference time of the Go language, January 2nd is Monday, therefore
MonAbbreviations representing the days of the week.{{ stampToDate(item.CreatedTime, "2006年01月02日 星期Mon") }} {# 输出: 2023年10月26日 星期四 #}
By combining these different parts of the reference time, you can create an infinite number of date and time display formats that precisely meet different content scenarios and user habits.
The value brought by this mechanism
AnQi CMS is developed using Go language, its template layer inherits this time formatting method of Go language, which not only ensures the consistency and efficiency of internal data processing of the system, but also provides extremely high flexibility for template developers.You do not need to remember a lot of abstract placeholder rules, just by combining the various fragments of 'Magic Time', you can intuitively 'draw' the date and time style you want.This is undoubtedly a powerful and practical feature for corporate websites and self-media operators who need to publish multi-language, multi-regional content or have specific date format display requirements, which can greatly enhance the professionalism and user experience of content display.
Frequently Asked Questions (FAQ)
1. Why did the Go language choose the '2006-01-02 15:04:05' date-time format as a reference?
Go language chooses this date and time as a reference mainly for convenience of memorization and clearly representing all common components of date and time. As mentioned in the article, it follows an interesting number sequence: 1 (month), 2 (day), 3 (hour), 4 (minute), 5 (second),