package utils import ( "time" ) // 给出日期 获取其中每小时的开始时间戳 func GetDayHour(date time.Time) []int64 { date = time.Date(date.Year(), date.Month(), date.Day(), 0, 0, 0, 0, time.Local) var hours []int64 // 遍历这一天中的每一小时 for hour := 0; hour < 24; hour++ { // 设置小时并保留分钟和秒为零 t := date.Add(time.Duration(hour) * time.Hour) //fmt.Println(t.Format("2006-01-02 15:04:05")) hours = append(hours, t.Unix()) } return hours } // 给出开始和结束日期 获取其中每一天的开始时间戳 func GetTimeDay(startDate string, endDate string) []int64 { var days []int64 startTime, _ := time.Parse("2006-01-02", startDate) endTime, _ := time.Parse("2006-01-02", endDate) for currTime := startTime; !currTime.After(endTime); currTime = currTime.AddDate(0, 0, 1) { // 设置时间为当天的开始时间 midnight := time.Date(currTime.Year(), currTime.Month(), currTime.Day(), 0, 0, 0, 0, currTime.Location()).Unix() days = append(days, midnight) } return days } // 给出开始和结束日期 获取中间每一天的date func GetTimeDayDate(startDate string, endDate string) map[string][]int { days := make(map[string][]int) startTime, _ := time.Parse("2006-01-02", startDate) endTime, _ := time.Parse("2006-01-02", endDate) for currTime := startTime; !currTime.After(endTime); currTime = currTime.AddDate(0, 0, 1) { // 设置时间为当天的开始时间 midnight := time.Date(currTime.Year(), currTime.Month(), currTime.Day(), 0, 0, 0, 0, currTime.Location()).Format("20060102") days[midnight] = []int{} } return days }