Time Duration Formatting Layouts

Format timestamps with reference layouts and turn durations into human units, keeping all rendering deterministic across time zones.

GoData Integrity

Code

gosnippet
20 lines
package main

import (
	"fmt"
	"time"
)

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

	// Go 用参考时间 01/02 03:04:05PM '06 -0700 作为布局
	fmt.Println(ts.Format(time.RFC3339))
	fmt.Println(ts.Format("2006-01-02 15:04:05 MST"))
	fmt.Println(ts.Format("Monday, Jan 2"))

	elapsed := 64*time.Minute + 12*time.Second + 350*time.Millisecond
	fmt.Println("rounded:", elapsed.Round(time.Second))
	fmt.Println("minutes:", int(elapsed.Minutes()))
	fmt.Println("human:", elapsed.Truncate(time.Second))
}

What the code does

Time Duration Formatting Layouts comes down to the function main.

Applying it

Running the file prints labeled results such as rounded:, minutes:, human: — each operation’s effect is visible in the output.

Statements that matter

elapsed := 64*time.Minute + 12*time.Second + 350*time.Millisecond

Interactive Tools for This Topic

FAQ

What does Time Duration Formatting Layouts print when run?
Run it as a complete program — the output lines carry labels such as `rounded:`, `minutes:`, `human:`.