增加 Golang Time 格式化输出。

Signed-off-by: rick.chan <cy@haoan119.com>
This commit is contained in:
rick.chan 2025-04-01 16:23:31 +08:00
parent 4a5a154ab8
commit 6ee5b499fa

View File

@ -0,0 +1,183 @@
# Golang Time 格式化输出
```go
time.Now().Format("2006-01-02 15:04:05")
```
Format layout 如下表:
| Unit | Golang Layout | Examples | Note |
|-------------|---------------|-----------------------------------|----------------------------------|
| Year | 06 | 21, 81, 01 | |
| Year | 2006 | 2021, 1981, 0001 | |
| Month | January | January, February, December | |
| Month | Jan | Jan, Feb, Dec | |
| Month | 1 | 1, 2, 12 | |
| Month | 01 | 01, 02, 12 | |
| Day | Monday | Monday, Wednesday, Sunday | |
| Day | Mon | Mon, Wed, Sun | |
| Day | 2 | 1, 2, 11, 31 | |
| Day | 02 | 01, 02, 11, 31 | zero padded day of the month |
| Day | _2 | ⎵1, ⎵2, 11, 31 | space padded day of the month |
| Day | 002 | 001, 002, 011, 031, 145, 365, 366 | zero padded day of the year |
| Day | __2 | ⎵⎵1, ⎵⎵2, ⎵11, ⎵31, 365, 366 | space padded day of the year |
| Part of day | PM | AM, PM | |
| Part of day | pm | am, pm | |
| Hour 24h | 15 | 00, 01, 12, 23 | |
| Hour 12h | 3 | 1, 2, 12 | |
| Hour 12h | 03 | 01, 02, 12 | |
| Minute | 4 | 0, 4 ,10, 35 | |
| Minute | 04 | 00, 04 ,10, 35 | |
| Second | 5 | 0, 5, 25 | |
| Second | 05 | 00, 05, 25 | |
| Decisecond | .0 | .1 | Trailing zeros included (. or ,) |
| Millisecond | .000 | .100 | Trailing zeros included (. or ,) |
| Nanosecond | .000000000 | .199000000 | Trailing zeros included (. or ,) |
| Decisecond | .9 | .1 | Trailing zeros omitted (. or ,) |
| Millisecond | .999 | .19 | Trailing zeros omitted (. or ,) |
| Nanosecond | .999999999 | .199 | Trailing zeros omitted (. or ,) |
| Time zone | MST | UTC, MST, CET | |
| Time zone | Z07 | Z, +08, -05 | Z is for UTC |
| Time zone | Z0700 | Z, +0800, -0500 | Z is for UTC |
| Time zone | Z070000 | Z, +080000, -050000 | Z is for UTC |
| Time zone | Z07:00 | Z, +08:00, -05:00 | Z is for UTC |
| Time zone | Z07:00:00 | Z, +08:00:00, -05:00:00 | Z is for UTC |
| Time zone | -07 | +00, +08, -05 | |
| Time zone | -0700 | +0000, +0800, -0500 | |
| Time zone | -070000 | +000000, +080000, -050000 | |
| Time zone | -07:00 | +00:00, +08:00, -05:00 | |
| Time zone | -07:00:00 | +00:00:00, +08:00:00, -05:00:00 | |
还可以使用 Golang 预先设定好的 Layout 风格:
```go
now := time.Now()
fmt.Println(now) // 2009-11-10 23:00:00 +0000 UTC m=+0.000000001
fmt.Println(now.Format(time.Layout)) // 01/02 03:04:05PM '06 -0700
fmt.Println(now.Format(time.ANSIC)) // Mon Jan _2 15:04:05 2006
fmt.Println(now.Format(time.UnixDate)) // Mon Jan _2 15:04:05 MST 2006
fmt.Println(now.Format(time.RubyDate)) // Mon Jan 02 15:04:05 -0700 2006
fmt.Println(now.Format(time.RFC822)) // 02 Jan 06 15:04 MST
fmt.Println(now.Format(time.RFC850)) // Monday, 02-Jan-06 15:04:05 MST
fmt.Println(now.Format(time.Kitchen)) // 3:04PM
fmt.Println(now.Format(time.Stamp)) // Jan _2 15:04:05
```
定义在 time 包的 format.go 文件中:
```go
// These are predefined layouts for use in Time.Format and time.Parse.
// The reference time used in these layouts is the specific time stamp:
//
// 01/02 03:04:05PM '06 -0700
//
// (January 2, 15:04:05, 2006, in time zone seven hours west of GMT).
// That value is recorded as the constant named Layout, listed below. As a Unix
// time, this is 1136239445. Since MST is GMT-0700, the reference would be
// printed by the Unix date command as:
//
// Mon Jan 2 15:04:05 MST 2006
//
// It is a regrettable historic error that the date uses the American convention
// of putting the numerical month before the day.
//
// The example for Time.Format demonstrates the working of the layout string
// in detail and is a good reference.
//
// Note that the RFC822, RFC850, and RFC1123 formats should be applied
// only to local times. Applying them to UTC times will use "UTC" as the
// time zone abbreviation, while strictly speaking those RFCs require the
// use of "GMT" in that case.
// In general RFC1123Z should be used instead of RFC1123 for servers
// that insist on that format, and RFC3339 should be preferred for new protocols.
// RFC3339, RFC822, RFC822Z, RFC1123, and RFC1123Z are useful for formatting;
// when used with time.Parse they do not accept all the time formats
// permitted by the RFCs and they do accept time formats not formally defined.
// The RFC3339Nano format removes trailing zeros from the seconds field
// and thus may not sort correctly once formatted.
//
// Most programs can use one of the defined constants as the layout passed to
// Format or Parse. The rest of this comment can be ignored unless you are
// creating a custom layout string.
//
// To define your own format, write down what the reference time would look like
// formatted your way; see the values of constants like ANSIC, StampMicro or
// Kitchen for examples. The model is to demonstrate what the reference time
// looks like so that the Format and Parse methods can apply the same
// transformation to a general time value.
//
// Here is a summary of the components of a layout string. Each element shows by
// example the formatting of an element of the reference time. Only these values
// are recognized. Text in the layout string that is not recognized as part of
// the reference time is echoed verbatim during Format and expected to appear
// verbatim in the input to Parse.
//
// Year: "2006" "06"
// Month: "Jan" "January" "01" "1"
// Day of the week: "Mon" "Monday"
// Day of the month: "2" "_2" "02"
// Day of the year: "__2" "002"
// Hour: "15" "3" "03" (PM or AM)
// Minute: "4" "04"
// Second: "5" "05"
// AM/PM mark: "PM"
//
// Numeric time zone offsets format as follows:
//
// "-0700" ±hhmm
// "-07:00" ±hh:mm
// "-07" ±hh
// "-070000" ±hhmmss
// "-07:00:00" ±hh:mm:ss
//
// Replacing the sign in the format with a Z triggers
// the ISO 8601 behavior of printing Z instead of an
// offset for the UTC zone. Thus:
//
// "Z0700" Z or ±hhmm
// "Z07:00" Z or ±hh:mm
// "Z07" Z or ±hh
// "Z070000" Z or ±hhmmss
// "Z07:00:00" Z or ±hh:mm:ss
//
// Within the format string, the underscores in "_2" and "__2" represent spaces
// that may be replaced by digits if the following number has multiple digits,
// for compatibility with fixed-width Unix time formats. A leading zero represents
// a zero-padded value.
//
// The formats __2 and 002 are space-padded and zero-padded
// three-character day of year; there is no unpadded day of year format.
//
// A comma or decimal point followed by one or more zeros represents
// a fractional second, printed to the given number of decimal places.
// A comma or decimal point followed by one or more nines represents
// a fractional second, printed to the given number of decimal places, with
// trailing zeros removed.
// For example "15:04:05,000" or "15:04:05.000" formats or parses with
// millisecond precision.
//
// Some valid layouts are invalid time values for time.Parse, due to formats
// such as _ for space padding and Z for zone information.
const (
Layout = "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order.
ANSIC = "Mon Jan _2 15:04:05 2006"
UnixDate = "Mon Jan _2 15:04:05 MST 2006"
RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
RFC822 = "02 Jan 06 15:04 MST"
RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
RFC850 = "Monday, 02-Jan-06 15:04:05 MST"
RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST"
RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
RFC3339 = "2006-01-02T15:04:05Z07:00"
RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
Kitchen = "3:04PM"
// Handy time stamps.
Stamp = "Jan _2 15:04:05"
StampMilli = "Jan _2 15:04:05.000"
StampMicro = "Jan _2 15:04:05.000000"
StampNano = "Jan _2 15:04:05.000000000"
DateTime = "2006-01-02 15:04:05"
DateOnly = "2006-01-02"
TimeOnly = "15:04:05"
)
```