//golang中的time详情
//https://blog.csdn.net/ma2595162349/article/details/112437771
package utils
import (
"fmt"
"time"
)
const (
TimeYMDHIS = "2006-01-02 15:04:05"
TimeYMD = "2006-01-02"
TimeHIS = "15:04:05"
AsiaShanghai = "Asia/Shanghai"
TimeFormat = "15:04:05"
DateFormat = "2006-01-02"
DateTimeFormat = "2006-01-02 15:04:05"
DateTimeTFormat = "2006-01-02T15:04:05"
DateTimeFormatEn = "2006-01-02 at 15:04:05"
)
func TimeTest() {
//返回初使时间 time类型
tm := time.Unix(0, 0) //tm: 1970-01-01 08:00:00 +0800 CST
fmt.Println("tm:", tm)
//返回当前时间string类型
fmt.Println(time.Now().Format(TimeYMDHIS)) //20221105073104
//当前时间戳
t1 := time.Now().Unix() //1667604664
fmt.Println(t1)
//时间戳转化为具体 字符串时间
fmt.Println(time.Unix(t1, 0).String()) //2020-03-26 00:04:37 +0800 CST
//基本格式化的字符串时间
fmt.Println(time.Now().String()) //2019-07-31 13:56:35.7766729 +0800 CST m=+0.005042501
// 时间戳 转 字符串 时间
fmt.Println(time.Now().Format(TimeYMDHIS)) //2022-11-05 07:31:04
fmt.Println(time.Now().Format(TimeYMD)) //2022-11-05
fmt.Println(time.Now().Format(TimeHIS)) //207:35:36
//获取第几周
year, week := time.Now().ISOWeek()
fmt.Println("week:", week) //week: 44
fmt.Println("year:", year) // year: 2022
}
// 时间戳转年月日 时分秒
func DateFormatYMDStr(timestamp int) string {
tm := time.Unix(int64(timestamp), 0)
return tm.Format("2006-01-02 15:04")
}
//时间戳转年月日
func DateFormatYmd(timestamp int) string {
tm := time.Unix(int64(timestamp), 0)
return tm.Format("2006-01-02")
}
// 获取日期的年月日
func DateYMDInt() (int, int, int) {
year, month, day := time.Now().Date()
return year, int(month), day
}
//获取当前格式化时间 "2006-01"
func DateFormatStr(fm string) string {
tm := time.Now()
return tm.Format(fm)
}
//获取年月日时分秒(字符串类型)
func DateNowFormatStr() string {
tm := time.Now()
return tm.Format("2006-01-02 15:04:05")
}
//时间戳
func DateUnix() int {
t := time.Now().Local().Unix()
return int(t)
}
//获取年月日时分秒(time类型)
func DateNowFormat() time.Time {
tm := time.Now()
return tm
}
//获取第几周
func DateWeek() int {
_, week := time.Now().ISOWeek()
return week
}
// 获取年月日
func DateYmdFormat() string {
tm := time.Now()
return tm.Format("2006-01-02")
}
//获取当天的时间范伟 Time类型 2020-08-18 00:00:00 +0800 CST 2020-08-18 23:59:59 +0800 CST
func GetCurrentTimestamp() (beginTime, endTime time.Time) {
t := time.Now()
timeStr := t.Format("2006-01-02")
beginTime, _ = time.ParseInLocation("2006-01-02", timeStr, time.Local)
endTimeTmp := beginTime.Unix() + 86399
endTime = time.Unix(endTimeTmp, 0)
return beginTime, endTime
}
//获取当天的时间范伟 int64类型 //1597680000 1597766399
func GetCurrentTimeInt64() (beginTime, endTime int64) {
t := time.Now()
timeStr := t.Format("2006-01-02")
beginTimeTmp, _ := time.ParseInLocation("2006-01-02", timeStr, time.Local)
beginTime = beginTimeTmp.Unix()
endTime = beginTime + 86399
return beginTime, endTime
}
//GetMonthStartAndEnd 获取月份的第一天和最后一天
/**
y := "2020"
m := "6"
result := GetMonthStartAndEnd(y,m)
fmt.Print(result)
**/
func GetMonthStartAndEnd(myYear string,myMonth string) (map[string]string) {
// 数字月份必须前置补零
if len(myMonth)==1 {
myMonth = "0"+myMonth
}
yInt,_ := strconv.Atoi(myYear)
timeLayout := "2006-01-02 15:04:05"
loc, _ := time.LoadLocation("Local")
theTime, _ := time.ParseInLocation(timeLayout, myYear+"-"+myMonth+"-01 00:00:00", loc)
newMonth := theTime.Month()
t1 := time.Date(yInt,newMonth, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
t2 := time.Date(yInt,newMonth+1, 0, 0, 0, 0, 0, time.Local).Format("2006-01-02")
result := map[string]string{"start":t1,"end":t2,}
return result
}
赋零值
nilTime:=time.Time{}//赋零值
waitConfirmRecordParam.DeletedAt = nilTime//此处即为零值
waitConfirmRecordParam.ConfirmTime = nilTime//此处即为零值
判断time.Time是否为零值
- 方法一
golang标准库里的IsZero方法,返回bool,判断time.Time是否为零值
注意格式IsZero报告t是否代表Time零值的时间点,January 1, year 1, 00:00:00 UTC。
func (Time) IsZero
func (t Time) IsZero() bool
t := time.Now()
if t.IsZero() {
tx = tx.Where("created_at = ?", waitConfirmRecord.CreatedAt)
}
- 方法二
nilTime := time.Time{}
//是否传入时间
if waitConfirmRecord.CreatedAt != nilTime {
tx = tx.Where("created_at = ?", waitConfirmRecord.CreatedAt)
}
获取一年前,一月前,一天前的时间
//五分钟之后
nt := time.Now().Add(time.Minute * 5)
//五分钟之前
nt := time.Now().Add(-time.Minute * 5)
nowTime := time.Now()
getTime := nowTime.AddDate(0, 0, -1) //年,月,日 获取一天前的时间
resTime := getTime.Format("2023-01-02 15:04:05+08") //获取的时间的格式
fmt.Println(resTime)
getTime = nowTime.AddDate(0, -1, 0) //年,月,日 获取一个月前的时间
resTime = getTime.Format("2023-01-02 15:04:05") //获取的时间的格式
fmt.Println(resTime)
getTime = nowTime.AddDate(-2, 0, 0) //年,月,日 获取两年前的时间
resTime = getTime.Format("20230102") //获取的时间的格式
fmt.Println(resTime)
计算时间差
start := time.Now()
... operation that takes 20 milliseconds ...
t := time.Now()
elapsed := t.Sub(start)
作者:admin 创建时间:2022-11-05 07:21
最后编辑:admin 更新时间:2024-12-22 19:32
最后编辑:admin 更新时间:2024-12-22 19:32