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
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM