Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between two time.Time objects

Tags:

datetime

time

go

Very new to the 'Go'. Question might be basic one.

I have two time.Time objects and I want to get the difference between the two in terms of hours/minutes/seconds. Lets say:

t1 = 2016-09-09 19:09:16 +0530 IST t2 = 2016-09-09 19:09:16 +0530 IST 

In above case, since the difference is 0. It should give me 00:00:00. Consider another case:

t1 = 2016-09-14 14:12:48 +0530 IST t2 = 2016-09-14 14:18:29 +0530 IST 

In this case, difference would be 00:05:41. I looked at the https://godoc.org/time but could not make anything out of it.

like image 281
Hemant Bhargava Avatar asked Oct 26 '16 11:10

Hemant Bhargava


People also ask

How do I find the difference between two datetime objects?

timedelta() method. To find the difference between two dates in Python, one can use the timedelta class which is present in the datetime library. The timedelta class stores the difference between two datetime objects.

How do you find the difference between two time objects in Python?

Subtract the end time from the start time To get the difference between two-time, subtract time1 from time2. A result is a timedelta object. The timedelta represents a duration which is the difference between two-time to the microsecond resolution.

How can you find the difference in time between two datetime objects time_1 and time_2?

Subtracting the later time from the first time difference = later_time - first_time creates a datetime object that only holds the difference.

What is the difference between time and datetime in Python?

Answer #1: the time module is principally for working with unix time stamps; expressed as a floating point number taken to be seconds since the unix epoch. the datetime module can support many of the same operations, but provides a more object oriented set of types, and also has some limited support for time zones.


1 Answers

You may use Time.Sub() to get the difference between the 2 time.Time values, result will be a value of time.Duration.

When printed, a time.Duration formats itself "intelligently":

t1 := time.Now() t2 := t1.Add(time.Second * 341)  fmt.Println(t1) fmt.Println(t2)  diff := t2.Sub(t1) fmt.Println(diff) 

Output:

2009-11-10 23:00:00 +0000 UTC 2009-11-10 23:05:41 +0000 UTC 5m41s 

If you want the time format HH:mm:ss, you may constuct a time.Time value and use its Time.Format() method like this:

out := time.Time{}.Add(diff) fmt.Println(out.Format("15:04:05")) 

Output:

00:05:41 

Try the examples on the Go Playground.

Of course this will only work if the time difference is less than a day. If the difference may be bigger, then it's another story. The result must include days, months and years. Complexity increases significnatly. See this question for details:

golang time.Since() with months and years

The solution presented there solves this issue by showing a function with signature:

func diff(a, b time.Time) (year, month, day, hour, min, sec int) 

You may use that even if your times are within 24 hours (in which case year, month and day will be 0).

like image 134
icza Avatar answered Sep 22 '22 13:09

icza