1 Star 0 Fork 0

小鱼儿小董子/dongli-platform

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
times.go 3.49 KB
一键复制 编辑 原始数据 按行查看 历史
小鱼儿小董子 提交于 2025-01-26 13:21 +08:00 . 1111
package tools
import (
"time"
)
func NowDateYMHSIsBetween(start, end string) bool {
return NowDateYMHSIsBetweenTime(time.Now(), start, end)
}
func NowDateYMHSIsBetweenTime(t time.Time, start, end string) bool {
return DateYMHSIsBetweenTime(t, start, end)
}
func DateYMHSIsBetween(start, end string) bool {
return DateYMHSIsBetweenTime(time.Now(), start, end)
}
func DateYMHSIsBetweenTime(t time.Time, start, end string) bool {
now := t.Format("2006-01-02 15:04")
return now >= start && now <= end
}
// TimestampDurationDayWithAnchor 计算从start时间开始到end时间结束经过的天数
// start: 开始时间戳
// end: 结束时间戳
// anchor (int): 锚点小时,格式为 5(凌晨5点为一天) 或 6(凌晨6点为一天)
// return: 经过的天数
func TimestampDurationDayWithAnchor(start, end int64, anchorHour int32) int32 {
if start >= end {
return 0
}
startTime := time.Unix(start, 0)
endTime := time.Unix(end, 0)
// 计算锚点时间
anchorTime := time.Date(startTime.Year(), startTime.Month(), startTime.Day(), int(anchorHour), 0, 0, 0, startTime.Location())
// 计算天数
days := int32(endTime.Sub(anchorTime).Hours() / 24)
// 计算锚点时间是否超过结束时间
if anchorTime.AddDate(0, 0, int(days)).After(endTime) {
days--
}
return days
}
// TimestampDurationDay 计算从start时间开始到end时间结束经过的天数(过24:00:00算一天)
func TimestampDurationDay(start, end int64) int32 {
return TimestampDurationDayWithAnchor(start, end, 0)
}
// GetTimeWithHourAnchor 计算当前时间指定天数的指定时间
// anchor (int): 锚点小时,格式为 5
// days (int): 指定天数
// return: 指定时间
func GetTimeWithHourAnchor(anchorHour int32, days int32) time.Time {
now := time.Now()
anchorTime := time.Date(now.Year(), now.Month(), now.Day(), int(anchorHour), 0, 0, 0, now.Location())
return anchorTime.AddDate(0, 0, int(days))
}
// todo 获取本周一的时间
func GetFirstDateOfWeek(now time.Time) (weekMonday time.Time) {
offset := int(time.Monday - now.Weekday())
if offset > 0 {
offset = -6
}
weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
return weekStartDate
}
func DateToTm(date string) int64 {
var cstSh, _ = time.LoadLocation("Asia/Shanghai")
tm, _ := time.ParseInLocation("2006-01-02 15:04:05", date, cstSh)
return tm.Unix()
}
// 获取本月开始结束时间
func NowMonthStartEndTime() (startTime, endTime time.Time) {
now := time.Now()
// 获取本月的第一天
firstOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
// 获取本月的最后一天
lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
//fmt.Println("本月的开始日期:", firstOfMonth.Format("2006-01-02"))
//fmt.Println("本月的结束日期:", lastOfMonth.Format("2006-01-02"))
return firstOfMonth, lastOfMonth
}
// 获取本月开始结束时间时分秒 2024-07-01 00:00:00 - 2024-07-31 23:59:59
func NowMonthStartEndTimeUnix() (startTime, endTime int64) {
now := time.Now()
// 获取本月的第一天
firstOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
// 获取本月的最后一天
lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
startTime = firstOfMonth.Unix()
endTime = lastOfMonth.Unix() + int64(24*3600-1)
//fmt.Println("本月的开始日期:", time.Unix(startTime, 0).Format("2006-01-02 15:04:05"))
//fmt.Println("本月的结束日期:", time.Unix(endTime, 0).Format("2006-01-02 15:04:05"))
return startTime, endTime
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wanjimao/dongli-platform.git
git@gitee.com:wanjimao/dongli-platform.git
wanjimao
dongli-platform
dongli-platform
v0.0.59

搜索帮助