Epoch Conversions With time.Unix

Build time instants from epoch seconds with time.Unix and split them back with Unix, UnixMilli, and UnixMicro for round trips.

GoConfig Management

Code

gosnippet
20 lines
package main

import (
	"fmt"
	"time"
)

func main() {
	// 秒 + 纳秒构造时间点:2026-11-18 00:00:00 UTC
	ts := time.Unix(1794960000, 0)
	fmt.Println("from epoch:", ts.UTC().Format(time.RFC3339))

	// 反向:time.Time 拆回三种精度的整数时间戳
	fmt.Println("seconds:", ts.UTC().Unix())
	fmt.Println("millis:", ts.UTC().UnixMilli())
	fmt.Println("micros:", ts.UTC().UnixMicro())

	// 往返一致
	fmt.Println("roundtrip ok:", time.Unix(ts.Unix(), 0).Equal(ts))
}

What the code does

Epoch Conversions With time.Unix keeps everything in the function main.

What to watch for

  • The runtime is the only requirement — no packages, no install.

Applying it

The main signature reads func main()

The file announces its steps in the output: from epoch:, seconds:, millis:.

Statements that matter

ts := time.Unix(1794960000, 0)

Interactive Tools for This Topic

FAQ

What does Epoch Conversions With time.Unix print when run?
No setup needed: run the file and look for labeled lines like `from epoch:`, `seconds:`, `millis:` in the output.