Go by Example

Time Formatting / Parsing

Format and parse times with reference-time layouts, standard layouts such as RFC3339, and ParseInLocation.

Go time layouts use the reference time Mon Jan 2 15:04:05 MST 2006 instead of format codes. Its component values are the patterns to write in a layout.

Use a layout with Format to produce text and the same layout with Parse to read it back. time.RFC3339 is a standard layout that includes a timezone offset.

package main
 
import (
    "fmt"
    "time"
)
 
func main() {
    now := time.Date(2026, 10, 6, 14, 30, 0, 0, time.UTC)
    text := now.Format(time.RFC3339)
    parsed, err := time.Parse(time.RFC3339, text)
    if err != nil {
        panic(err)
    }
    fmt.Println(text)
    fmt.Println(parsed)
}

In production

A layout without a timezone, such as 2006-01-02, is parsed as UTC by time.Parse. Use time.ParseInLocation when the input means local wall time in a known location. Keep offsets when an input represents an instant, and return parse errors instead of accepting the zero time.Time.

Enjoyed this? Get more software craft delivered to your inbox.