Menu Close

Go – How to convert time duration into Milliseconds in Go

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

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

Function prototype:

func (d Duration) Milliseconds() int64

Input parameters:


Duration: type Duration int64

Return value:

Milliseconds() function in time package returns the duration as an integer milliseconds count.

Example with code:

package main

import (
        "fmt"
        "time"
)

func main() {

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

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

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

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

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

        fmt.Printf("Convert 30 minutes to Milliseconds: %d\n", h.Milliseconds())

}

Output:

 Convert 2 hour 30 minutes to Milliseconds: 9000000

 Convert 2 seconds to Milliseconds: 2000

 Convert 30 minutes to Milliseconds: 1800000

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

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

References:

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

2 Comments

  1. www.xmc.pl

    The way you write make it very easy to read. And the design you use, wow. Thats a really decent combination. And I am wondering what is the name of the design you use?

Leave a Reply

Your email address will not be published. Required fields are marked *