Menu Close

Go – How to convert time duration into hours in Go

Here, we are going to learnĀ about How to convert time duration into hours in Go. We will learn it by example and program.

Hours() method of time package will be used to convert time duration into hours in Go.

Function prototype:

func (d Duration) Hours() float64

Input parameters:


Duration: type Duration int64

Return value:

Hours() function in time package returns the duration as a floating point number of hours.

Example with code:

package main

import (
        "fmt"
        "time"
)

func main() {

        h, _ := time.ParseDuration("2h30m")

        fmt.Printf("Convert 2 hour 30 minutes to hours: %.2f\n", h.Hours())

        h, _ = time.ParseDuration("200s")

        fmt.Printf("Convert 200 seconds to hours: %.2f\n", h.Hours())

        h, _ = time.ParseDuration("30m")

        fmt.Printf("Convert 30 minutes to hours: %.2f\n", h.Hours())

}

Output:

 Convert 2 hour 30 minutes to hours: 2.50

 Convert 200 seconds to hours: 0.06

 Convert 30 minutes to hours: 0.50

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, time

Leave a Reply

Your email address will not be published.

Contact Us