How to get hours difference between two dates

Use time.Parse and time.Since:

package main

import (
    "fmt"
    "time"
)

const (
    // See http://golang.org/pkg/time/#Parse
    timeFormat = "2006-01-02 15:04 MST"
)

func main() {
    v := "2014-05-03 20:57 UTC"
    then, err := time.Parse(timeFormat, v)
    if err != nil {
        fmt.Println(err)
        return
    }
    duration := time.Since(then)
    fmt.Println(duration.Hours())
}

Have a look at the time package.

package main

import "fmt"
import "time"

func main() {
    a, err := time.Parse("2006-01-02 15:04 MST", "2014-05-03 20:57 UTC")
    if err != nil {
        // ...
        return
    }

    delta := time.Now().Sub(a)
    fmt.Println(delta.Hours())
}

Tags:

Time

Go