package utils import ( "fmt" "strconv" "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) var endTime time.Time if len(endDate) > 10 { endTime, _ = time.Parse("2006-01-02 15:04:05", endDate) } else { endTime, _ = time.Parse("2006-01-02", endDate) } for currTime := startTime; !currTime.After(endTime); currTime = currTime.AddDate(0, 0, 1) { // 设置时间为当天的开始时间 localLocation, _ := time.LoadLocation("Local") //设置时区 midnight := time.Date(currTime.Year(), currTime.Month(), currTime.Day(), 0, 0, 0, 0, localLocation).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("2006-01-02") days[midnight] = []int{} } return days } func GetTimeDayDateFormat(startDate string, endDate string) []string { days := make([]string, 0) startTime, _ := time.Parse("2006-01-02", startDate) var endTime time.Time if len(endDate) > 10 { endTime, _ = time.Parse("2006-01-02 15:04:05", endDate) } else { 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("2006-01-02") days = append(days, midnight) } return days } // 时间戳转化为 00:02:37 格式 func TimeStampToMDS(timestamp int) string { hours := timestamp / (60 * 24) remainingMinutes := timestamp % (60 * 24) minutes := remainingMinutes / 60 remainingSeconds := timestamp % 60 return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, remainingSeconds) } // 比较日期 , func CompareDates(dateA, dateB string) (bool, string) { tA, errA := time.Parse("2006-01-02", dateA) tB, errB := time.Parse("2006-01-02", dateB) if errA != nil || errB != nil { return false, "" } diff := int(tA.Sub(tB).Hours() / 24) valuableDiff := []int{1, 2, 3, 4, 5, 6, 7, 14, 30} if InArray(diff, valuableDiff) { return true, "+" + strconv.Itoa(diff) + "day" } else { return false, "" } }