date_utils.go 326 B

12345678910111213
  1. package date
  2. import "time"
  3. // GetBeginOfTheWeek 计算给定日期所在周的周一日期
  4. func GetBeginOfTheWeek(t time.Time, beginFrom time.Weekday) time.Time {
  5. weekday := t.Weekday()
  6. daysToSubtract := int(weekday - beginFrom)
  7. if daysToSubtract < 0 {
  8. daysToSubtract += 7
  9. }
  10. return t.AddDate(0, 0, -daysToSubtract)
  11. }