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.
Code
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
Duration HumanizerTurn a number of seconds into a readable duration such as 1 day, 2 hours, 3 minutes, and show the equivalent in minutes and hours.ASCII Table ReferenceLook up any ASCII code point 0-127 by decimal, hex, or character and see the binary, octal, and control-name view.CSS Color Name LookupTranslate between CSS keyword color names and hex values with RGB and HSL conversions, exact name matching, and nearest-color suggestions.CSS Flexbox PlaygroundInteractively test CSS flexbox container and item properties with a live preview. Adjust justify-content, align-items, flex-direction, and flex-grow to see layout changes.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.