Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get hours difference between two dates

Tags:

time

go

People also ask

How do I calculate hours between two dates in Excel?

Another simple technique to calculate the duration between two times in Excel is using the TEXT function: Calculate hours between two times: =TEXT(B2-A2, "h") Return hours and minutes between 2 times: =TEXT(B2-A2, "h:mm") Return hours, minutes and seconds between 2 times: =TEXT(B2-A2, "h:mm:ss")

How do I calculate the difference between two dates in Excel?

Just subtract one date from the other. For example if cell A2 has an invoice date in it of 1/1/2015 and cell B2 has a date paid of 1/30/2015, then you could enter use the formula =B2-A2 to get the number of days between the two dates, or 29.

How do you find the difference between two dates in hours and minutes?

get time() -startDate. gettime())/1000; Log. d("App","difference in hour is"+diff/1000/60/60); Mins = diff/1000/60; Seconds = diff/1000; Using this code I'm getting hours as a correct value.


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())
}