For Developers
How to get the current timestamp, convert it to a date, and convert a date back to a timestamp in TypeScript — plus the milliseconds mismatch that breaks type-safe code anyway.
const ts: number = Date.now(); // milliseconds
const tsSeconds: number = Math.floor(Date.now() / 1000);
const seconds: number = 1750000000;
const date: Date = new Date(seconds * 1000);
date.toISOString(); // always UTC
const date: Date = new Date(Date.UTC(2025, 5, 15, 12, 0, 0));
const ts: number = Math.floor(date.getTime() / 1000);
TypeScript's type system checks that a value is a number, but it has no concept of which number — seconds and milliseconds are the same type, so nothing warns you when a backend API's seconds-based Unix timestamp gets passed straight into new Date(), which expects milliseconds. The result is a date sitting somewhere in 1970, and it compiles and type-checks perfectly. Some teams solve this with branded types (type UnixSeconds = number & { __brand: 'seconds' }) to make the unit part of the type; at minimum, name every timestamp variable with its unit so the mismatch is visible at the call site.
Paste it into the Timestamp ⇄ Date Converter to check it instantly, in any timezone, without writing any code. Working across several languages on the same project? The full Epoch Time in Programming Languages guide has all of them side by side for quick comparison.
Related
Every language's snippets side by side, for quick comparison.
Convert any timestamp instantly, no code required.
The plain-JS version of these same conversions.