time.Parse, time.Time.Format
func Parse(layout, value string) (Time, error)
func (t Time) Format(layout string) string
const RFC3339 = "2006-01-02T15:04:05Z07:00"Parse a time from a string, or format a Time, using a layout that refers to the reference timestamp Mon Jan 2 15:04:05 MST 2006.
| Parameter | Description |
|---|---|
layout | A sample of the reference time, not a numeric pattern such as YYYY-MM-DD. Predefined layouts include time.RFC3339 and time.Kitchen. |
value | The text to parse. It must match the layout, including separators. |
Returns: Parse returns the Time and a parse error. When the layout has no time zone, the result is in UTC. Format returns the formatted string and does not return an error.
Errors
*time.ParseError— Returned when the value cannot be parsed as the layout. The error text quotes both the layout and the value.
- Parse("YYYY-MM-DD", "2026-11-21") fails. Parse("2006-01-02", "2026-11-21") succeeds and is reported as UTC.
- time.Kitchen is "3:04PM". Formatting 15:04 UTC therefore prints 3:04PM.
- Do not use time.Now() when you need a reproducible example or test.
package main
import (
"fmt"
"time"
)
func main() {
wrong, err := time.Parse("YYYY-MM-DD", "2026-11-21")
fmt.Printf("err=%v zero=%v\n", err, wrong.IsZero())
ok, err := time.Parse("2006-01-02", "2026-11-21")
fmt.Printf("%v %v loc=%s\n", ok, err, ok.Location())
t, err := time.Parse(time.RFC3339, "2026-11-21T15:04:05Z")
if err != nil {
panic(err)
}
fmt.Println(t.UTC().Format("2006-01-02 15:04:05"))
fmt.Println(t.Format(time.Kitchen))
}Expected output
err=parsing time "2026-11-21" as "YYYY-MM-DD": cannot parse "2026-11-21" as "YYYY-MM-DD" zero=true
2026-11-21 00:00:00 +0000 UTC <nil> loc=UTC
2026-11-21 15:04:05
3:04PMRelated: Official Time.Format layouts