I'm trying to iterate through days between 2 dates. One date is time.Parse("2006-01-02", "2017-08-24")
another is time.Now()
but my loop seems to be stopping at the turn of the year and I can't figure out why.
func IsToday(date time.Time) bool {
today := time.Now()
return date.Year() == today.Year() &&
date.Month() == today.Month() &&
date.Day() == date.Day()
}
t1, _ := time.Parse("2006-01-02", "2017-08-24")
for curr := t1; !IsToday(curr); curr = curr.AddDate(0,0,1) {
fmt.Println(curr)
}
The loop prints out:
2017-08-24 00:00:00 +0000 UTC
2017-08-25 00:00:00 +0000 UTC
2017-08-26 00:00:00 +0000 UTC
2017-08-27 00:00:00 +0000 UTC
2017-08-28 00:00:00 +0000 UTC
2017-08-29 00:00:00 +0000 UTC
2017-08-30 00:00:00 +0000 UTC
2017-08-31 00:00:00 +0000 UTC
2017-09-01 00:00:00 +0000 UTC
2017-09-02 00:00:00 +0000 UTC
...
2017-12-18 00:00:00 +0000 UTC
2017-12-19 00:00:00 +0000 UTC
2017-12-20 00:00:00 +0000 UTC
2017-12-21 00:00:00 +0000 UTC
2017-12-22 00:00:00 +0000 UTC
2017-12-23 00:00:00 +0000 UTC
2017-12-24 00:00:00 +0000 UTC
2017-12-25 00:00:00 +0000 UTC
2017-12-26 00:00:00 +0000 UTC
2017-12-27 00:00:00 +0000 UTC
2017-12-28 00:00:00 +0000 UTC
2017-12-29 00:00:00 +0000 UTC
2017-12-30 00:00:00 +0000 UTC
2017-12-31 00:00:00 +0000 UTC
Notice how it does not iterate till today's date. Testing the AddDate individually as time.Parse("2006-01-02", "2017-12-31").AddDate(0,0,1)
does result in 2018-01-01
.