GoLang 时间相关的方法

间隔指定时间运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main

import (
"fmt"
"time"
)

func main() {
// 每隔一秒执行一次
tick := time.Tick(time.Second)
for range tick {
fmt.Print(".")
}
}

时间格式化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main

import (
"fmt"
"time"
)

func main() {

currentTime := time.Now()
fmt.Println("当前时间 : ", currentTime)
fmt.Println("当前时间字符串: ", currentTime.String())
fmt.Println("MM-DD-YYYY : ", currentTime.Format("01-02-2006"))
fmt.Println("YYYY-MM-DD : ", currentTime.Format("2006-01-02"))
fmt.Println("YYYY.MM.DD : ", currentTime.Format("2006.01.02 15:04:05"))
fmt.Println("YYYY#MM#DD {Special Character} : ", currentTime.Format("2006#01#02"))
fmt.Println("YYYY-MM-DD hh:mm:ss : ", currentTime.Format("2006-01-02 15:04:05"))
fmt.Println("Time with MicroSeconds: ", currentTime.Format("2006-01-02 15:04:05.000000"))
fmt.Println("Time with NanoSeconds: ", currentTime.Format("2006-01-02 15:04:05.000000000"))
fmt.Println("ShortNum Month : ", currentTime.Format("2006-1-02"))
fmt.Println("LongMonth : ", currentTime.Format("2006-January-02"))
fmt.Println("ShortMonth : ", currentTime.Format("2006-Jan-02"))
fmt.Println("ShortYear : ", currentTime.Format("06-Jan-02"))
fmt.Println("LongWeekDay : ", currentTime.Format("2006-01-02 15:04:05 Monday"))
fmt.Println("ShortWeek Day : ", currentTime.Format("2006-01-02 Mon"))
fmt.Println("ShortDay : ", currentTime.Format("Mon 2006-01-2"))
fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5"))
fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5 PM"))
fmt.Println("Short Hour Minute Second: ", currentTime.Format("2006-01-02 3:4:5 pm"))
}

时间加减

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
func main() {
// Add 时间相加
now := time.Now()
// ParseDuration parses a duration string.
// A duration string is a possibly signed sequence of decimal numbers,
// each with optional fraction and a unit suffix,
// such as "300ms", "-1.5h" or "2h45m".
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
// 10分钟前
m, _ := time.ParseDuration("-1m")
m1 := now.Add(m)
fmt.Println(m1)

// 8个小时前
h, _ := time.ParseDuration("-1h")
h1 := now.Add(8 * h)
fmt.Println(h1)

// 一天前
d, _ := time.ParseDuration("-24h")
d1 := now.Add(d)
fmt.Println(d1)

printSplit(50)

// 10分钟后
mm, _ := time.ParseDuration("1m")
mm1 := now.Add(mm)
fmt.Println(mm1)

// 8小时后
hh, _ := time.ParseDuration("1h")
hh1 := now.Add(hh)
fmt.Println(hh1)

// 一天后
dd, _ := time.ParseDuration("24h")
dd1 := now.Add(dd)
fmt.Println(dd1)

printSplit(50)

// Sub 计算两个时间差
subM := now.Sub(m1)
fmt.Println(subM.Minutes(), "分钟")

sumH := now.Sub(h1)
fmt.Println(sumH.Hours(), "小时")

sumD := now.Sub(d1)
fmt.Printf("%v 天\n", sumD.Hours()/24)

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
t := time.Now()
addOneHour := t.Add(time.Hour)
addTwoHour := t.Add(2 * time.Hour)
fmt.Println("增加1小时:", addOneHour)
fmt.Println("增加2小时:", addTwoHour)

subTwoHour := t.Add(-2 * time.Hour)
fmt.Println("减去2小时:", subTwoHour)

addDate := t.AddDate(1, 0, 0)
fmt.Println("增加1年:", addDate) // 2021-10-24 22:10:53.328973 +0800 CST

subDate := t.AddDate(-1, 0, 0)
fmt.Println("减去1年:", subDate) // 2019-10-24 22:10:53.328973 +0800 CST

before := t.Before(t.Add(time.Hour))
fmt.Println("before:", before)

after := t.After(t.Add(time.Hour))
fmt.Println("after:", after)

计算耗时

1
2
3
4
t := time.Now()
time.Sleep(2e9) // 休眠2秒
delta := time.Now().Sub(t)
fmt.Println("时间差:", delta) // 2.0534341s

时间转换

毫秒转Time

1
2
nanoSecondToTime := time.Unix(0, 1603546715761482000)
fmt.Println("毫秒值转Time:", nanoSecondToTime) // 2020-10-24 21:38:35.761482 +0800 CST

Time转秒

1
2
secondToTime := time.Unix(1603546715, 0)
fmt.Println("秒值转Time:", secondToTime) // 2020-10-24 21:38:35 +0800 CST