← Back to Blog

Working with Epoch Time in JavaScript, Python, and Go

Every developer works with Unix timestamps regularly — parsing API responses, logging events, scheduling tasks, comparing dates. But the syntax varies significantly between languages. This guide provides practical, copy-paste-ready examples for the three languages you're most likely to encounter in backend and full-stack development: JavaScript, Python, and Go.

JavaScript: The Millisecond Language

JavaScript is unique among major languages because Date.now() returns milliseconds, not seconds. This catches many developers off guard when interacting with APIs that use second-precision timestamps.

Getting the Current Timestamp

// Milliseconds (JavaScript native)
const msTimestamp = Date.now();           // 1710000000000

// Seconds (most APIs expect this)
const secTimestamp = Math.floor(Date.now() / 1000);  // 1710000000

// High-resolution (Node.js only)
const hrTime = process.hrtime.bigint();  // nanoseconds

Converting Timestamp to Date

// From seconds (multiply by 1000!)
const date = new Date(1710000000 * 1000);

// From milliseconds (direct)
const date2 = new Date(1710000000000);

// Format as ISO 8601
date.toISOString();  // "2024-03-09T16:00:00.000Z"

// Format in a specific timezone
date.toLocaleString('en-US', {
  timeZone: 'America/New_York',
  dateStyle: 'full',
  timeStyle: 'long'
});
// "Saturday, March 9, 2024 at 11:00:00 AM EST"

Converting Date to Timestamp

// From a date string
const ts = Math.floor(new Date('2026-03-10T14:30:00Z').getTime() / 1000);

// From date components (months are 0-indexed!)
const ts2 = Math.floor(new Date(2026, 2, 10, 14, 30).getTime() / 1000);

// Common gotcha: new Date() without 'Z' uses LOCAL timezone
new Date('2026-03-10T14:30:00');   // local timezone ⚠️
new Date('2026-03-10T14:30:00Z');  // UTC ✅

Date Arithmetic

// Add 24 hours
const tomorrow = new Date(Date.now() + 86400 * 1000);

// Difference in minutes
const diff = (date2 - date1) / (1000 * 60);

// Start of today (UTC)
const startOfDay = new Date();
startOfDay.setUTCHours(0, 0, 0, 0);

Python: Clean and Intuitive

Python's time and datetime modules make timestamp handling straightforward. Python uses seconds with float precision by default.

Getting the Current Timestamp

import time
from datetime import datetime, timezone

# Seconds (float with microsecond precision)
time.time()          # 1710000000.123456

# Seconds (integer)
int(time.time())     # 1710000000

# Using datetime
datetime.now(timezone.utc).timestamp()  # 1710000000.123456

Converting Timestamp to Date

from datetime import datetime, timezone

# UTC datetime from timestamp
dt = datetime.fromtimestamp(1710000000, tz=timezone.utc)
# datetime(2024, 3, 9, 16, 0, tzinfo=timezone.utc)

# Format as ISO 8601
dt.isoformat()  # "2024-03-09T16:00:00+00:00"

# Custom format
dt.strftime("%B %d, %Y at %I:%M %p UTC")
# "March 09, 2024 at 04:00 PM UTC"

# ⚠️ NEVER use fromtimestamp() without tz parameter
# It uses local timezone and causes bugs in production!
datetime.fromtimestamp(1710000000)  # Local TZ — BAD ⚠️
datetime.fromtimestamp(1710000000, tz=timezone.utc)  # UTC — GOOD ✅

Converting Date to Timestamp

from datetime import datetime, timezone

# From a date string
dt = datetime.fromisoformat("2026-03-10T14:30:00+00:00")
int(dt.timestamp())  # 1773333000

# From components (always specify timezone!)
dt = datetime(2026, 3, 10, 14, 30, tzinfo=timezone.utc)
int(dt.timestamp())  # 1773333000

Go: Explicit and Type-Safe

Go's time package is well-designed and explicit. It uses a time.Time type that carries timezone information, avoiding common pitfalls. Go's unique date formatting uses a reference date: Mon Jan 2 15:04:05 MST 2006.

Getting the Current Timestamp

package main
import "time"

// Seconds
ts := time.Now().Unix()        // int64: 1710000000

// Milliseconds
tsMs := time.Now().UnixMilli() // int64: 1710000000000

// Microseconds
tsUs := time.Now().UnixMicro() // int64: 1710000000000000

// Nanoseconds
tsNs := time.Now().UnixNano()  // int64: 1710000000000000000

Converting Timestamp to Date

// From seconds
t := time.Unix(1710000000, 0)  // 2024-03-09 16:00:00 +0000 UTC

// From milliseconds
t := time.UnixMilli(1710000000000)

// Format (Go's reference time: Mon Jan 2 15:04:05 MST 2006)
t.Format(time.RFC3339)  // "2024-03-09T16:00:00Z"
t.Format("2006-01-02")  // "2024-03-09"
t.Format("January 2, 2006 at 3:04 PM MST")  // "March 9, 2024 at 4:00 PM UTC"

// Convert to a timezone
loc, _ := time.LoadLocation("America/New_York")
t.In(loc).Format(time.RFC3339)  // "2024-03-09T11:00:00-05:00"

Common Gotchas Across Languages

  • Seconds vs milliseconds: JavaScript uses ms, Python uses seconds (float), Go has separate methods. Always check which unit an API expects.
  • Timezone-naive dates: In Python and JavaScript, creating dates without explicit timezone info uses the local timezone of the server — a recipe for bugs in production.
  • Integer overflow: In languages with 32-bit integers, timestamps after 2038 will overflow. Use 64-bit integers (int64 in Go, BigInt in JS if needed).
  • Floating point precision: Python's time.time() returns a float, which loses precision for large timestamps. Use int() or time.time_ns() for exact values.
  • Daylight Saving Time: When converting from a local date string to a timestamp, be aware that some local times are ambiguous (during the DST "fall back" hour) or don't exist (during the "spring forward" skip).

Quick Conversion Cheat Sheet

TaskJavaScriptPythonGo
Now (sec)Math.floor(Date.now()/1000)int(time.time())time.Now().Unix()
ts → Datenew Date(ts*1000)datetime.fromtimestamp(ts, utc)time.Unix(ts, 0)
Date → tsMath.floor(d.getTime()/1000)int(dt.timestamp())t.Unix()

Conclusion

Working with Unix timestamps is a daily task for backend developers, and the patterns shown here cover 95% of real-world use cases. When you need a quick conversion without opening your IDE, use EpochNow — it handles seconds, milliseconds, and microseconds across 14+ timezones, all in your browser.