Go

time.Format — The Reference Time

admin by @admin ADMIN
Jun 16, 2026
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Go's time formatting uses a reference date instead of strftime tokens: `Mon Jan 2 15:04:05 MST 2006` (which is 01/02 03:04:05PM '06 -0700, or 1/2/3/4/5/6/7 — mnemonic). Strange at first, instantly memorable.
Go
Raw
package main

import (
    "fmt"
    "time"
)

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

    // Common predefined layouts in the time package
    fmt.Println(now.Format(time.RFC3339))                 // 2025-03-12T14:25:00-05:00
    fmt.Println(now.Format(time.RFC3339Nano))             // ...with nanoseconds
    fmt.Println(now.Format(time.DateOnly))                // 2025-03-12 (Go 1.20+)
    fmt.Println(now.Format(time.TimeOnly))                // 14:25:00   (Go 1.20+)
    fmt.Println(now.Format(time.Kitchen))                 // 2:25PM

    // Custom — the reference date is THE format spec
    fmt.Println(now.Format("2006-01-02 15:04:05"))         // ISO local
    fmt.Println(now.UTC().Format("2006-01-02T15:04:05Z"))  // ISO UTC

    // Parse — same layout convention
    t, _ := time.Parse(time.RFC3339, "2025-03-12T14:25:00Z")
    fmt.Println(t)
}
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.