Convert duration in nanoseconds to hours, minutes, seconds, nanoseconds

Hello, is there a fast way to convert a duration in nanoseconds to hh:mm:ss.fffffffff?


Hi! I think this is what you need:

	t := time.Unix(0, 1257567890000000000 //nanoseconds)
	elapsed := time.Since(t)

	fmt.Printf("Elapsed time: %s\n", elapsed)

taken from:

1 Like

You could also use time.Duration ( which is type based on int64 in nanoseconds. Then you can convert this to hours by dividing with time.Hour (constant for number of nanosecons on an hour). Then you have the hours you can subtract the hours * time.Hours from the duration and do the same thing with minutes. Lastly you want to convert the remaining seconds to a float and divide it by time.Seconds. Then you can format the output with fmt.Sprintf("%02d:%02:%02f", hours, minutes, seconds)

1 Like


Doesn’t time.Since(t) return the duration from then t was until now? If I convert 1257567890000000000 to a duration d := time.Duration(1257567890000000000) and prints it with fmt.Println(d) it will give 349324h24m50s

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.