Golang 获取指定时间当月的开始结束时间

func (s *Service) getLastMonthStartEnd() (int64, int64) {
   now := time.Now()
   env := g.Cfg().GetString("bonus.env")
   var start, end time.Time
   if env == "dev" { // 测试环境就查本月的
      start, end = helper.GetMonthStartEnd(now)
   } else {
      lastMonth := now.AddDate(0, -1, -now.Day()+1)
      start, end = helper.GetMonthStartEnd(lastMonth)
   }
   return start.Unix(), end.Unix()
}

// 获取指定时间所在月的开始 结束时间
func GetMonthStartEnd(t time.Time) (time.Time, time.Time) {
   monthStartDay := t.AddDate(0, 0, -t.Day()+1)
   monthStartTime := time.Date(monthStartDay.Year(), monthStartDay.Month(), monthStartDay.Day(), 0, 0, 0, 0, t.Location())
   monthEndDay := monthStartTime.AddDate(0, 1, -1)
   monthEndTime := time.Date(monthEndDay.Year(), monthEndDay.Month(), monthEndDay.Day(), 23, 59, 59, 0, t.Location())
   return monthStartTime, monthEndTime
}
原文地址:https://www.cnblogs.com/arvin-an/p/13716596.html