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.
Code
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
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.Dotenv File ParserParse .env file content into a JSON object, handling comments, export prefixes, single quotes, and double-quoted escape sequences.Duration ConverterConvert between human durations like 1w2d3h45m and total seconds, hours, and minutes with negative support.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.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.