Go time.Unix and epoch seconds

Go has one unusually strict rule for time: formatting is done with a reference date, not strftime tokens. Convert a number below, then read how time.Unix, UnixMilli, UTC and the monotonic clock decide what your program actually sees.

[MOD.CONV // INSTRUMENT]

UNIX TIMESTAMP INPUT

Auto-detects seconds, ms, µs, ns

Date output is floored to milliseconds. Choose a unit explicitly for older dates or values outside auto-detection.

Typical today: 10 (s), 13 (ms), 16 (µs), 19 (ns) digits. A unit heuristic, not a definition.

01 Local time —
02 UTC standard —
03 ISO 8601 —
04 Relative —
[SUB.REVERSE]
SECONDS (S) —
MILLISECONDS (MS) —

Local means your device timezone. During a repeated daylight-saving hour, the earlier occurrence is used; use UTC to choose an unambiguous instant.

Browser-side reference calculator. It does not execute Java, Go, PHP or shell commands; use the examples below in your own runtime.

[SEC.02 // THE REFERENCE LAYOUT]

Why the layout string is 2006-01-02T15:04:05Z07:00

NO STRFTIME IN GO

Go formats and parses time with a reference instant: Mon Jan 2 15:04:05 MST 2006 (Unix 1136239445). Every token in a layout is written as the value it should print for that one instant, so a layout doubles as a readable example. The mnemonic is the sequence 1 2 3 4 5 6 7 — month 1, day 2, hour 3 (15:00), minute 4, second 5, year 6, zone 7.

You wantWrite in layoutPrints for the reference
Four-digit year20062006
Zero-padded month / day01 / 0201 / 02
24-hour clock15:04:0515:04:05
Numeric zone-07:00-07:00
Numeric zone or Z at UTCZ07:00-07:00 (Z only at UTC)
Fixed fractional digits.000.000
Trim trailing fractional zeros.999999999empty for a whole second
Full RFC 3339time.RFC33392006-01-02T15:04:05-07:00

A C-style format like %Y-%m-%d is not an error in Go — time.Format copies unknown bytes verbatim, so you literally get %Y-%m-%d back. That silent output is the classic first-day Go bug. time.Parse takes the same layout; for text with no zone it assumes UTC, while time.ParseInLocation(layout, s, loc) interprets it in the zone you pass.

[SEC.03 // SECONDS IN AND OUT]

time.Unix, UnixMilli and the unit trap

PICK BY SOURCE UNIT

The constructors differ only in the unit they assume. Feeding one the other’s number never panics — you just get a date millennia away or one in January 1970:

The number came fromCallWhy
time.Now().Unix() or a seconds columntime.Unix(sec, 0)Seconds since 1970 plus a nanosecond adjustment; nsec outside 0–999,999,999 is normalized
time.Now().UnixMilli() or a JS Date.now() valuetime.UnixMilli(ms)Milliseconds; Go 1.17+ also has time.UnixMicro(us)
A time.Time you already holdt.Unix() / t.UnixMilli()Whole seconds / milliseconds; t.UnixNano() is an int64 and overflows outside 1678–2262
  • A year around 56000 means a millisecond value reached time.Unix as seconds — the number is 1,000× too large.
  • A date in January 1970 is the reverse: seconds handed to time.UnixMilli. The contemporary-date heuristic from the timestamp to date page suggests 10 digits for seconds and 13 for milliseconds; confirm the source unit, and the milliseconds to seconds page does the bare conversion.
  • time.Unix(sec, 0) returns a Time in Local location, not UTC. Call .UTC() before formatting or comparing text if that is what the API expects.
[SEC.04 // ZONES AND CLOCKS]

UTC, Local and the monotonic reading

TWO CLOCKS IN ONE VALUE
  • A time.Time is an instant plus a Location. t.In(time.UTC) and t.In(loc) only change the wall-clock reading; the instant is identical. time.LoadLocation("Asia/Shanghai") reads the host tzdata — in a scratch container import _ "time/tzdata" to embed it.
  • time.Now() carries a monotonic reading used by t.Sub(u), time.Since(t) and comparisons, so elapsed time stays correct across a wall-clock adjustment. t.String() shows it as the trailing m=+… part.
  • Serialization drops it. t.Format, JSON and the database round-trip only the wall clock; t.Round(0) or .UTC() also strips monotonic. Comparing a reconstructed value with == can fail even for the same instant.
  • Use t.Equal(u) for instants. The == operator also compares the Location pointer and the monotonic field, so t == t.In(otherLocation) can be false even though both represent the same instant.
  • time.Duration is int64 nanoseconds — about 292 years of range. time.Unix takes an int64 second count, so durations and timestamps do not share a ceiling.
[SEC.05 // PRACTICAL EDGES]

Parsing, zero values and portability

GO / JAVA / BASH
  • Parsing: time.Parse(time.RFC3339, s) expects an explicit zone but is not a strict RFC 3339 validator: some invalid offsets, such as +24:60, can be accepted and normalized. Validate the required syntax and offset bounds separately for strict input. Both RFC3339 layouts accept fractional seconds; time.RFC3339Nano trims trailing zeros when formatting. For a zone-less layout use ParseInLocation to choose a zone; otherwise Go assumes UTC.
  • Zero value: var t time.Time is 0001-01-01 00:00:00 UTC; t.IsZero() is the check. Writing that into a DB column as an epoch number produces a large negative value, not 0.
  • Same trap in other runtimes: the Java Instant page covers ofEpochSecond vs ofEpochMilli, and the Bash date page covers GNU vs BSD command forms.
[DOC.FAQ // INDEX]

ANSWERS

06 ITEMS
01 How do I get Unix seconds from a Go time.Time?

Call t.Unix() for seconds, t.UnixMilli() for milliseconds (Go 1.17+), t.UnixMicro() for microseconds or t.UnixNano() for nanoseconds. All four ignore the Location — a timestamp is an absolute count from 1970-01-01 UTC.

02 Should I use time.Unix or time.UnixMilli?

It depends on the unit you already have. time.Unix(sec, nsec) expects seconds plus a nanosecond adjustment, normalizing nsec values outside the usual sub-second range; time.UnixMilli(ms) expects milliseconds. A 13-digit millisecond value passed to time.Unix lands roughly 54,000 years in the future, with no error raised.

03 Why does Go use 2006-01-02 as a date format?

Go formats by example with the reference instant Mon Jan 2 15:04:05 MST 2006. Each token is the value it prints for that instant: 01 month, 02 day, 03 (15) hour, 04 minute, 05 second, 06 year, 07 zone. time.RFC3339 is the predefined 2006-01-02T15:04:05Z07:00 layout.

04 Does time.Unix return a UTC or local time?

It returns a Time in the Local location. The instant is correct either way, but t.Format and t.String() render local wall-clock text. Chain .UTC() when you need a UTC reading or stable serialized output.

05 Why does comparing two Go times with == give the wrong answer?

== compares the wall clock, the monotonic reading and the Location pointer. Parsed values have no monotonic reading and may carry different Location pointers, so equal instants can compare unequal. Use t.Equal(u), which compares instants only.

06 How do I parse a timestamp string without a timezone in Go?

time.Parse assumes UTC for zone-less text, which is rarely what a human meant. time.ParseInLocation("2006-01-02 15:04:05", s, loc) interprets it in the zone you supply. Passing a time.RFC3339 string to time.Parse keeps its explicit offset.

[SEC.MATRIX // RELATED]

Related tools

3 PAGES

Sources and specifications