Reference Time Layout Format And Parse

Format and parse times through the reference-time layout Mon Jan 2, reusing constants like RFC3339 in both directions for lossless round trips.

GoData Integrity

Code

gosnippet
24 lines
package main

import (
	"fmt"
	"time"
)

func main() {
	start := time.Date(2026, 11, 14, 9, 30, 0, 0, time.UTC)

	// 布局以参考时间 Mon Jan 2 15:04:05 MST 2006 书写,数字形状即格式
	fmt.Println("rfc3339:", start.Format(time.RFC3339))
	fmt.Println("date only:", start.Format("2006-01-02"))
	fmt.Println("kitchen:", start.Format(time.Kitchen))

	// 解析与格式化互为逆操作:同一布局常量双向使用
	raw := "2026-11-14T09:30:00Z"
	parsed, err := time.Parse(time.RFC3339, raw)
	if err != nil {
		fmt.Println("parse error:", err)
		return
	}
	fmt.Println("round trip equal:", parsed.Equal(start))
}

What the code does

Reference Time Layout Format And Parse comes down to the function main.

Nothing falls through silently — 1 checkpoint test err != nil on the way through.

Applying it

The run emits 5 labeled sections — rfc3339:, date only:, kitchen:, parse error:, round trip equal: — one per step.

Statements that matter

if err != nil {
fmt.Println("parse error:", err)
return
}

Interactive Tools for This Topic

FAQ

What does Reference Time Layout Format And Parse print when run?
Invoke the file as-is — the `rfc3339:`, `date only:`, `kitchen:` prefixes identify which step printed each line.