[Go] time.Time 时间格式处理
Contents
golang中对时间和日期格式处理
date format in Go
golang 时间格式化文档time constants
|
|
Parse date with a timezone
|
|
计算两个日期的时间差
使用Time.Sub()函数计算两个时间的差值,将差值转换为不同时间单位。
|
|
Unix time in Go
标准库Time包可以轻松处理Unix time,获取时间截,转换时间格式。
Convert date/time to Unix epoch time
标准库time.Time结构包含多个获取Unix时间截的方法,将日期转换类型为int64的时间截
|
|
Get current Unix timestamp
获取当前时间的时间截,只需要通过time.Now()方法获取到当前时间的Time结构,然后使用时间截方法转换。
Get current Unix/Epoch time in seconds
|
|
Get current Unix/Epoch time in milliseconds
|
|
Get current Unix/Epoch time in microseconds
|
|
Get current Unix/Epoch time in nanoseconds
|
|
Convert Unix timestamp to time.Time
标准库同样提供方法将时间截类型int64转换为Time
Second Unix timestamp to time.Time
|
|
Millisecond Unix timestamp to time.Time
|
|
Microsecond Unix timestamp to time.Time
|
|
Nanosecond Unix timestamp to time.Time
|
|
Parse Unix timestamp from string to time.Time
将字符串类型的时间截转换为time.Time
|
|
Date and time format in Go
时间格式
时间格式定义,time constants
“01/02 03:04:05PM ‘06 -0700”
- 01 - month
- 02 - day
- 03 - hour (12h)
- 04 - minute
- 05 - second
- 06 - year
- 07 - time zone offset
Date format
Year format
| Go layout | Format | Example | Description |
|---|---|---|---|
| 2006 | YYYY | “2022” | Four-digit year |
| 06 | YY | “22” | Two-digit year |
Month format
| Go layout | Format | Example | Description |
|---|---|---|---|
| January | MMMM | “July” | Full month name |
| Jan | MMM | “Jul” | Three-letter abbreviation of the month |
| 01 | MM | “07” | Two-digit month (with a leading 0 if necessary) |
| 1 | M | “7” | At most two-digit month (without a leading 0) |
Day format
| Go layout | Format | Example | Description |
|---|---|---|---|
| Monday | DDDD | “Tuesday” | Full weekday name |
| Mon | DDD | “Tue” | Three-letter abbreviation of the weekday |
| 02 | DD | “08” | Two-digit month day (with a leading 0 if necessary) |
| _2 | _D | " 8” | Two-character month day with a leading space if necessary |
| 2 | D | “8” | At most two-digit month day (without a leading 0) |
| 002 | ddd | “074” | Three-digit day of the year (with a leading 0 if necessary) |
| __2 | __d | " 74” | Three-character day of the year with a leading spaces if necessary |
Time format
Hour format
| Go layout | Format | Example | Description |
|---|---|---|---|
| 15 | hh | “17” | Two-digit 24h format hour |
| 03 | hh | “05” | Two digit 12h format hour (with a leading 0 if necessary) |
| 3 | h | “5” | At most two-digit 12h format hour (without a leading 0) |
| PM | am/pm | “AM” | AM/PM mark (uppercase) |
| pm | am/pm | “am” | AM/PM mark (lowercase) |
Minute format
| Go layout | Format | Example | Description |
|---|---|---|---|
| 04 | mm | "07” | Two-digit minute (with a leading 0 if necessary) |
| 4 | m | "7” | At most two-digit minute (without a leading 0) |
Second format
| Go layout | Format | Example | Description |
|---|---|---|---|
| 05 | ss | “09” | Two-digit second (with a leading 0 if necessary) |
| 5 | s | “9” | At most two-digit second (without a leading 0) |
| .0, .00, …, .000000000 | .s | “.126284000” | A fractional second (trailing zeros included) |
| .9, .99, …, .999999999 | .s | “.126284” | A fractional second (trailing zeros omitted) |
Time zone format
| Go layout | Format | Example | Description |
|---|---|---|---|
| MST | TTT | “CEST” | Abbreviation of the time zone |
| -070000 | ±hhmmss | “+010000” | Numeric time zone offset with hours, minutes, and seconds |
| -07:00:00 | ±hh:mm:ss | “+01:00:00” | Numeric time zone offset with hours, minutes, and seconds separated by colons |
| -0700 | ±hhmm | “+0100” | Numeric time zone offset with hours and minutes |
| -07:00 | ±hh:mm | “+01:00” | Numeric time zone offset with hours and minutes separated by colons |
| -07 | ±hh | “+01” | Numeric time zone offset with hours |
| Z070000 | Z or ±hhmmss | “+010000” | Like -070000 but prints “Z” instead of “+000000” for the UTC zone (ISO 8601 behavior) |
| Z07:00:00 | Z or ±hh:mm:ss | “+01:00:00” | Like -07:00:00 but prints “Z” instead of “+00:00:00” for the UTC zone (ISO 8601 behavior) |
| Z0700 | Z or ±hhmm | “+0100” | Like -0700 but prints “Z” instead of “+0000” for the UTC zone (ISO 8601 behavior) |
| Z07:00 | Z or ±hh:mm | “+01:00” | Like -07:00 but prints “Z” instead of “+00:00” for the UTC zone (ISO 8601 behavior) |
| Z07 | Z or ±hh | “+01” | Like -07 but prints “Z” instead of “+00” for the UTC zone (ISO 8601 behavior) |