golang中对时间和日期格式处理

date format in Go

golang 时间格式化文档time constants

1
2
3
4
5
6
7
time.Now().UTC().Format("2006-01-02")

time.Now().UTC().Format("02/01/2006")

time.Now().UTC().Format("2006-01-02 15:04:05")

time.Now().UTC().Format(time.RFC3339)

Parse date with a timezone

1
2
3
4
s := "2022-07-30T19:12:00+01:00"
loc, _ := time.LoadLocation("Europe/Berlin")
t, _ := time.ParseInLocation(time.RFC3339, s, loc)
fmt.Println(t)

计算两个日期的时间差

使用Time.Sub()函数计算两个时间的差值,将差值转换为不同时间单位。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
d := time.Date(2000, 2, 1, 12, 30, 0, 0, time.UTC)
year, month, day := d.Date()

fmt.Printf("year = %v\n", year)
fmt.Printf("month = %v\n", month)
fmt.Printf("day = %v\n", day)

nowDate := time.Date(2022, 7, 30, 17, 0, 0, 0, time.UTC)
oldDate := time.Date(2020, 2, 12, 5, 0, 0, 0, time.UTC)
difference := nowDate.Sub(oldDate)

fmt.Printf("Years: %d\n", int64(difference.Hours()/24/365))
fmt.Printf("Months: %d\n", int64(difference.Hours()/24/30))
fmt.Printf("Weeks: %d\n", int64(difference.Hours()/24/7))
fmt.Printf("Days: %d\n", int64(difference.Hours()/24))
fmt.Printf("Hours: %.f\n", difference.Hours())
fmt.Printf("Minutes: %.f\n", difference.Minutes())
fmt.Printf("Seconds: %.f\n", difference.Seconds())
fmt.Printf("Milliseconds: %d\n", difference.Milliseconds())
fmt.Printf("Microseconds: %d\n", difference.Microseconds())
fmt.Printf("Nanoseconds: %d\n", difference.Nanoseconds())

Unix time in Go

标准库Time包可以轻松处理Unix time,获取时间截,转换时间格式。

Convert date/time to Unix epoch time

标准库time.Time结构包含多个获取Unix时间截的方法,将日期转换类型为int64的时间截

1
2
3
4
5
date := time.Date(2022, 7, 30, 0, 0, 0, 0, time.UTC)
fmt.Println(date.Unix())
fmt.Println(date.UnixMilli())
fmt.Println(date.UnixMicro())
fmt.Println(date.UnixNano())

Get current Unix timestamp

获取当前时间的时间截,只需要通过time.Now()方法获取到当前时间的Time结构,然后使用时间截方法转换。

Get current Unix/Epoch time in seconds

1
fmt.Println(time.Now().Unix())

Get current Unix/Epoch time in milliseconds

1
fmt.Println(time.Now().UnixMilli())

Get current Unix/Epoch time in microseconds

1
fmt.Println(time.Now().UnixMicro())

Get current Unix/Epoch time in nanoseconds

1
fmt.Println(time.Now().UnixNano())

Convert Unix timestamp to time.Time

标准库同样提供方法将时间截类型int64转换为Time

Second Unix timestamp to time.Time

1
2
3
timestamp := time.Now().Unix()
t := time.Unix(timestamp, 0)
fmt.Println(t.UTC())

Millisecond Unix timestamp to time.Time

1
2
3
timestamp := time.Now().UnixMilli()
t := time.UnixMilli(timestamp)
fmt.Println(t.UTC())

Microsecond Unix timestamp to time.Time

1
2
3
timestamp := time.Now().UnixMicro()
t := time.UnixMicro(timestamp)
fmt.Println(t.UTC())

Nanosecond Unix timestamp to time.Time

1
2
3
timestamp := time.Now().UnixNano()
t := time.Unix(0, timestamp)
fmt.Println(t.UTC())

Parse Unix timestamp from string to time.Time

将字符串类型的时间截转换为time.Time

1
2
3
4
timeString := "1659173687"
timestamp, _ := strconv.ParseInt(timeString, 10, 64)
t := time.Unix(timestamp, 0)
fmt.Println(t.UTC())

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)

参考