当前时间
now := time.Now()
fmt.Println("now:", now)
now: 2022-04-16 10:50:52.283976 +0800 CST m=+0.000111228
指定时间
then := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
fmt.Println("then:", then)
then: 2009-11-17 20:34:58.651387237 +0000 UTC
参数分别为:年,月,日,时,分,秒,纳秒,时区。
时间字段
now := time.Now()
fmt.Println("now:", now)
fmt.Println("year:", now.Year())
fmt.Println("month:", now.Month())
fmt.Println("day:", now.Day())
fmt.Println("hour:", now.Hour())
fmt.Println("minute:", now.Minute())
fmt.Println("second:", now.Second())
fmt.Println("nanosecond:", now.Nanosecond())
fmt.Println("location:", now.Location())
fmt.Println("weekday:", now.Weekday())
now: 2022-04-16 11:10:21.76101 +0800 CST m=+0.000108970
year: 2022
month: April
day: 16
hour: 11
minute: 10
second: 21
nanosecond: 761010000
location: Local
weekday: Saturday
时间比较
now := time.Now()
then := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
fmt.Println("then before now:", then.Before(now))
fmt.Println("then after now:", then.After(now))
fmt.Println("then equal now:", then.Equal(now))
then before now: true
then after now: false
then equal now: false
时间间隔
now := time.Now()
then := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC)
diff := now.Sub(then)
fmt.Println("diff:", diff)
fmt.Println("hours:", diff.Hours())
fmt.Println("minutes:", diff.Minutes())
fmt.Println("seconds:", diff.Seconds())
fmt.Println("nanoseconds:", diff.Nanoseconds())
diff: 108774h59m46.516185763s
hours: 108774.99625449604
minutes: 6.526499775269763e+06
seconds: 3.9158998651618576e+08
nanoseconds: 391589986516185763
时间计算
now := time.Now()
fmt.Println("now:", now)
fmt.Println("now add 3 hour", now.Add(3*time.Hour))
fmt.Println("now add -3 hour", now.Add(-3*time.Hour))
now: 2022-04-16 11:47:08.20477 +0800 CST m=+0.000109580
now add 3 hour 2022-04-16 14:47:08.20477 +0800 CST m=+10800.000109580
now add -3 hour 2022-04-16 08:47:08.20477 +0800 CST m=-10799.999890420
时间戳
now := time.Now()
fmt.Println("now:", now)
fmt.Println("now unix:", now.Unix())
fmt.Println("now unix milli:", now.UnixMilli())
fmt.Println("now unix nano:", now.UnixNano())
now: 2022-04-16 13:18:19.23993 +0800 CST m=+0.000111504
now unix: 1650086299
now unix milli: 1650086299239
now unix nano: 1650086299239930000
时间戳转换
fmt.Println("time from unix:", time.Unix(1650080700, 0))
fmt.Println("time from unix nano:", time.Unix(0, 1650080700587122000))
time from unix: 2022-04-16 11:45:00 +0800 CST
time from unix nano: 2022-04-16 11:45:00.587122 +0800 CST
时间格式化
以下是格式化的说明:
// Year: “2006” “06”
// Month: “Jan” “January”
// Textual day of the week: “Mon” “Monday”
// Numeric day of the month: “2” “_2” “02”
// Numeric 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
// 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
now := time.Now()
fmt.Println(now.Format(time.RFC3339))
fmt.Println(now.Format("3:04PM"))
fmt.Println(now.Format("Mon Jan _2 15:04:05 2006"))
fmt.Println(now.Format("2006-01-02T15:04:05.999999-07:00"))
2022-04-16T14:24:19+08:00
2:24PM
Sat Apr 16 14:24:19 2022
2022-04-16T14:24:19.73223+08:00
now := time.Now()
fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
2022-04-16T14:31:59-00:00
时间字符串解析
layout := "3 04 PM"
t, _ := time.Parse(layout, "8 41 PM")
fmt.Println(t)
0000-01-01 20:41:00 +0000 UTC
t, _ := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")
fmt.Println(t)
2012-11-01 22:08:41 +0000 +0000
解析失败时,error会被返回。
layout := "Mon Jan _2 15:04:05 2006"
_, e := time.Parse(layout, "8:41PM")
fmt.Println(e)
parsing time “8:41PM” as “Mon Jan _2 15:04:05 2006”: cannot parse “8:41PM” as “Mon”
本文详细介绍了Go语言中处理当前时间、指定时间、时间字段、比较、间隔计算、格式化以及时间戳的用法,包括年月日、时分秒、时区等,并展示了关键的时间操作技巧和示例。
368

被折叠的 条评论
为什么被折叠?



