Parse and format using reference time
Parse and format using reference time
Go layouts describe how the reference instant Monday January 2, 2006 at 15:04:05, offset -0700, would be printed. Use 2006 for a four-digit year, 01 for a two-digit month and 02 for day. Parse converts text to Time; Format converts Time to text. Prefer RFC3339 for timestamp exchange.
package main
import ( "fmt" "time")
func check(e error) { if e != nil { panic(e) }}
func main() { t, e := time.Parse("2006-01-02", "2026-07-09") check(e) fmt.Println(t.Format("02/01/2006")) fmt.Println(t.Format(time.RFC3339))}In the absence of zone information Parse assumes UTC. Missing clock fields here become zero. The same layout tokens can be rearranged for display without changing the instant. A date-only string does not itself identify a local time zone. Avoid YYYY-MM-DD as a Go layout: those letters are not date tokens.
Expected output:
09/07/20262026-07-09T00:00:00ZValidate and preserve an offset
package main
import ( "fmt" "time")
func check(e error) { if e != nil { panic(e) }}
func main() { _, e := time.Parse("2006-01-02", "2026-02-30") fmt.Println(e != nil) t, e := time.Parse(time.RFC3339, "2026-07-09T14:30:00+03:30") check(e) fmt.Println(t.UTC().Format(time.RFC3339))}An impossible calendar date returns a parse error. The explicit +03:30 offset identifies the instant; converting to UTC changes its representation. Parsing is not a guarantee of canonical spelling (for example, fractional seconds may be accepted even when absent from a layout). Apply a separate round-trip or schema check if exact spelling is required.
Expected output:
true2026-07-09T11:00:00ZTry it locally
Parse 2026-11-27T08:15:00+02:00 and print RFC3339 in UTC.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Time and scheduling 02: Parse and format using reference time
Check the lesson objective before moving on.
Which Go layout parses a date such as 2026-07-09?
What should you do if Parse returns an error?