Go 1.27 reference · Errors, Context and Time
Go time.Duration, Parse and Format Type
Represent durations, measure elapsed time, and parse or format times with Go's reference layout (2006-01-02 15:04:05).
Go 1.27
Syntax
type Duration int64 func Parse(layout, value string) (Time, error) func (t Time) Format(layout string) string
Parameters
| Parameter | Description |
|---|---|
time.Second, Minute... |
Duration constants: 2*time.Second. |
ParseDuration("1h30m") |
Parse a duration string. |
layout |
Written using the reference time Mon Jan 2 15:04:05 MST 2006. |
time.RFC3339 |
Standard ISO-style layout. |
Examples
package main
import (
"fmt"
"time"
)
func main() {
d := 90 * time.Minute
fmt.Println(d, d.Hours())
p, _ := time.ParseDuration("1h15m30s")
fmt.Println(p.Minutes())
}
Output
1h30m0s 1.5 75.5
Run this on your computer — see “Get Started” for setup.
package main
import (
"fmt"
"time"
)
func main() {
t, err := time.Parse(time.RFC3339, "2026-01-15T10:30:00Z")
fmt.Println(err)
fmt.Println(t.Format("02 Jan 2006 15:04"), t.Weekday())
fmt.Println(t.Add(36 * time.Hour).Format(time.DateOnly))
}
Output
<nil> 15 Jan 2026 10:30 Thursday 2026-01-16
Run this on your computer — see “Get Started” for setup.
Related
context.WithTimeout and WithCancel· Derive a context that is cancelled after a timeout or on demand, to stop work and outbound calls that take too long.