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.
UNIX TIMESTAMP INPUT
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. Nnow Ccopy /focus
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.
Why the layout string is 2006-01-02T15:04:05Z07:00
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 want | Write in layout | Prints for the reference |
|---|---|---|
| Four-digit year | 2006 | 2006 |
| Zero-padded month / day | 01 / 02 | 01 / 02 |
| 24-hour clock | 15:04:05 | 15:04:05 |
| Numeric zone | -07:00 | -07:00 |
| Numeric zone or Z at UTC | Z07:00 | -07:00 (Z only at UTC) |
| Fixed fractional digits | .000 | .000 |
| Trim trailing fractional zeros | .999999999 | empty for a whole second |
| Full RFC 3339 | time.RFC3339 | 2006-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.
time.Unix, UnixMilli and the unit trap
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 from | Call | Why |
|---|---|---|
time.Now().Unix() or a seconds column | time.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() value | time.UnixMilli(ms) | Milliseconds; Go 1.17+ also has time.UnixMicro(us) |
A time.Time you already hold | t.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.Unixas 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 aTimein Local location, not UTC. Call.UTC()before formatting or comparing text if that is what the API expects.
UTC, Local and the monotonic reading
- A
time.Timeis an instant plus a Location.t.In(time.UTC)andt.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 byt.Sub(u),time.Since(t)and comparisons, so elapsed time stays correct across a wall-clock adjustment.t.String()shows it as the trailingm=+…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, sot == t.In(otherLocation)can be false even though both represent the same instant. time.Durationis int64 nanoseconds — about 292 years of range.time.Unixtakes an int64 second count, so durations and timestamps do not share a ceiling.
Parsing, zero values and portability
- 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.RFC3339Nanotrims trailing zeros when formatting. For a zone-less layout useParseInLocationto choose a zone; otherwise Go assumes UTC. - Zero value:
var t time.Timeis0001-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
ofEpochSecondvsofEpochMilli, and the Bash date page covers GNU vs BSD command forms.
ANSWERS
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.