1 Star 0 Fork 0

szmaozi/go-utils

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
datetime.go 2.99 KB
Copy Edit Raw Blame History
szmaozi authored 2021-09-02 13:41 . wip: merge biob-v1
/*
* @Name:
* @Descripttion:
* @Warning:
* @version:
* @Author: moo
* @Date: 2021-01-06 14:22:20
* @LastEditors: moo
* @LastEditTime: 2021-05-28 15:16:07
*/
package utils
import (
"time"
)
const (
TIME_LAYOUT string = "2006-01-02 15:04:05"
TIME_DATE_LAYOUT string = "2006-01-02"
)
func CurrLocation() *time.Location {
now := time.Now()
location := now.Location()
return location
}
// 计算日期相差多少天
// 返回值day>0, t1晚于t2; day<0, t1早于t2
func GapDays(t1, t2 time.Time) int {
var i int
d := t2.Sub(t1)
i = int(d.Hours() / 24)
return i
}
func GapMonth(t1, t2 time.Time) (month int) {
y1 := t1.Year()
y2 := t2.Year()
m1 := int(t1.Month())
m2 := int(t2.Month())
d1 := t1.Day()
d2 := t2.Day()
yearInterval := y1 - y2
// 如果 d1的 月-日 小于 d2的 月-日 那么 yearInterval-- 这样就得到了相差的年数
if m1 < m2 || m1 == m2 && d1 < d2 {
yearInterval--
}
// 获取月数差值
monthInterval := (m1 + 12) - m2
if d1 < d2 {
monthInterval--
}
monthInterval %= 12
month = yearInterval*12 + monthInterval
return
}
/* 获取两个时间相差多少小时 */
func HourDiffer(start_time, end_time time.Time) int64 {
var hour int64
diff := end_time.Unix() - start_time.Unix()
hour = diff / 3600
return hour
}
/* 获取两个时间相差多少分钟 */
func MinuteDiffer(start_time, end_time time.Time) int64 {
var m int64
diff := end_time.Unix() - start_time.Unix()
m = diff / 60
return m
}
//获取传入的时间所在月份的第一天,即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。
func GetFirstDateOfMonth(d time.Time) time.Time {
d = d.AddDate(0, 0, -d.Day()+1)
return GetZeroTime(d)
}
//获取传入的时间所在月份的最后一天,即某月最后一天的0点。如传入time.Now(), 返回当前月份的最后一天0点时间。
func GetLastDateOfMonth(d time.Time) time.Time {
return GetFirstDateOfMonth(d).AddDate(0, 1, -1)
}
//获取某一天的0点时间
func GetZeroTime(d time.Time) time.Time {
return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
}
func GetLastTime(d time.Time) time.Time {
return time.Date(d.Year(), d.Month(), d.Day(), 23, 59, 59, 59, d.Location())
}
func TimeStrToTime(s string) time.Time {
now := time.Now()
locatoin := now.Location()
t, _ := time.ParseInLocation(TIME_LAYOUT, s, locatoin)
return t
}
func DateStrToTime(s string) time.Time {
now := time.Now()
locatoin := now.Location()
t, _ := time.ParseInLocation(TIME_DATE_LAYOUT, s, locatoin)
return t
}
func DateStrToZeroTime(s string) time.Time {
var sTime string = s
if len(s) > 10 {
sTime = s[:10]
}
t := DateStrToTime(sTime)
t = GetZeroTime(t)
return t
}
func DateStrToLastTime(s string) time.Time {
var sTime string = s
if len(s) > 10 {
sTime = s[:10]
}
t := DateStrToTime(sTime)
t = GetLastTime(t)
return t
}
func DateToStr(t time.Time) string {
s := t.Format(TIME_DATE_LAYOUT)
return s
}
func TimeToStr(t time.Time) string {
s := t.Format(TIME_LAYOUT)
return s
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/szmaozi/go-utils.git
git@gitee.com:szmaozi/go-utils.git
szmaozi
go-utils
go-utils
29e02a007caf

Search