Post

GoLangGoLang

n
naseer mohammad

Posted on 24th April 2024|77 views

0
votes

Golang Time Difference

How can I get the difference between two time objects?

In my code, I have two time.Time objects and I want to calculate their difference here are the two terms

t1 = 2017-09-08 20:09:18 +0520 IST 

t2 = 2017-09-08 20:09:18 +0520 IST

In this case, the difference would be 00:00:00, but this is not showing the result in that format why?

 

Answers
P
Anthony A

Posted on 24th April 2024

So if you want the result in the time format like HH:mm:ss you just have to construct a time.Time value and utilize its Time.Format() like this

out := time.Time{}.Add(diff) 

fmt.Println(out.Format("15:04:05"))

This will give you the result as you desire.

 

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos