我在日期比较时遇到问题。
代码:
func inTimeSpan(start, end, check time.Time) bool {
return check.After(start) && check.Before(end)
}
func checkTime(date string) { //Note that the date passed here is from SQL Server
date1 := time.Now()
date2, err := time.Parse("2006-01-02 15:04:05", date)
if err != nil {
log.Print(err)
return
}
date3 := now.Add(1 * time.Hour) // 1 hour later than now
log.Print("Date1 : ", date1.Format("2006-01-02 15:04:05"))
log.Print("Date2 : ", date2.Format("2006-01-02 15:04:05"))
log.Print("Date3 : ", date3.Format("2006-01-02 15:04:05"))
if inTimeSpan(date1, date3, date2) {
log.Print("Date2 is in the range between date1 and date3.")
} else {
log.Print("Date2 is not between date1 and date3.")
}
return
}
预期输出:
2019/12/15 19:31:02 Date1 : 2019-12-15 19:31:02
2019/12/15 19:31:02 Date2 : 2019-12-15 19:45:00
2019/12/15 19:31:02 Date3 : 2019-12-15 20:31:02
2019/12/15 19:31:02 Date2 is in the range between date1 and date3.
实际输出:
2019/12/15 19:31:02 Date1 : 2019-12-15 19:31:02
2019/12/15 19:31:02 Date2 : 2019-12-15 19:45:00
2019/12/15 19:31:02 Date3 : 2019-12-15 20:31:02
2019/12/15 19:31:02 Date2 is not between date1 and date3.
由于某种原因,日期比较按预期工作。有什么我想念的吗?
qq_花开花谢_0
相关分类