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.
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.
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.
- Confirm whether the API expects seconds or milliseconds.
- When turning a calendar value into an instant, keep the location information attached to the time.Time value instead of doing manual hour arithmetic.
- Keep UTC as the reference when comparing distributed logs.
- Do not infer the unit solely from digit count.
Common mistakes in Go (Golang)
- Passing milliseconds where an API expects seconds.
- Formatting a local time as if it were UTC.
- Dropping fractional seconds when the application requires millisecond precision.
- Using manual UTC offsets instead of the runtime's timezone-aware facilities.
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
Quick reference
Practical checks
- Define the timestamp unit in your JSON or database schema.
- Use UTC/RFC3339 for logs when a readable representation is needed.
- Do not manually add timezone offsets to Unix seconds.
- Test values around daylight-saving transitions if you parse local calendar times.
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.