1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- 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)
- }
- func Last7Days() (time.Time, time.Time) {
- today := Today()
- return today.AddDate(0, 0, -6), today.Add(time.Hour * 23).Add(time.Minute * 59).Add(time.Second * 59)
- }
- func LastWeek() (time.Time, time.Time) {
- start := Today().AddDate(0, 0, -7)
- end := start.AddDate(0, 0, 6).Add(time.Hour * 23).Add(time.Minute * 59).Add(time.Second * 59)
- return start, end
- }
- func ThisWeek() (time.Time, time.Time) {
- start := Today().Round(0).Local().AddDate(0, 0, 0-int(time.Now().Weekday())+1) // 0是本周的第一天,+1是因为AddDate是相对于当前时间的
- end := start.AddDate(0, 0, 6).Add(time.Hour * 23).Add(time.Minute * 59).Add(time.Second * 59)
- return start, end
- }
- func NextWeek() (time.Time, time.Time) {
- _, thisEnd := ThisWeek()
- start := thisEnd.AddDate(0, 0, 1)
- end := start.AddDate(0, 0, 6).Add(time.Hour * 23).Add(time.Minute * 59).Add(time.Second * 59)
- 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) {
- now := time.Now()
- year, month, _ := now.Date()
- nextMonth := month + 1
- if nextMonth > 12 {
- nextMonth = 1
- year++
- }
- startOfNextMonth := time.Date(year, nextMonth, 1, 0, 0, 0, 0, now.Location())
- endOfNextMonth := startOfNextMonth.AddDate(0, 1, -1).Add(time.Hour*23 + time.Minute*59 + time.Second*59)
- return startOfNextMonth, endOfNextMonth
- }
|