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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

type Duration int64   func Parse(layout, value string) (Time, error)   func (t Time) Format(layout string) string

Parameters

ParameterDescription
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

Example 1
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.

Parsing and formatting
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.

Was this page helpful?