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.
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(or01) — Month (January = 1)2(or02) — Day3(or03,15) — Hour (3 PM = 15:00)4(or04) — Minute5(or05) — Second6(or06,2006) — Year7(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
- Padding: Use
02for zero-padded,2for no padding,_2for space-padded - 12h vs 24h: Use
3or03for 12-hour,15for 24-hour - Timezone:
-0700for numeric offset,MSTfor abbreviation,Z0700for 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.