time.ParseDuration() Function in Golang With Examples
Go language provides inbuilt support for measuring and displaying time with the help of the time package. In this package, the calendrical calculations always assume a Gregorian calendar with no leap seconds. This package provides a ParseDuration() function which parses a duration string. Duration string is a signed sequence of decimal numbers with optional fraction and unit suffix, like “100ms”, “2.3h” or “4h35m”. To access ParseDuration() function you need to add a time package in your program with the help of the import keyword.
Note: Valid time units are “ns”, “us” (or “µs”), “ms”, “s”, “m”, “h”.
func ParseDuration(str string) (Duration, error)
Time Duration 1: 3h0m0s Time Duration 2: 5h30m40s
Time Duration 1: 5h0m0s Time Duration 2: 2h30m40s There are 9040 seconds in 2h30m40s. There are 3000 nanoseconds in 3µs. There are 3.00e-06 seconds in 3µs.