golang 获取2个时间的月分
//获取2个时间的月分
func GetMoths(starttime, endttime time.Time) []string {
arr := []string{}
if starttime.Sub(endttime) > 0 {
cyears := starttime.Year()
cmonths := int(starttime.Month())
arr = append(arr, fmt.Sprintf("%d-%02d", cyears, cmonths))
return arr
}
years := starttime.Year()
months := int(starttime.Month())
yeare := endttime.Year() * 100000
monthe := int(endttime.Month())
//fmt.Println(years,months,yeare,monthe)
i := 0
for {
if years*100000+months <= yeare+monthe {
i = i + 1
arr = append(arr, fmt.Sprintf("%d-%02d", years, months))
months = months + 1
if months > 12 {
years = years + 1
months = 1
}
} else {
break
}
}
return arr
}