⏱️ TimestampEasy

Home → Unix Timestamp in Go (Golang)

Unix Timestamp in Go (Golang)

Go’s time package provides direct Unix conversion methods, making it straightforward to move between time.Time values and numeric epoch representations.

Open TimestampEasy Converter

Working example

time.Time.Unix() returns seconds. Newer Go versions also expose UnixMilli for milliseconds. When an API expects UTC display, format a converted instant explicitly rather than assuming the local machine timezone.

now := time.Now() seconds := now.Unix() millis := now.UnixMilli() readable := time.Unix(seconds, 0).UTC()

Unit and timezone checks

In Go, the cleanest context is a time.Time instant plus the exact Unix unit used only at the serialization boundary. The time package already separates calendar operations from Unix conversion.

Common mistakes in Go (Golang)

When to use Unix timestamps

Go timestamp values are common in services, telemetry, event records, and JSON APIs; a stable unit contract makes these boundaries easier to test.

Related Timestamp Tools

Unix Timestamps in Go: Seconds, Milliseconds, and time.Time

Go's time.Time type provides direct Unix conversion methods, which makes it easy to keep timestamp arithmetic tied to an explicit unit. Use Unix() for whole seconds and UnixMilli() when millisecond precision is required. The reverse conversion can use time.Unix with seconds and nanoseconds, or the corresponding convenience methods available in the Go version you target.

Keep the time instant separate from display

A useful Go pattern is to keep a time.Time value internally, serialize it as Unix seconds or milliseconds only at the API/database boundary, and format it as RFC3339/UTC when humans need to inspect it. This keeps timezone and formatting decisions out of the core business logic.

Example code

now := time.Now() seconds := now.Unix() millis := now.UnixMilli() restored := time.Unix(seconds, 0).UTC() fmt.Println(restored.Format(time.RFC3339))

Quick reference

Practical checks

Frequently Asked Questions: Practical Details

What does time.Now().Unix() return?

Unix seconds for the represented instant.

How do I get Unix milliseconds in Go?

Use the millisecond Unix method available in modern Go versions, or derive milliseconds explicitly when targeting an older runtime.

Why use time.Time instead of storing only integers?

time.Time carries useful calendar and timezone semantics while integer timestamps are compact at system boundaries.

Frequently Asked Questions

How does Go (Golang) represent the current Unix time?

Use the time.Time facilities shown on this page and confirm the returned unit in the language documentation.

When should Go code use UnixMilli instead of Unix?

Use Unix for whole-second interfaces and UnixMilli when the receiving system needs millisecond precision. Keep that choice explicit at the serialization boundary.

How should Go services handle timezone conversions?

For Go services that share events across regions, compare instants in UTC and keep local-zone formatting at the display boundary.