Menu Close

Go – How to convert time duration into seconds in Go

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

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

Function prototype:

func (d Duration) Seconds() float64

Input parameters:


Duration: type Duration int64

Return value:

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

Example with code:

package main

import (
        "fmt"
        "time"
)

func main() {

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

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

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

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

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

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

}

Output:

 Convert 2 hour 30 minutes to Seconds: 9000.00

 Convert 2 seconds to Seconds: 2.00

 Convert 30 minutes to Seconds: 1800.00

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

2 Comments

  1. zoritoler imol

    Hi there! I know this is kinda off topic however I’d figured I’d ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa? My blog covers a lot of the same topics as yours and I believe we could greatly benefit from each other. If you happen to be interested feel free to shoot me an e-mail. I look forward to hearing from you! Excellent blog by the way!

    • techieindoor

      Hi ,
      Thanks for contacting us. Currently, we want to focus on blogs only because It’s in an initial state. Definitely, we will connect with you after sometime.
      Thanks in advance 🙂

Leave a Reply

Your email address will not be published.

Contact Us