Formatting time in Go
Go's layout is an example time, not percent codes.
func formats() ([]string, error) {
stamp := time.Date(2026, time.July, 30, 9, 5, 0, 0, time.UTC)
out := []string{
stamp.Format(time.RFC3339),
stamp.Format("2006-01-02"),
stamp.Format("Mon 02 Jan 15:04"),
stamp.Format(time.Kitchen),
}
parsed, err := time.Parse("2006-01-02", "2026-08-01")
if err != nil {
return nil, fmt.Errorf("parse: %w", err)
}
out = append(out, parsed.Weekday().String())
return out, nil
}
How it works
- The reference layout is 2006-01-02 15:04:05.
time.RFC3339and friends are the named layouts.Parsereturns an error, and it is easy to get wrong.
Keywords and builtins used here
appenderrorfuncifreturnstring
The run, in numbers
- Lines
- 17
- Characters to type
- 411
- Tokens
- 127
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 76 seconds.
Step 1 of 2 in Time, step 4 of 26 in Standard library & project layout.