golang 时间方法

//获取date本周周几的日期
func getDateOfWeek(date time.Time, weekday time.Weekday) (time.Time) {
   var offset int
   switch weekday {
   case time.Sunday: //0
      offset = int(time.Sunday-date.Weekday()) + 7
   case time.Monday: //1
      offset = int(time.Monday - date.Weekday())
   case time.Tuesday: //2
      offset = int(time.Tuesday - date.Weekday())
   case time.Wednesday:
      offset = int(time.Wednesday - date.Weekday())
   case time.Thursday: //4
      offset = int(time.Thursday - date.Weekday())
   case time.Friday: //5
      offset = int(time.Friday - date.Weekday())
   case time.Saturday:
      offset = int(time.Saturday - date.Weekday())
   }

   weekStartDate := time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
   return weekStartDate
}

//获取date本周前n周周一的日期
func getLastWeekFirstDate(date time.Time, preWeekNums int) (time.Time) {
   thisWeekMonday := getDateOfWeek(date, time.Monday)

   lastWeekMonday := thisWeekMonday.AddDate(0, 0, -7*preWeekNums)

   return lastWeekMonday
}

//获取date所在月第一天
func getMonthFirstDate(date time.Time) (time.Time) {
   return time.Date(date.Year(), date.Month(), 1, 0, 0, 0, 0, date.Location())
}

//获取date所在有最后一天
func getMonthEndDate(date time.Time) (time.Time) {
   return getMonthFirstDate(date).AddDate(0, 1, -1)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值