← Back to Articles

Unix Timestamps in JavaScript and TypeScript

JavaScript represents dates as milliseconds since the Unix epoch internally, making it easy to work with Unix timestamps. This guide covers the most common patterns in both JS and TypeScript.

Get the current Unix timestamp

Date.now() returns milliseconds. Divide by 1000 and floor to get seconds.

// Milliseconds since epoch
const ms = Date.now(); // e.g. 1700000000000

// Seconds since epoch
const seconds = Math.floor(Date.now() / 1000); // e.g. 1700000000

// TypeScript with explicit type
const ts: number = Math.floor(Date.now() / 1000);
AdSense — In-Article Banner

Convert a timestamp to a Date object

Pass milliseconds to new Date(). If you have seconds, multiply by 1000 first.

// From seconds
const date = new Date(1700000000 * 1000);
console.log(date.toISOString()); // "2023-11-14T22:13:20.000Z"

// From milliseconds
const date2 = new Date(1700000000000);
console.log(date2.toUTCString());

Convert a Date back to a Unix timestamp

Use .getTime() to get milliseconds, or divide by 1000 for seconds.

const date = new Date('2026-01-15T12:00:00Z');

// Milliseconds
const ms = date.getTime(); // 1768468800000

// Seconds
const seconds = Math.floor(date.getTime() / 1000); // 1768468800
AdSense — In-Article Banner