import "time"
// 获取当前时间
now := time.Now()
now.Format("2006-01-02 15:04:05") // 6 1 2 3 4 5 2020-12-02 14:33:01
// 前一天
now.AddDate(0,0,-1).Format("2006-01-02") // 当前日期-1天
now.AddDate(0,0,1).Format("2006-01-02") // 当前日期+1天
// 获取某一天
day1 := time.Date(2020,7,1,0,0,0,0,time.UTC)
// 某一天的加减
day1.AddDate(0,0,1) // +1天
day1.AddDate(0,0,-1) // -1天
day1.AddDate(0,1,0) // +1月
// 字符串转时间戳
instr := "2020-07-03 16:57:34"
location, _ := time.LoadLocation("Asia/Shanghai")
st,_ := time.ParseInLocation("2006-01-02 15:04:05",instr,location)
fmt.Println(st.Unix())
// 时间戳转time.Time
getstime := time.Unix(st.Unix(),0)
// time.Time转字符串
getstime.Format("2006-01-02 15:04:05")
time模块
最新推荐文章于 2023-04-24 14:09:08 发布