Go time.Format and the Reference Time Approach

Understand Go's unique reference time approach to date formatting. Learn why 'Mon Jan 2 15:04:05 MST 2006' is the magic reference and how to write custom layouts.

Language-Specific

Detailed Explanation

Go's Reference Time Approach

Go takes a fundamentally different approach to date formatting compared to every other mainstream language. Instead of abstract tokens like YYYY or %Y, Go formats a specific reference time in the layout you want.

The Magic Reference Time

Mon Jan 2 15:04:05 MST 2006

Or equivalently:

01/02 03:04:05PM '06 -0700

Each component is a specific number chosen to be unique:

  • 1 (or 01) — Month (January = 1)
  • 2 (or 02) — Day
  • 3 (or 03, 15) — Hour (3 PM = 15:00)
  • 4 (or 04) — Minute
  • 5 (or 05) — Second
  • 6 (or 06, 2006) — Year
  • 7 (in -0700) — Timezone offset (MST = -0700)

Why This Approach?

The Go team designed this system so that the format string itself is a valid, readable date. When you see "2006-01-02", you immediately know the output will look like "2026-02-28" because the format IS a date. No lookup table required.

Common Layouts

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Now()

    fmt.Println(t.Format("2006-01-02"))                    // 2026-02-28
    fmt.Println(t.Format("2006-01-02 15:04:05"))           // 2026-02-28 14:30:00
    fmt.Println(t.Format("Mon, 02 Jan 2006 15:04:05 -0700")) // RFC 2822
    fmt.Println(t.Format("January 2, 2006"))               // February 28, 2026
    fmt.Println(t.Format("3:04 PM"))                       // 2:30 PM
    fmt.Println(t.Format(time.RFC3339))                    // 2026-02-28T14:30:00+09:00
}

Predefined Layouts

Go's time package includes predefined layout constants:

Constant Layout
time.Layout 01/02 03:04:05PM '06 -0700
time.ANSIC Mon Jan _2 15:04:05 2006
time.RFC822 02 Jan 06 15:04 MST
time.RFC1123 Mon, 02 Jan 2006 15:04:05 MST
time.RFC3339 2006-01-02T15:04:05Z07:00

Common Gotchas

  1. Padding: Use 02 for zero-padded, 2 for no padding, _2 for space-padded
  2. 12h vs 24h: Use 3 or 03 for 12-hour, 15 for 24-hour
  3. Timezone: -0700 for numeric offset, MST for abbreviation, Z0700 for Z-style UTC

Use Case

Go's time formatting is used in CLI tools, Kubernetes operator timestamps, REST API servers (Gin, Echo, Fiber), log formatting in cloud-native applications, Prometheus metric timestamps, Terraform provider date handling, and Docker container log timestamps. The reference time approach becomes intuitive after initial learning.

Try It — Date Format Reference & Tester

Open full tool