Go Unix Timestamps Cheatsheet
Last updated May 2026
Go Timestamps Cheatsheet
Every timestamp operation you need. Print or save as PDF.
Get Current Timestamp
import "time" // Unix seconds (int64) time.Now().Unix() // → 1733529600 // Unix milliseconds (int64) time.Now().UnixMilli() // → 1733529600000 // Unix microseconds (int64) time.Now().UnixMicro() // Unix nanoseconds (int64) time.Now().UnixNano() // → 1733529600000000000 // time.Time object t := time.Now() t.UTC() // convert to UTC
Timestamp → time.Time
// Unix seconds → time.Time t := time.Unix(1733529600, 0) // Unix milliseconds → time.Time ms := int64(1733529600000) t := time.UnixMilli(ms) // Unix nanoseconds → time.Time ns := int64(1733529600000000000) t := time.Unix(0, ns) // Always convert to UTC for storage t.UTC() // Format as ISO 8601 t.UTC().Format(time.RFC3339) // → "2024-12-07T04:00:00Z"
time.Time → Timestamp
t := time.Now()
// → Unix seconds
t.Unix()
// → Unix milliseconds
t.UnixMilli()
// → Unix nanoseconds
t.UnixNano()
// String → time.Time → Unix
layout := "2006-01-02T15:04:05Z"
t, err := time.Parse(layout, "2024-12-07T04:00:00Z")
if err != nil { /* handle */ }
t.Unix() // → 1733529600The Reference Time (CRITICAL)
// Go uses this EXACT reference time for formatting
// Mon Jan 2 15:04:05 MST 2006
// = 01/02 03:04:05PM '06 -0700
// Common format layouts
time.RFC3339 // "2006-01-02T15:04:05Z07:00"
time.RFC3339Nano // with nanoseconds
time.RFC822 // "02 Jan 06 15:04 MST"
time.Kitchen // "3:04PM"
// Custom formats use the reference time
t.Format("2006-01-02") // → "2024-12-07"
t.Format("02/01/2006 15:04") // → "07/12/2024 04:00"
t.Format("Jan 2, 2006") // → "Dec 7, 2024"
// ✗ WRONG — these are NOT format strings
t.Format("YYYY-MM-DD") // broken output
t.Format("yyyy/mm/dd") // broken outputTimezone Handling
// Load timezone
loc, err := time.LoadLocation("America/New_York")
if err != nil { /* handle */ }
// Convert UTC to timezone
utc := time.Unix(1733529600, 0).UTC()
eastern := utc.In(loc)
// Format with timezone
eastern.Format("2006-01-02 15:04:05 MST")
// → "2024-12-06 23:00:00 EST"
// Common locations
time.UTC
time.Local
// Load others with time.LoadLocation()
// Get offset
_, offset := eastern.Zone()
// offset in seconds from UTCDate Arithmetic
t := time.Now() // Add duration tomorrow := t.Add(24 * time.Hour) nextWeek := t.Add(7 * 24 * time.Hour) in1Hour := t.Add(time.Hour) in30Mins := t.Add(30 * time.Minute) // Subtract yesterday := t.Add(-24 * time.Hour) // Duration between two times diff := t2.Sub(t1) diff.Hours() // float64 diff.Minutes() // float64 diff.Seconds() // float64 // Between Unix timestamps diffSec := ts2 - ts1 days := diffSec / 86400 hours := (diffSec % 86400) / 3600
Expiry & Comparison
now := time.Now() // Compare times t1.Before(t2) t1.After(t2) t1.Equal(t2) // Check JWT expiry (exp is Unix seconds) expTime := time.Unix(payload.Exp, 0) isExpired := time.Now().After(expTime) // Time until expiry timeLeft := expTime.Sub(time.Now()) isExpiringSoon := timeLeft < 5*time.Minute // Create expiry expiry := time.Now().Add(time.Hour).Unix()
Parsing Common Formats
import "strconv"
// RFC3339 (ISO 8601)
t, _ := time.Parse(time.RFC3339,
"2024-12-07T04:00:00Z")
// Custom format
t, _ := time.Parse("2006-01-02 15:04:05",
"2024-12-07 04:00:00")
// With timezone
t, _ := time.Parse("2006-01-02T15:04:05-07:00",
"2024-12-06T23:00:00-05:00")
// Apache log format
t, _ := time.Parse("02/Jan/2006:15:04:05 -0700",
"07/Dec/2024:04:00:00 +0000")
// Unix timestamp string
tsStr := "1733529600"
ts, _ := strconv.ParseInt(tsStr, 10, 64)
t := time.Unix(ts, 0)⚠️ Common Mistakes
// ✗ WRONG — UnixNano overflows ~year 2262
t.UnixNano() // int64 max = ~year 2262
// ✓ RIGHT for far-future dates
t.Unix() // seconds — safe for billions of years
// ✗ WRONG — time.Now() is local time
t := time.Now()
// ✓ RIGHT — always store UTC
t := time.Now().UTC()
// ✗ WRONG — format string uses wrong reference
t.Format("YYYY-MM-DD")
// ✓ RIGHT — Go uses 2006-01-02
t.Format("2006-01-02")
// ✗ WRONG — comparing wall clock to monotonic
// Use t.Round(0) to strip monotonic reading
t1.Round(0).Equal(t2.Round(0))Useful Constants
// time.Duration constants
time.Nanosecond
time.Microsecond
time.Millisecond
time.Second // = 1,000,000,000 nanoseconds
time.Minute // = 60 * time.Second
time.Hour // = 60 * time.Minute
// Compute longer durations
oneDay := 24 * time.Hour
oneWeek := 7 * 24 * time.Hour
// Famous Unix timestamps
var (
Y2K = time.Unix(946684800, 0)
Y2038Limit = time.Unix(2147483647, 0)
UnixEpoch = time.Unix(0, 0)
)
// Current in multiple formats
t := time.Now().UTC()
t.Unix() // seconds
t.UnixMilli() // ms
t.UnixMicro() // µs
t.UnixNano() // nsunixcalculator.com · /knowledge/golang-unix-timestamps · /tutorials/javascript-timestamps