Guide

Working with Unix Timestamps in JavaScript

Everything you need to work with Unix timestamps in JavaScript — getting the current time, creating Dates from timestamps, formatting for any timezone, and avoiding common mistakes.

Getting the current Unix timestamp

JavaScript provides several equivalent ways to get the current time as a numeric timestamp. All of these return the number of milliseconds since the Unix epoch.

  • Date.now() → the fastest and most readable — 1700000000000 (milliseconds)
  • Math.floor(Date.now() / 1000) → Unix timestamp in seconds — 1700000000
  • +new Date() → same as Date.now(), using the unary plus operator
  • new Date().getTime() → explicit method call, same result as Date.now()

Creating a Date object from a Unix timestamp

The Date constructor accepts milliseconds since the epoch. Always multiply a seconds-based timestamp by 1000 before passing it to the constructor.

  • new Date(1700000000 * 1000) → from seconds (most common case)
  • new Date(1700000000000) → from milliseconds (JavaScript APIs, Java)
  • new Date(Date.now()) → current time as a Date object
  • new Date(0) → the Unix epoch: January 1, 1970 00:00:00 UTC

Formatting dates — built-in string methods

The Date object has several built-in string conversion methods. Each produces a different format.

  • .toISOString() → '2023-11-15T06:13:20.000Z' — ISO 8601, always UTC, machine-readable
  • .toUTCString() → 'Wed, 15 Nov 2023 06:13:20 GMT' — RFC 7231, human-readable UTC
  • .toString() → local timezone string with full zone name
  • .toLocaleString() → locale-aware format in the user's local timezone

Timezone-aware formatting with Intl.DateTimeFormat

For consistent, timezone-aware date formatting without external libraries, use the Intl.DateTimeFormat API. It is supported in all modern browsers and Node.js 13+.

  • new Intl.DateTimeFormat('en-US', { timeZone: 'America/New_York', dateStyle: 'full', timeStyle: 'long' }).format(date)
  • date.toLocaleString('en-GB', { timeZone: 'Europe/London', hour12: false })
  • new Intl.DateTimeFormat('zh-CN', { timeZone: 'Asia/Shanghai' }).format(date)
  • new Intl.DateTimeFormat('en-CA', { timeZone: tz }).formatToParts(date) → array of {type, value} parts for custom layouts

Converting a date string back to a Unix timestamp

To go in the opposite direction — from a date string to a Unix timestamp — use Date.parse() or pass the string to the Date constructor.

  • new Date('2023-11-15T06:13:20Z').getTime() / 1000 → Unix seconds from ISO 8601 UTC
  • new Date('2023-11-15T01:13:20-05:00').getTime() → Unix milliseconds from ISO with offset
  • Date.parse('2023-11-15T06:13:20Z') → same as new Date(...).getTime()
  • Always use ISO 8601 format with explicit timezone for predictable parsing

Common pitfalls

These are the most frequent mistakes JavaScript developers make with dates and timestamps:

  • new Date(1700000000) — missing × 1000 converts to year ~1970 instead of 2023
  • getMonth() returns 0–11 not 1–12 — always add 1 when displaying
  • new Date('2024-01-01') is UTC midnight; new Date('2024/01/01') is local midnight
  • new Date(2024, 0, 1) = January 1 (month is 0-indexed in the constructor too)
  • Adding 86400000ms to get 'tomorrow' can fail at DST boundaries — use setDate(d.getDate() + 1) instead