1.时间戳
1.1 获取当前时间戳
时间戳是指 1970年1月1日 0时0分 到现在的 毫秒值
复制代码
1
2
3
4
5
6
7//获取时间戳 func getTimeStamp() { now := time.Now() unix := now.Unix() fmt.Println(unix) }
1.2 时间戳转换
复制代码
1
2
3
4
5
6
7
8
9
10
11
12now := time.Now() // now是返回值 unix := now.Unix() fmt.Println(unix) // 将时间戳转换为时间 // time是包 Unix 是方法名 t := time.Unix(unix, 0) fmt.Println(t)
2.时间加减,比较
2.1 获取当前时间 年 月 日
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24//获取当前时间 now := time.Now() fmt.Println(now) //获取当前年份 year := now.Year() //获取当前月 month := now.Month() //获取当前日 day := now.Day() //获取当前小时 hour := now.Hour() //获取当前分钟 minute := now.Minute() //获取当前秒 second := now.Second() fmt.Printf("当前时间为 %d 年 %d 月 %d 日 %d 时 %d 分 %d 秒", year, month, day, hour, minute, second)
2.2 添加时间
复制代码
1
2
3
4
5
6
7
8
9
10// 获取当前时间 now := time.Now() // 当前时间加上 1.5小时 = 1小时 30 分钟 duration := time.Duration(time.Hour + time.Duration(30)*time.Minute) add := now.Add(duration) fmt.Println(add)
2.3 计算时间间隔
复制代码
1
2
3
4
5
6
7
8
9
10// 计算 2018年8月8日 到 1999年8月8日的时间间隔 // 东八区 location, _ := time.LoadLocation("Asia/Shanghai") new := time.Date(2018, 8, 8, 0, 0, 0, 0, location) old := time.Date(1999, 8, 8, 0, 0, 0, 0, location) sub := new.Sub(old) fmt.Println(sub)
2.4 判断时间是否在前 Before
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13//获取当前时间 now := time.Now() //当前时间减去30分钟 注意是 负号 duration := time.Minute * 30 old := now.Add(-duration) // 判断 old 是否在 new 之前 flag := old.Before(now) fmt.Println(flag) // 输出结果 true
2.5 判断当前时间是否在后 After
复制代码
1
2
3
4
5
6
7
8
9//获取当前时间 now := time.Now() //当前时间 加了一天 add := now.Add(time.Duration(time.Hour) * 24) // 判断 add 是否在 now 之后 println(add.After(now)) // true
3. Sleep 与 定时器
3.1 Sleep
复制代码
1
2
3
4
5fmt.Println("休眠5秒 ", time.Now().Second()) //程序运行到此处休眠5秒 time.Sleep(time.Second * 5) fmt.Println("休眠结束 ", time.Now().Second())
3.2 定时器
复制代码
1
2
3
4
5
6
7
8
9
10
11num := 0 // 定义一个每秒执行一次的定时器 for t := range time.Tick(time.Second) { //执行5秒后结束 if num++; num >= 5 { return } fmt.Println(t.Second()) }
4. 时间格式化与转换
4.1 时间格式化输出
固定数字 | 对应格式 |
---|---|
2006 | yyyy |
01 | MM |
02 | dd |
15 | HH |
04 | mm |
05 | SS |
复制代码
1
2
3
4
5
6
7
8// 获取当前时间 now := time.Now() // 将当前时间格式化 format := now.Format("2006/01/02 15/04/05") fmt.Println(format)
4.2 将字符串转换为 时间
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13str := "2022/07/04 22/40/50" t, _ := time.Parse("2006/01/02 15/04/05", str) fmt.Println(t) //默认时区 2022-07-04 22:40:50 +0000 UTC // 以指定的时区解析 location, _ := time.LoadLocation("Asia/Shanghai") inLocation, _ := time.ParseInLocation("2006/01/02 15/04/05", str, location) fmt.Println(inLocation) //东八区 2022-07-04 22:40:50 +0800 CST
end 练习
1.统计程序运行时间,精确到纳秒
复制代码
1
2
3
4
5
6
7
8begin := time.Now().Nanosecond() beginUnixMilli := time.Now().UnixMilli() beginSecond := time.Now().Second() strToTime() fmt.Printf("程序运行 %d 纳秒 n", time.Now().Nanosecond()-begin) fmt.Printf("程序运行 %d 毫秒 n", time.Now().UnixMilli()-beginUnixMilli) fmt.Printf("程序运行 %d 秒", time.Now().Second()-beginSecond)
最后
以上就是强健路人最近收集整理的关于【Go ~ 0到1 】 第七天 获取时间戳,时间比较,时间格式转换,Sleep与定时器的全部内容,更多相关【Go内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复