1. golang时间time
1.1 time包处理时间 ,我们一般情况下需要的是 :
a.格式化时间字符串 : 2021-12-11 12:13:14 或 2021/12/11 12:13:14
b.时间戳:1689649862 秒
1.2 要明白的是 time是一个强大的时间对象,我们通过 时间字符串或时间戳转化为时间对象,然后通过时间对象去获取到我们需要的时间,比如 上一周 、上一个月、上一年、昨天、明天.....,最后将时间对象转换成我们需要的 时间字符串或时间戳
1.3 所以时间转换 时间对象计算 非常重要
2. 时间转换
2.1 注意:时间字符串转换需要模板 :"2006-01-02 15:04:05", 时间对象有时区,不同时区获取到的时间是不一样的
2.2 时间戳转时间对象
// 时间戳 转时间对象
timestamp := 1689690584
t := time.Unix(int64(timestamp), 0)
fmt.Println(t)
2.3 时间格式字符串 转 时间对象
// 格式化时间字符串 转时间对象
timeStr := "2023-07-18 11:11:02"
t, _ := time.ParseInLocation("2006-01-02 15:04:05", timeStr, time.Local)
fmt.Println(t)
注意:时间字符串转时间对象,还有 time.Parse()这个函数,但是我们会发现转成的时间会相差8个小时,其原因就是时区不同。
2.4 时间对象转时间戳 和 时间格式字符串
//时间对象转时间戳
timestamp := 1689690584
t := time.Unix(int64(timestamp), 0)
fmt.Println(t.Unix()) //时间戳
fmt.Println(t.Format("2006-01-02 15:04:05")) //字符串
3.时间对象的计算
3.1所谓的时间对象计算就是,通过时间对象的函数获取到我们需要的时间
// 根据某天的时间戳得到这天的开始时间和结束时间
timestamp := 1689782939 //2023-07-20 00:08:59
t := time.Unix(int64(timestamp), 0)
str := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local).Format("2006-01-02 15:04:05")
fmt.Println(str)
解释:时间戳转为 time对象,通过此time对象获得 年 月 日 ,另外通过time.Date() 构建一个新的time对象,根据新的年月日 时分秒 参数来构建, 这样我们就可以通过某个时间戳,得到当天开始时间或结束时间的时间戳或时间字符串。
3.2 我们需要昨天、今天、明天、上个月、当月、下个月、去年、今年、明年 的时间,可以通过time对象的 AddDate方法进行计算
使用案例如下:
//昨天
timestr := "2023-07-20 00:08:59" //时间字符串
t, _ := time.ParseInLocation("2006-01-02 15:04:05", timestr, time.Local) //转时间对象
//AddDate 参数:年,月,日 第三个参数 -1 即减少一天,则为昨天, 1为加一天,0为当天
t = t.AddDate(0, 0, -1)
str := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local).Format("2006-01-02 15:04:05")
fmt.Println(str) //2023-07-19 00:08:59
//昨天开始时间
t = t.AddDate(0, 0, -1) //参数:年,月,日 第三个参数 -1 即减少一天,则为昨天
//Date方法:时分秒毫秒 固定为0,年月日从上个time对象方法获得
str = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local).Format("2006-01-02 15:04:05")
fmt.Println(str) //2023-07-18 00:00:00
//昨天结束时间
t = t.AddDate(0, 0, -1) //参数:年,月,日 第三个参数 -1 即减少一天,则为昨天
//Date方法:时分秒毫秒 固定为23, 59, 59, 0,年月日从上个time对象方法获得
str = time.Date(t.Year(), t.Month(), t.Day(), 23, 59, 59, 0, time.Local).Format("2006-01-02 15:04:05")
fmt.Println(str) //2023-07-17 23:59:59
//下个月
t = t.AddDate(0, 1, 0) //参数:年,月,日 第二个参数 +1 即增加一个月,则为下个月
str = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local).Format("2006-01-02 15:04:05")
fmt.Println(str) //2023-08-19 00:08:59
//去年
t = t.AddDate(-1, 0, 0) //参数:年,月,日 第一个参数 -1 即减少一年,则为去年
str = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, time.Local).Format("2006-01-02 15:04:05")
fmt.Println(str) //2022-08-19 00:08:59
注意:time对象的 AddDate方法三个参数,分别代表年月日,0为不添加,1为添加1天或1月或1年,-1为 减少1天 1月 1年 。 time.Date方法是重新构建一个time对象,通过具体的年月日时分秒参数构建。
自己的工具包 mytime
package tool
import (
"strconv"
"time"
)
// 时间字符串转时间戳 timestr格式 yyyy-mm-dd hh:ii:ss timestamp int
func TimestrToTimestamp(timestr string) int {
t, _ := time.ParseInLocation(time.DateTime, timestr, time.Local)
return int(t.Unix())
}
// 时间戳转时间字符串
func TimestampToTimestr(timestamp int) string {
t := time.Unix(int64(timestamp), 0)
return t.Format(time.DateTime)
}
// n小时前后 1h:1小时后 -1h:1小时前(当前时间为基准)
func NHourLastAfterTimestamp(str string) int {
nowT := time.Now()
hh, _ := time.ParseDuration(str)
t := nowT.Add(hh)
return int(t.Unix())
}
// 根据秒数获取相差 天时分秒
func SecondsDifferDayHoursMinutesSeconds(seconds int) (day, hour, minute, second int) {
day = seconds / 60 / 60 / 24 % 365
hour = seconds / 60 / 60 % 24
minute = seconds / 60 % 60
second = seconds % 60
return
}
// 根据时间字符串获取年月日int
func TimestrGetYearMonthDayInt(timestr string) (year, month, day int) {
t, _ := time.ParseInLocation(time.DateTime, timestr, time.Local)
year = t.Year()
month, _ = strconv.Atoi(t.Format("01"))
day = t.Day()
return
}
// 根据时间戳获取年月日int
func TimestampGetYearMonthDayInt(timestamp int) (year, month, day int) {
t := time.Unix(int64(timestamp), 0)
year = t.Year()
month, _ = strconv.Atoi(t.Format("01"))
day = t.Day()
return
}
// 今天开始时间与结束时间戳
func CurDayStartEndTimestamp() (StartTimeStamp, EndTimeStamp int) {
nowT := time.Now()
CurYear, CurMonth, CurDay := nowT.Date()
StartTimeStamp = int(time.Date(CurYear, CurMonth, CurDay, 0, 0, 0, 0, time.Local).Unix())
EndTimeStamp = int(time.Date(CurYear, CurMonth, CurDay, 23, 59, 59, 0, time.Local).Unix())
return
}
// 昨天开始与结束时间戳
func YesterdayStartEndTimestamp() (StartTimeStamp, EndTimeStamp int) {
nowT := time.Now()
yesterT := nowT.AddDate(0, 0, -1)
StartTimeStamp = int(time.Date(yesterT.Year(), yesterT.Month(), yesterT.Day(), 0, 0, 0, 0, time.Local).Unix())
EndTimeStamp = int(time.Date(yesterT.Year(), yesterT.Month(), yesterT.Day(), 23, 59, 59, 0, time.Local).Unix())
return
}
// 某天时间字符串获取其当天开始与结束时间戳 timestr格式 yyyy-mm-dd hh:ii:ss
func DayTimestrToStartEndTimestamp(timestr string) (StartTimeStamp, EndTimeStamp int) {
t, _ := time.ParseInLocation(time.DateTime, timestr, time.Local)
CurYear, CurMonth, CurDay := t.Date()
StartTimeStamp = int(time.Date(CurYear, CurMonth, CurDay, 0, 0, 0, 0, time.Local).Unix())
EndTimeStamp = int(time.Date(CurYear, CurMonth, CurDay, 23, 59, 59, 0, time.Local).Unix())
return
}
// 某天时间字戳获取其当天开始与结束时间戳
func DayTimestampToStartEndTimestamp(timestamp int) (StartTimeStamp, EndTimeStamp int) {
t := time.Unix(int64(timestamp), 0)
CurYear, CurMonth, CurDay := t.Date()
StartTimeStamp = int(time.Date(CurYear, CurMonth, CurDay, 0, 0, 0, 0, time.Local).Unix())
EndTimeStamp = int(time.Date(CurYear, CurMonth, CurDay, 23, 59, 59, 0, time.Local).Unix())
return
}
// 这个月开始与结束时间戳
func CurMonthStartEndTimestamp() (StartTimeStamp, EndTimeStamp int) {
t := time.Now()
CurYear, CurMonth, _ := t.Date()
StartTimeStamp = int(time.Date(CurYear, CurMonth, 1, 0, 0, 0, 0, time.Local).Unix())
EndTimeStamp = int(time.Date(CurYear, CurMonth+1, 1, 0, 0, 0, -1, time.Local).Unix())
return
}
// 上个月的开始与结束时间戳
func LastMonthStartEndTimestamp() (StartTimeStamp, EndTimeStamp int) {
t := time.Now()
CurYear, CurMonth, _ := t.Date()
StartTimeStamp = int(time.Date(CurYear, CurMonth-1, 1, 0, 0, 0, 0, time.Local).Unix())
EndTimeStamp = int(time.Date(CurYear, CurMonth, 1, 0, 0, 0, -1, time.Local).Unix())
return
}
// 今年某个月的开始与结束时间戳
func SomeMonthStartEndTimestamp(m time.Month) (StartTimeStamp, EndTimeStamp int) {
t := time.Now()
CurYear, _, _ := t.Date()
StartTimeStamp = int(time.Date(CurYear, m, 1, 0, 0, 0, 0, time.Local).Unix())
EndTimeStamp = int(time.Date(CurYear, m+1, 1, 0, 0, 0, -1, time.Local).Unix())
return
}