feat: timer 包新增 GetCurrWeekDate 和 GetLastWeekDate 函数

This commit is contained in:
kercylan98 2024-01-31 11:46:32 +08:00
parent c95b206592
commit ad4777a379
1 changed files with 28 additions and 0 deletions

View File

@ -15,6 +15,34 @@ const (
Week = Day * 7 Week = Day * 7
) )
// GetCurrWeekDate 获取以 week 为周一的本周 week 时间
func GetCurrWeekDate(now time.Time, week time.Weekday) time.Time {
w := int(week)
monday := GetMondayZero(now)
var curr time.Time
if WeekDay(now) > w {
curr = monday.AddDate(0, 0, w-1)
} else {
curr = monday.AddDate(0, 0, w-1)
curr = curr.AddDate(0, 0, -7)
}
return curr
}
// GetLastWeekDate 获取以 week 为周一的特定时间 now 的上周 week 时间
func GetLastWeekDate(now time.Time, week time.Weekday) time.Time {
w := int(week)
monday := GetMondayZero(now)
var last time.Time
if WeekDay(now) > w {
last = monday.AddDate(0, 0, -(7 - w + 1))
} else {
last = monday.AddDate(0, 0, -(7 - w + 1))
last = last.AddDate(0, 0, -7)
}
return last
}
// GetMonthDays 获取一个时间当月共有多少天 // GetMonthDays 获取一个时间当月共有多少天
func GetMonthDays(t time.Time) int { func GetMonthDays(t time.Time) int {
t = GetToday(t) t = GetToday(t)