Time Formatting / Parsing
Format and parse times with reference-time layouts, standard layouts such as RFC3339, and ParseInLocation.
Go time layouts use the example time Mon Jan 2 15:04:05 MST 2006 instead of format codes such as %Y. The numbers and words from that example show how the output should look.
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 read as UTC by time.Parse. UTC is the shared time standard with no local offset. Use time.ParseInLocation when the input represents local time in a known place. Check the returned error because invalid text does not produce a useful time.
Enjoyed this? Get more software craft delivered to your inbox.