分享
golang snippet 之 time 使用
happen · · 1014 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
时间格式化与解析
// 获取当前时间
t := time.Now() // 2020年05月13日 22:23:56.253851 +0800 CST m=+0.001626130
fmt.Println(t)
//获取当前年月日,时分秒
y := t.Year() //年
m := t.Month() //月
d := t.Day() //日
h := t.Hour() //小时
i := t.Minute() //分钟
s := t.Second() //秒
fmt.Println(y, m, d, h, i, s) // 2020 May 13 22 41 20
// 获取当前时间戳 (int64 数字)
timestamp := t.Unix() // 1589379836
fmt.Println(timestamp)
// 时间 -> 字符串,注意,格式化字符串 2006年01月02日 15:04:05
fmt.Println(t.Format("2006年01月02日 15:04:05")) // 2020年05月13日 22:23:56
// 字符串时间 ->时间戳(带时区)
loc, _ := time.LoadLocation("Asia/Shanghai") //设置时区
tt, _ := time.ParseInLocation("2006年01月02日 15:04:05", "2020年05月13日 22:23:56", loc) //2006-01-02 15:04:05是转换的格式
fmt.Println(tt.Unix()) // 1589379836
// 字符串时间 ->时间戳(不带时区)
ttt, _ := time.Parse("2006年01月02日 15:04:05", "2020年05月13日 22:23:56")
fmt.Println(ttt.Format("2006年01月02日 15:04:05"))
// 时间戳 -> 时间
tm := time.Unix(1531293019, 0)
fmt.Println(tm.Format("2006年01月02日 15:04:05")) //2018-07-11 15:10:19
计算时间差
start := time.Now()
time.Sleep(2 * time.Second)
t := time.Now()
elapsed := t.Sub(start)
fmt.Println(elapsed) // 2.002571917s
fmt.Println(elapsed.Seconds()) // 2.000620669 (float64类型),还可以转换成 hours / minutes 等
// Since is shorthand for time.Now().Sub(t).
fmt.Println(time.Since(start)) // 2.002534752s
定时器使用
// 使用 Ticker
// 定时器,周期性触发
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
done := make(chan bool)
go func() {
time.Sleep(10 * time.Second)
done <- true
}()
for {
select {
case <-done:
fmt.Println("Done!")
return
case t := <-ticker.C:
fmt.Println("Current time: ", t)
}
}
// 使用 Timer
// 定时器,一次触发
goon := true
timer := time.NewTimer(2 * time.Second)
// 防止内存泄漏,不用 stop 关闭,stop 不会关闭 channel
defer timer.Reset(0)
go func() {
<-timer.C
goon = false
}()
for goon {
fmt.Println("loop")
time.Sleep(time.Second)
}
更多使用技巧参考 godoc https://www.godoc.org/time
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信1014 次点击
上一篇:Linux下安装Go
下一篇:Golang——流程控制
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
时间格式化与解析
// 获取当前时间
t := time.Now() // 2020年05月13日 22:23:56.253851 +0800 CST m=+0.001626130
fmt.Println(t)
//获取当前年月日,时分秒
y := t.Year() //年
m := t.Month() //月
d := t.Day() //日
h := t.Hour() //小时
i := t.Minute() //分钟
s := t.Second() //秒
fmt.Println(y, m, d, h, i, s) // 2020 May 13 22 41 20
// 获取当前时间戳 (int64 数字)
timestamp := t.Unix() // 1589379836
fmt.Println(timestamp)
// 时间 -> 字符串,注意,格式化字符串 2006年01月02日 15:04:05
fmt.Println(t.Format("2006年01月02日 15:04:05")) // 2020年05月13日 22:23:56
// 字符串时间 ->时间戳(带时区)
loc, _ := time.LoadLocation("Asia/Shanghai") //设置时区
tt, _ := time.ParseInLocation("2006年01月02日 15:04:05", "2020年05月13日 22:23:56", loc) //2006-01-02 15:04:05是转换的格式
fmt.Println(tt.Unix()) // 1589379836
// 字符串时间 ->时间戳(不带时区)
ttt, _ := time.Parse("2006年01月02日 15:04:05", "2020年05月13日 22:23:56")
fmt.Println(ttt.Format("2006年01月02日 15:04:05"))
// 时间戳 -> 时间
tm := time.Unix(1531293019, 0)
fmt.Println(tm.Format("2006年01月02日 15:04:05")) //2018-07-11 15:10:19
计算时间差
start := time.Now()
time.Sleep(2 * time.Second)
t := time.Now()
elapsed := t.Sub(start)
fmt.Println(elapsed) // 2.002571917s
fmt.Println(elapsed.Seconds()) // 2.000620669 (float64类型),还可以转换成 hours / minutes 等
// Since is shorthand for time.Now().Sub(t).
fmt.Println(time.Since(start)) // 2.002534752s
定时器使用
// 使用 Ticker
// 定时器,周期性触发
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
done := make(chan bool)
go func() {
time.Sleep(10 * time.Second)
done <- true
}()
for {
select {
case <-done:
fmt.Println("Done!")
return
case t := <-ticker.C:
fmt.Println("Current time: ", t)
}
}
// 使用 Timer
// 定时器,一次触发
goon := true
timer := time.NewTimer(2 * time.Second)
// 防止内存泄漏,不用 stop 关闭,stop 不会关闭 channel
defer timer.Reset(0)
go func() {
<-timer.C
goon = false
}()
for goon {
fmt.Println("loop")
time.Sleep(time.Second)
}
更多使用技巧参考 godoc https://www.godoc.org/time