Skip to main content
Go supports time formatting and parsing via pattern-based layouts.

Formatting Time

Format a time according to RFC3339 using the corresponding layout constant:
t := time.Now()
fmt.Println(t.Format(time.RFC3339))
// Output: 2026-03-03T10:15:30Z

Parsing Time

Time parsing uses the same layout values as Format:
t1, _ := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")
fmt.Println(t1)
// Output: 2012-11-01 22:08:41 +0000 +0000

Custom Layouts

Format and Parse use example-based layouts. Layouts must use the reference time Mon Jan 2 15:04:05 MST 2006:
The example time must be exactly as shown: the year 2006, 15 for the hour, Monday for the day of the week, etc. This is how Go knows which part of the pattern represents which component.
fmt.Println(t.Format("3:04PM"))
// Output: 10:15AM

fmt.Println(t.Format("Mon Jan _2 15:04:05 2006"))
// Output: Tue Mar  3 10:15:30 2026

fmt.Println(t.Format("2006-01-02T15:04:05.999999-07:00"))
// Output: 2026-03-03T10:15:30.123456+00:00

Parsing Custom Layouts

form := "3 04 PM"
t2, _ := time.Parse(form, "8 41 PM")
fmt.Println(t2)
// Output: 0000-01-01 20:41:00 +0000 UTC

Numeric Formatting

For purely numeric representations, use standard string formatting with extracted components:
fmt.Printf("%d-%02d-%02dT%02d:%02d:%02d-00:00\n",
    t.Year(), t.Month(), t.Day(),
    t.Hour(), t.Minute(), t.Second())
// Output: 2026-03-03T10:15:30-00:00

Error Handling

Parse returns an error on malformed input:
_, err := time.Parse("Mon Jan _2 15:04:05 2006", "8:41PM")
fmt.Println(err)
// Output: parsing time "8:41PM" as "Mon Jan _2 15:04:05 2006": cannot parse "8:41PM" as "Mon"

Standard Layout Constants

2006-01-02T15:04:05Z07:00
02 Jan 06 15:04 MST
3:04PM
2006-01-02 15:04:05
2006-01-02
15:04:05

Layout Reference Time

The reference time used for layouts:
Mon Jan 2 15:04:05 MST 2006
Or numerically:
01/02 03:04:05PM '06 -0700
Remember: 01/02 03:04:05PM ‘06 -0700 (month/day hour:minute:second year timezone)

Common Layout Patterns

"2006-01-02"           // 2026-03-03
"01/02/2006"           // 03/03/2026
"Jan 2, 2006"          // Mar 3, 2026
"Monday, Jan 2, 2006"  // Tuesday, Mar 3, 2026

Key Functions

Format
func(layout string) string
Formats the time according to the layout
Parse
func(layout, value string) (Time, error)
Parses a formatted string and returns the time value
ParseInLocation
func(layout, value string, loc *Location) (Time, error)
Like Parse but in a specific time zone

Package Reference

Build docs developers (and LLMs) love