Time Duration Formatting Layouts
Format timestamps with reference layouts and turn durations into human units, keeping all rendering deterministic across time zones.
Code
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
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.Duration ConverterConvert between human durations like 1w2d3h45m and total seconds, hours, and minutes with negative support.ISO 8601 Duration ParserParse ISO 8601 durations like PT1H30M into year, month, week, day, and time components with exact or approximate total seconds.cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.
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:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.