fix(utils):调整月差值计算,使其可以使用负数表示前后顺序。

This commit is contained in:
徐涛 2022-08-20 12:09:51 +08:00
parent a2c39263d3
commit 52a83bd34e

View File

@ -14,16 +14,11 @@ func VeryEndOfDate(date time.Time) time.Time {
func DifferenceInMonth(t1, t2 time.Time) int { func DifferenceInMonth(t1, t2 time.Time) int {
var differYear, differMonth int var differYear, differMonth int
if t1.After(t2) { differYear = t1.Year() - t2.Year()
differYear = t1.Year() - t2.Year() differMonth = int(t1.Month() - t2.Month())
differMonth = int(t1.Month() - t2.Month())
} else {
differYear = t2.Year() - t1.Year()
differMonth = int(t2.Month() - t1.Month())
}
return differYear*12 + differMonth return differYear*12 + differMonth
} }
func IsNextMonth(t1, t2 time.Time) bool { func IsNextMonth(t1, t2 time.Time) bool {
return DifferenceInMonth(t1, t2) == 1 return DifferenceInMonth(t1, t2) == -1
} }