HowtoGo
Home / How to Go / Format a date and time
How to Go

Format a date and time

Convert a time.Time into a formatted string using Go's unique layout reference date.

Go does not use %Y-%m-%d format codes. Instead, you format dates by showing Go how the specific reference time Mon Jan 2 15:04:05 MST 2006 should look.

Think of it sequentially as 1 2 3 4 5 6: Month (1), Day (2), Hour (3), Minute (4), Second (5), Year (6).

t := time.Now()
// Format by showing how to arrange the reference date: 
// Mon Jan 2 15:04:05 MST 2006
formatted := t.Format("2006-01-02")

For standard specifications like ISO 8601, the time package provides built-in constants so you don't have to write the layout string manually.

// The time package includes constants for standard formats
stamp := t.Format(time.RFC3339)

A minimal program

This program formats one fixed date three ways: two custom layouts and one of the package constants.

package main

import (
    "fmt"
    "time"
)

func main() {
    // Create a fixed date to demonstrate output
    t := time.Date(2026, time.August, 29, 14, 2, 11, 0, time.UTC)

    fmt.Println("Date only:", t.Format("2006-01-02"))
    fmt.Println("12-hour time:", t.Format("03:04 PM"))
    fmt.Println("RFC3339:", t.Format(time.RFC3339))
}

Run it and the three layouts print in order. The date is fixed, so you get the same output every time.

Terminal
$ go run main.go
Date only: 2026-08-29
12-hour time: 02:02 PM
RFC3339: 2026-08-29T14:02:11Z