//获取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) }
golang 时间方法
最新推荐文章于 2024-09-22 01:53:08 发布