golang 对datetime进行封装
封装时间相关的操作
1.func AddSecond(t time.Time, second int64) time.Time
在指定时间上增加秒
示例
t1 := time.Now()
t2 := AddSecond(t1, 3)
fmt.Println(t1)
fmt.Println(t2)
结果
2022-05-14 18:35:02.609778 +0800 CST m=+0.001242595
2022-05-14 18:35:05.609778 +0800 CST m=+3.001242595
2.func AddMinute(t time.Time, minute int64) time.Time
在指定时间上增加分
示例
t1 := time.Now()
t2 := AddMinute(t1, 3)
fmt.Println(t1)
fmt.Println(t2)
结果
2022-05-14 18:36:40.803252 +0800 CST m=+0.001042905
2022-05-14 18:39:40.803252 +0800 CST m=+180.001042905
3.func AddHour(t time.Time, hour int64) time.Time
在指定时间上增加时
示例
t1 := time.Now()
t2 := AddHour(t1, 3)
fmt.Println(t1)
fmt.Println(t2)
结果
2022-05-14 18:37:34.4036 +0800 CST m=+0.001190035
2022-05-14 21:37:34.4036 +0800 CST m=+10800.001190035
4.func AddDay(t time.Time, day int64) time.Time
在指定时间上增加天
示例
t1 := time.Now()
t2 := AddDay(t1, 3)
fmt.Println(t1)
fmt.Println(t2)
结果
2022-05-14 18:38:41.302278 +0800 CST m=+0.001035704
2022-05-17 18:38:41.302278 +0800 CST
5.func AddMonth(t time.Time, month int64) time.Time
在指定时间上增加月
示例
t1 := time.Now()
t2 := AddMonth(t1, 3)
fmt.Println(t1)
fmt.Println(t2)
结果
2022-05-14 18:39:07.647648 +0800 CST m=+0.000863768
2022-08-14 18:39:07.647648 +0800 CST
6.func AddYear(t time.Time, year int64) time.Time
在指定时间上增加年
示例
t1 := time.Now()
t2 := AddYear(t1, 3)
fmt.Println(t1)
fmt.Println(t2)
结果
2022-05-14 18:39:55.254627 +0800 CST m=+0.001446201
2025-05-14 18:39:55.254627 +0800 CST
7.func GetNowDate() string
获取当前日期,格式为:yyyy-mm-dd
示例
t1 := GetNowDate()
fmt.Println(t1)
结果
2022-05-14
8.func GetNowTime() string
获取当前时间,格式为:hh-mm-ss
示例
t1 := GetNowTime()
fmt.Println(t1)
结果
18:41:22
9.func GetNowDateTime() string
获取当前日期时间,格式为:yyyy-mm-dd hh-mm-ss
示例
t1 := GetNowDateTime()
fmt.Println(t1)
结果
2022-05-14 18:41:45
10.func GetNowUnix() int64
获取当前时间戳
示例
t1 := GetNowUnix()
fmt.Println(t1)
结果
int64(1652524928)
11.func TimeToStr(t time.Time, format DateTimeFormatType) string
将指定时间装换成指定格式的字符串
示例
now := time.Now()
t1 := TimeToStr(now, YYYYMMDD_HHMMSS)
fmt.Println(t1)
t2 := TimeToStr(now, YYYY_MM_DD_HHMMSS)
fmt.Println(t2)
结果
2022/05/14 18:43:50
2022-05-14 18:43:50
12.func StrToTime(str string, format DateTimeFormatType) (time.Time, error)
将字符串格式的世界转换成指定格式的时间
示例
t1, err := StrToTime("2022-05-12 18", YYYY_MM_DD_HH)
fmt.Println(t1)
结果
2022-05-12 18:00:00 +0000 UTC
13.func BeginOfMinute(t time.Time) time.Time
获取当前日期时间的分钟的第一秒
示例
now := time.Now()
t1 := BeginOfMinute(now)
fmt.Println(now)
fmt.Println(t1)
结果
2022-05-14 18:54:12.981551 +0800 CST m=+0.001253133
2022-05-14 18:54:00 +0800 CST
14.func EndOfMinute(t time.Time) time.Time
获取当前日期时间的分钟的最后一秒
示例
now := time.Now()
t1 := EndOfMinute(now)
fmt.Println(now)
fmt.Println(t1)
结果
2022-05-14 18:55:03.583692 +0800 CST m=+0.001060298
2022-05-14 18:55:59.999999999 +0800 CST
15.func BeginOfHour(t time.Time) time.Time
获取当前日期时间的小时的第一秒
示例
now := time.Now()
t1 := BeginOfHour(now)
fmt.Println(now)
fmt.Println(t1)
结果
2022-05-14 18:55:52.200207 +0800 CST m=+0.001168724
2022-05-14 18:00:00 +0800 CST
16.func EndOfHour(t time.Time) time.Time
获取当前日期时间的小时的最后一秒
示例
now := time.Now()
t1 := EndOfHour(now)
fmt.Println(now)
fmt.Println(t1)
结果
2022-05-14 18:56:23.597624 +0800 CST m=+0.001134038
2022-05-14 18:59:59.999999999 +0800 CST
17.func BeginOfDay(t time.Time) time.Time
获取当前日期时间的天的第一秒
示例
now := time.Now()
t1 := BeginOfDay(now)
fmt.Println(now)
fmt.Println(t1)
结果
2022-05-14 18:56:52.93774 +0800 CST m=+0.001043605
2022-05-14 00:00:00 +0800 CST
18.func EndOfDay(t time.Time) time.Time
获取当前日期时间的天的最后一秒
示例
now := time.Now()
t1 := EndOfDay(now)
fmt.Println(now)
fmt.Println(t1)
结果
2022-05-14 18:57:18.299599 +0800 CST m=+0.000876310
2022-05-14 23:59:59.999999999 +0800 CST
19.func BeginOfWeek(t time.Time) time.Time
获取当前日期时间的星期的第一秒
示例
now := time.Now()
t1 := BeginOfWeek(now)
fmt.Println(now)
fmt.Println(t1)
结果
2022-05-14 18:57:51.776713 +0800 CST m=+0.001589796
2022-05-08 00:00:00 +0800 CST
20.func EndOfWeek(t time.Time) time.Time
获取当前日期时间的星期的最后一秒
示例
now := time.Now()
t1 := EndOfWeek(now)
fmt.Println(now)
fmt.Println(t1)
结果
2022-05-14 18:58:21.049562 +0800 CST m=+0.001055471
2022-05-14 23:59:59.999999999 +0800 CST
21.func BeginOfMonth(t time.Time) time.Time
获取当前日期时间的月的第一秒
示例
now := time.Now()
t1 := BeginOfMonth(now)
fmt.Println(now)
fmt.Println(t1)
结果
2022-05-14 18:58:56.572063 +0800 CST m=+0.001119684
2022-05-01 00:00:00 +0800 CST
22.func EndOfMonth(t time.Time) time.Time
获取当前日期时间的月的最后一秒
示例
now := time.Now()
t1 := EndOfMonth(now)
fmt.Println(now)
fmt.Println(t1)
结果
2022-05-14 18:59:22.822109 +0800 CST m=+0.001001976
2022-05-31 23:59:59.999999999 +0800 CST
23.func BeginOfYear(t time.Time) time.Time
获取当前日期时间的年的第一秒
示例
now := time.Now()
t1 := BeginOfYear(now)
fmt.Println(now)
fmt.Println(t1)
结果
2022-05-14 19:00:11.415559 +0800 CST m=+0.001288018
2022-01-01 00:00:00 +0800 CST
24.func EndOfYear(t time.Time) time.Time
获取当前日期时间的年的最后一秒
示例
now := time.Now()
t1 := EndOfYear(now)
fmt.Println(now)
fmt.Println(t1)
结果
2022-05-14 19:00:39.779115 +0800 CST m=+0.001094013
2022-12-31 23:59:59.999999999 +0800 CST
源代码: datetime