import “time”
官方文档
func main() {fmt.Println(time.Now())// Parse解析一个格式化的时间字符串并返回它代表的时间fmt.Println(time.Parse("2006.01.02", "2012.02.11"))date := time.Now()// 判断两个时间是否相同,会考虑时区的影响,因此不同时区标准的时间也可以正确比较。//本方法和用t==u不同,这种方法还会比较地点和时区信息。fmt.Println(date.Equal(time.Now()))// 如果t代表的时间点在u之前,返回真;否则返回假。time.Sleep(200)temp := time.Now()if temp.Before(date) {fmt.Println("Before")} else {fmt.Println("如果t代表的时间点在u之前,返回真;否则返回假。")}// 如果t代表的时间点在u之后,返回真;否则返回假。if temp.After(date) {fmt.Println("Before")} else {fmt.Println("如果t代表的时间点在u之前,返回真;否则返回假。")}fmt.Println(time.Date)// 返回t对应的那一天的时、分、秒。fmt.Println(temp.Clock())fmt.Println(temp.Year())fmt.Println(temp.Month())fmt.Println(temp.Day())// 返回时间点t对应的那一年的第几天,平年的返回值范围[1,365],闰年[1,366]fmt.Println(temp.YearDay())fmt.Println(temp.Day())fmt.Println(temp.Weekday())fmt.Println(temp.Hour())fmt.Println(temp.Minute())fmt.Println(temp.Second())// 纳秒fmt.Println(temp.Nanosecond())//AddDate返回增加了给出的年份、月份和天数的时间点Time。fmt.Println(temp.Date())fmt.Println(temp.AddDate(1,0,0))// 时间的减法fmt.Println(temp.Sub(date))fmt.Println(temp.Sub(date).Seconds())// 必须是这个时间否则无法解析layout := "2006-01-02 03:04:05"fmt.Println(date)fmt.Println(date.Format(layout))fmt.Println(date.String())
}