0Pricing
Go Academy · Lesson

Formatting and Parsing

Use the reference layout.

Formatting and Parsing is a free Go Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Reference Layout

Go does not use codes like %Y. Instead it uses a single reference time as a template:

Mon Jan 2 15:04:05 MST 2006

You write the date in that exact layout to describe the format you want.

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Date(2026, 5, 30, 9, 5, 0, 0, time.UTC)
    fmt.Println(t.Format("2006-01-02"))
}

Why Those Numbers?

The reference values count up: 1=month, 2=day, 3=hour, 4=minute, 5=second, 6=year, 7=timezone. So 1 2 3 4 5 6 = Jan 2, 03:04:05, 2006.

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Date(2026, 5, 30, 14, 5, 9, 0, time.UTC)
    fmt.Println(t.Format("15:04:05"))
}

Formatting a Date

Mix the layout pieces freely with literal text and separators to build any date string.

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Date(2026, 5, 30, 9, 0, 0, 0, time.UTC)
    fmt.Println(t.Format("02/01/2006"))
}

Named Components

Use Jan or January for month names and Mon or Monday for weekday names.

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Date(2026, 5, 30, 9, 0, 0, 0, time.UTC)
    fmt.Println(t.Format("Monday, January 2, 2006"))
}

12-Hour Clock and AM/PM

Use 3 for the 12-hour clock and PM for the meridiem indicator.

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Date(2026, 5, 30, 14, 30, 0, 0, time.UTC)
    fmt.Println(t.Format("3:04 PM"))
}

Built-in Layout Constants

The package ships ready-made layouts such as time.RFC3339 and time.Kitchen, so you do not have to memorize the digits.

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Date(2026, 5, 30, 9, 0, 0, 0, time.UTC)
    fmt.Println(t.Format(time.RFC3339))
}

Parsing a String

time.Parse(layout, value) turns text into a time.Time. The layout describes the shape of the input using the same reference time.

package main

import (
    "fmt"
    "time"
)

func main() {
    t, _ := time.Parse("2006-01-02", "2026-05-30")
    fmt.Println(t.Year(), t.Month())
}

Layout Must Match Input

If the layout does not match the input shape, Parse returns an error. Always check it.

package main

import (
    "fmt"
    "time"
)

func main() {
    _, err := time.Parse("2006-01-02", "30/05/2026")
    if err != nil {
        fmt.Println("parse failed")
    }
}

Parsing Times with Clocks

Include time fields in both layout and input to capture hours and minutes.

package main

import (
    "fmt"
    "time"
)

func main() {
    t, _ := time.Parse("2006-01-02 15:04", "2026-05-30 09:05")
    fmt.Println(t.Hour(), t.Minute())
}

Round Trip

Parse and Format are inverses. You can read a timestamp, work with it, then format it back out.

package main

import (
    "fmt"
    "time"
)

func main() {
    t, _ := time.Parse(time.RFC3339, "2026-05-30T09:00:00Z")
    fmt.Println(t.Format("Jan 2, 2006"))
}

Common Mistake: Wrong Reference

Beginners often write "2020-12-31" as a layout. That is wrong: the layout must use the reference values (2006, 01, 02), not your example date.

package main

import (
    "fmt"
    "time"
)

func main() {
    t := time.Date(2026, 5, 30, 0, 0, 0, 0, time.UTC)
    fmt.Println(t.Format("2006/01/02"))
}

Quick Check

Which string is Go's reference layout date?

Recap: Formatting and Parsing

You mastered time text:

  • Layouts use the reference time 2006-01-02 15:04:05.
  • Format produces strings; Parse reads them.
  • Use built-in constants like time.RFC3339 and always check parse errors.
package main

import (
    "fmt"
    "time"
)

func main() {
    t, _ := time.Parse("2006-01-02", "2026-05-30")
    fmt.Println(t.Format("Monday"))
}

Frequently asked questions

Is the “Formatting and Parsing” lesson free?

Yes — the full text of “Formatting and Parsing” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Formatting and Parsing”?

Use the reference layout. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Formatting and Parsing” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Time and Duration
  2. Formatting and Parsing
  3. Timers and Tickers
  4. Time Zones
← Back to Go Academy