0PricingLogin
Go Academy · Lesson

Formatting and Parsing

Use the reference layout.

The Reference Layout

Go does not use codes like %Y. Instead it uses a single reference time as a template:

Mon Jan 2 15:04:05 MST 2006

You write the date in that exact layout to describe the format you want.

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Date(2026, 5, 30, 9, 5, 0, 0, time.UTC)
    fmt.Println(t.Format("2006-01-02"))
}

Why Those Numbers?

The reference values count up: 1=month, 2=day, 3=hour, 4=minute, 5=second, 6=year, 7=timezone. So 1 2 3 4 5 6 = Jan 2, 03:04:05, 2006.

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Date(2026, 5, 30, 14, 5, 9, 0, time.UTC)
    fmt.Println(t.Format("15:04:05"))
}

All lessons in this course

  1. Time and Duration
  2. Formatting and Parsing
  3. Timers and Tickers
  4. Time Zones
← Back to Go Academy