datetime - Difference between two time.Time objects -
very new 'go'. question might basic one.
i have 2 time.time objects , want difference between 2 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 difference 0. should give me 00:00:00. consider case:
t1 = 2016-09-14 14:12:48 +0530 ist t2 = 2016-09-14 14:18:29 +0530 ist in case, difference 00:05:41. looked @ https://godoc.org/time not make out of it.
you may use time.sub() difference between 2 time.time values, result value of time.duration.
when printed, time.duration formats "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 want time format hh:mm:ss, may constuct time.time value , use time.format() method this:
out := time.time{}.add(diff) fmt.println(out.format("15:04:05")) output:
00:05:41 try examples on go playground.
of course work if time difference less day. if difference may bigger, it's story. result must include days, months , years. complexity increases significnatly. see question details:
golang time.since() months , years
the solution presented there solves issue showing function signature:
func diff(a, b time.time) (year, month, day, hour, min, sec int) you may use if times within 24 hours (in case year, month , day 0).
Comments
Post a Comment