🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] ## time包 ### 时间格式化格式 ~~~ format := "2006-01-02 15:04:05" ~~~ ### 获取当前时间 ~~~ now := time.Now() fmt.Println(now) //2024-02-29 11:3 4.41257 +0800 CST m=+0.000129751 //时间戳 timeStamp := now.Unix() fmt.Println(timeStamp) // 1709176339 fmt.Printf("%T \n", timeStamp) //int64 //毫秒、微妙、纳秒 fmt.Println(now.UnixMilli(), now.UnixMicro(), now.UnixNano()) //1709176339194 1709176339194819 1709176339194819000 ~~~ ### 年月日时分秒、星期、一年的某天 ~~~ //获取年月日 year,month,day := now.Date() //可以单独获取年月日 year1 := now.Year() month1 := now.Month() day1 := now.Day() //获取时分秒 hour,minute,second := now.Clock() //可以单独获取时分秒 hour1 := now.Hour() minute1 := now.Minute() second1 := now.Second() //一年中的第几天 yeadDay := now.YearDay() fmt.Println(yeadDay) //60 //一周中的第几天 weekDay := now.Weekday() fmt.Println(weekDay) // Thursday 英语上的周一到周日 // 转化数字 weekDayInt := int(weekDay) fmt.Println(weekDayInt) // 4 周日是0 范围0-6 ~~~ ### 格式化时间 ~~~ now := time.Now() fmt.Println(now.Format("2006-01-02 15:03:04")) //2024-02-29 12:12:07 fmt.Println(now.Format("2006-01-02")) //2024-02-29 fmt.Println(now.Format("15:03:04")) //12:12:07 fmt.Println(now.Format("2006/01/02 15:04")) //2024/02/29 12:07 fmt.Println(now.Format("15:04 2006/01/02")) //12:07 2024/02/29 ~~~ ### 设置时区获取时间 ~~~ //加载时区 loc, _ := time.LoadLocation("Asia/Shanghai") t, _ := time.ParseInLocation(format, nowTimeStr, loc) fmt.Println(now) //2024-02-29 11:49:17 +0800 CST ~~~ ### 指定时间返回 ~~~ local, _ := time.LoadLocation("Asia/Shanghai") //local := time.Local //分别指定年,月,日,时,分,秒,纳秒,时区 onlyDay := time.Date(2024, 3, 3, 23, 0, 0, 0, local) ~~~ ### 设置时间大于24小时 ~~~ //分别指定年,月,日 ,可以设置负数 now := time.Now()//2024-02-29 11:49:17 +0800 CST addDate := now.AddDate(0,0,1); fmt.Println(addDate)//2024-03-01 11:49:17 +0800 CST ~~~ ### 设置时间小于24小时 ~~~ now := time.Now() //加法 1小时之后 later := now.Add(time.Hour) fmt.Println(now.Unix(), later.Unix()) //1709187802 1709191402 //减法 sub1 := later.Add(-time.Hour) sub2 := later.Sub(now) fmt.Println(sub1.Unix(), sub2) //1707740816 1h0m0s //判断两个时间是否相同,会考虑时区的影响,因此不同时区标准的时间也可以正确比较 isEqual := later.Equal(later) fmt.Println(isEqual) // true isAfer := later.After(now) fmt.Println(isAfer) // true isBefore := later.Before(now) fmt.Println(isBefore) // false //使用ParseDuration t1, _ := time.ParseDuration("13h4m5s") after2 := now.Add(t1) //2024-03-01 03:24:07.560044 +0800 CST m=+47045.000031876 ~~~ ### time.since ~~~ now := time.Now() time.Sleep(3 * time.Second)//睡眠3秒 dua := time.Since(now) // 可以用于统计代码执行时长 3.00147325s fmt.Println(dua) ~~~ ### timer ~~~ tim := time.NewTimer(1 * time.Second) <-tim.C fmt.Println(time.Now().Unix()) tim.Stop() ~~~ ### ticker ~~~ //本质上是一个channel ticker := time.Tick(time.Second) //定义一个1秒间隔的定时器 for i := range ticker { fmt.Println(i) //每秒都会执行的任务 } ~~~ ## 封装 ### 日期-->时间戳 调用时注意fmtStr与valueStr格式相同 ~~~ func Date2TimeStamp(fmtStr, valueStr, locStr string) int64 { loc := time.Local if locStr != "" { loc, _ = time.LoadLocation(locStr) // 设置时区 } if fmtStr == "" { fmtStr = "2006-01-02 15:04:05" //设置时间格式 } t, _ := time.ParseInLocation(fmtStr, valueStr, loc) return t.Unix() } ~~~ ### 时间戳-->日期 ~~~ func TimeStamp2Date(sec int64, fmtStr string) string { if fmtStr == "" { fmtStr = "2006-01-02 15:04:05" } return time.Unix(sec, 0).Format(fmtStr) } ~~~ ### 时间日期Time.time转换 ![](https://img.kancloud.cn/f9/dd/f9dd2f2b263e41e52b9f511055b85894_1324x290.jpg)