Skip to content
Home » Program on ParseDuration() function in Golang

Program on ParseDuration() function in Golang

Learn about Program on ParseDuration() function in Golang in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

Program on ParseDuration() function in Golang

// Golang program to demonstrate the
// ParseDuration() function

package main

import "fmt"
import "time"

func main() {
	time1, _ := time.ParseDuration("20h")
	time2, _ := time.ParseDuration("2h15m17s")
	time3, _ := time.ParseDuration("10µs")

	fmt.Printf("Total Minutes in time1: %f\n", time1.Minutes())
	fmt.Printf("Total Seconds in time2: %f\n", time2.Seconds())
	fmt.Printf("Total Nanoseconds in time3: %d\n", time3.Nanoseconds())
}

In the above program, we imported the fmt package, which contains the files of the fmt package, and then we can use a function related to the fmt package. Here, we created three-time objects using ParseDuration() function by specifying time value. After that, we printed Minutes, Seconds, Nanoseconds values corresponding to time objects

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
Program to get the date, month, year using Date() function in Golang
Program to print current date-time using Format() function in Golang