GO语言常用标准库03---time包
Posted yunweiqiang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GO语言常用标准库03---time包相关的知识,希望对你有一定的参考价值。
package main import ( "fmt" "time" ) func main021() { nowTime := time.Now() fmt.Println(nowTime.Year())//2018 fmt.Println(nowTime.Month())//December fmt.Println(nowTime.Day())//25 fmt.Println(nowTime.Date())//2018 December 25 fmt.Println(nowTime.Hour())//10 fmt.Println(nowTime.Minute())//26 fmt.Println(nowTime.Second())//? fmt.Println(nowTime.Nanosecond())//? fmt.Println(nowTime.Weekday())//Tuesday fmt.Println(nowTime.YearDay())//359 //定义2019年元旦的Time对象 yuandanTime := time.Date(2019, time.January, 1, 0, 0, 0, 0, time.Now().Location()) fmt.Println(yuandanTime) //2019-01-01 00:00:00 +0800 CST //计算当前时间距离2019年元旦的时间差 duration := nowTime.Sub(yuandanTime) fmt.Println(duration)//-157h19m31.9387253s //解析获得时间差Duration对象 duration, _ = time.ParseDuration("157h19m31.9387253s") //将当前时间时间加上时间差获得新的时间对象 newTime := nowTime.Add(duration) fmt.Println("newTime=",newTime) } /*计算中国成为大一统国家的历史长度*/ func main() { duration := time.Now().Sub(time.Date(-211, time.January, 1, 0, 0, 0, 0, time.Now().Location())) fmt.Println(duration.String()) time.Now().Unix() }
以上是关于GO语言常用标准库03---time包的主要内容,如果未能解决你的问题,请参考以下文章