Check if two time objects are on the same Date in Go
You can truncate to days too, by using:
t.Truncate(time.Hour * 24)
It's inefficient to parse the time for the date components three times by making separate method calls for the year, month, and day. Use a single method call for all three date components. From my benchmarks, it's nearly three times faster. For example,
import "time"
func DateEqual(date1, date2 time.Time) bool {
y1, m1, d1 := date1.Date()
y2, m2, d2 := date2.Date()
return y1 == y2 && m1 == m2 && d1 == d2
}
if a.Day() == b.Day() && a.Month() == b.Month() && a.Year() == b.Year() {
// same date
}