DEV Community

loading...

Discussion on: Get the number of days between two dates in Go

Collapse
sergivb01 profile image
sergi
func daysBetween(a, b time.Time) int {
    if a.After(b) {
        a, b = b, a
    }
    return int(b.Sub(a).Hours() / 24)
}
Collapse
samwho profile image
Sam Rose Author

Not quite as robust as I'd like. Not all days have 24 hours in them. 😔

Collapse
alexyslozada profile image
Alexys Lozada
func daysBetween(a, b time.Time) float64 {
    if a.After(b) {
        a, b = b, a
    }
    return math.Ceil(b.Sub(a).Hours() / 24.0)
}