Calculate number of days between two dates?

How can I calculate the number of days between two dates? In the code below I should get the number of hours, which means that I should only need to divide by 24. However, the result I get is something like -44929.000000 . I’m only looking a day or two back so I would expect 24 or 48 hours.

package main

import (

func main() {
    timeFormat := "2006-01-02"

    t, _ := time.Parse(timeFormat, "2014-12-28")
    //  duration := time.Since(t)
    duration := time.Now().Sub(t)
    fmt.Printf("%f", duration.Hours())

Here’s the executable Go code:

1 Like

Your program seems to work as intended. I’m getting 45.55 hours. Have you tried to run it locally?

Playground time is fixed, time.Now() will give you 2009-11-10 23:00:00 +0000 UTC always.

Refer: calculate-number-of-days-between-two-dates