# Time and Formatting — Go (Golang)

Source: https://www.skillbyai.com/en/go/s-time

> The reference date 2006-01-02 15:04:05.

## Layouts by example

The `time` package formats and parses with **layouts written as the reference time** Mon Jan 2 15:04:05 MST 2006 (1 2 3 4 5 6 7 in American order), so `"2006-01-02 15:04"` means year-month-day hour:minute. Predefined layouts include RFC3339. Durations are typed (`90 * time.Minute`), times carry a location (convert with `In`), and parse errors are explicit. Store and transmit times in UTC or RFC3339 with an offset, and convert to local time for display.

## Formatting, zones, durations and parse errors, run

I ran this with Go 1.27.1 (`go run .` in a module named demo, standard library only). The same instant formats as a date-time, RFC3339, and in Indian Standard Time (UTC+5:30); 90 minutes later is 10:35AM; month 13 fails to parse.

```go
package main

import (
	"fmt"
	"time"
)

func main() {
	t := time.Date(2026, time.October, 2, 9, 5, 0, 0, time.UTC)
	fmt.Println(t.Format("2006-01-02 15:04"))             // layout uses the reference time
	fmt.Println(t.Format(time.RFC3339))
	ist := time.FixedZone("IST", 5*3600+1800)
	fmt.Println(t.In(ist).Format("02 Jan 2006, 3:04 PM MST"))
	d := 90 * time.Minute
	fmt.Println(d, t.Add(d).Format(time.Kitchen))
	p, err := time.Parse("2006-01-02", "2026-13-01")
	fmt.Println(p.IsZero(), err)
}
```

Output:

```
2026-10-02 09:05
2026-10-02T09:05:00Z
02 Oct 2026, 2:35 PM IST
1h30m0s 10:35AM
true parsing time "2026-13-01": month out of range
```

## Use RFC3339 for APIs

time.RFC3339 includes the offset, so clients in any time zone interpret timestamps correctly.

**Quiz:** What does the layout "2006-01-02" mean in Go?

- [ ] The date the code was written
- [x] Year-month-day, written using the reference date
- [ ] A fixed date constant to display
- [ ] An invalid layout

*Answer:* Year-month-day, written using the reference date. Layouts use the reference time.
