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