123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package utils
- import "time"
- // 获取今天的日期(零点零分零秒)
- func Today() time.Time {
- now := time.Now()
- return time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
- }
- // 获取明天的日期(零点零分零秒)
- func Tomorrow() time.Time {
- return Today().AddDate(0, 0, 1)
- }
- // 获取最近7天的日期范围
- func Last7Days() (time.Time, time.Time) {
- today := Today()
- return today.AddDate(0, 0, -6), today
- }
- // 获取上周的日期范围
- func LastWeek() (time.Time, time.Time) {
- start := Today().AddDate(0, 0, -7)
- end := start.AddDate(0, 0, 6)
- return start, end
- }
- // 获取本周的日期范围
- func ThisWeek() (time.Time, time.Time) {
- start := time.Now().Round(0).Local().AddDate(0, 0, 0-int(time.Now().Weekday())+1) // 0是本周的第一天,+1是因为AddDate是相对于当前时间的
- end := start.AddDate(0, 0, 6)
- return start, end
- }
- // 获取下周的日期范围
- func NextWeek() (time.Time, time.Time) {
- _, thisEnd := ThisWeek()
- start := thisEnd.AddDate(0, 0, 1)
- end := start.AddDate(0, 0, 6)
- return start, end
- }
- // 获取上月的日期范围
- func LastMonth() (time.Time, time.Time) {
- today := time.Now()
- year, month, _ := today.Date()
- start := time.Date(year, month-1, 1, 0, 0, 0, 0, today.Location())
- end := time.Date(year, month, 0, 23, 59, 59, 999999999, today.Location())
- return start, end
- }
- // 获取本月的日期范围
- func ThisMonth() (time.Time, time.Time) {
- today := time.Now()
- year, month, _ := today.Date()
- start := time.Date(year, month, 1, 0, 0, 0, 0, today.Location())
- end := time.Date(year, month+1, 0, 23, 59, 59, 999999999, today.Location())
- return start, end
- }
- // 获取下月的日期范围
- func NextMonth() (time.Time, time.Time) {
- start, end := ThisMonth()
- return end.AddDate(0, 0, 1), start.AddDate(0, 1, 0)
- }
|