Standard library / time

time.ParseDuration()

Parse signed quantities with units ns, us (or µs), ms, s, m, h. Days and months are not duration units. Invalid syntax or overflow returns an error. Expected stdout: '1h30m0s\n'.

Formal Syntax & Structure

go syntaxgo
func ParseDuration(s string) (Duration, error)

Parameters & Attributes

NameTypeDescription
sstringDuration text containing numeric values and units.

Return Value

(Duration, error)

Parse signed quantities with units ns, us (or µs), ms, s, m, h.

Working Example

Example Snippetgo
package main
import (  "fmt"  "time")
func must(err error) {  if err != nil {    panic(err)  }}func main() { d, err := time.ParseDuration("1h30m"); must(err); fmt.Println(d) }
Go Reference