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() }