获取两个日期相差几天

// 获取两个日期相差几天 time类型参数
func timeSubDays(t1, t2 time.Time) int {

if t1.Location().String() != t2.Location().String() {
	return -1
}
hours := t2.Sub(t1).Hours()+24

if hours <= 0 {
	return -1
}
// sub hours less than 24
if hours < 24 {
	// may same day
	t1y, t1m, t1d := t1.Date()
	t2y, t2m, t2d := t2.Date()
	isSameDay := (t1y == t2y && t1m == t2m && t1d == t2d)
	
	if isSameDay {
		
		return 0
	} else {
		return 1
	}
	
} else { // equal or more than 24
	
	if (hours/24)-float64(int(hours/24)) == 0 { // just 24's times
		return int(hours / 24)
	} else { // more than 24 hours
		return int(hours/24) + 1
	}
}

}

猜你喜欢

转载自blog.csdn.net/w15562397578/article/details/104970790