Unix Timestamp in TypeScript
TypeScript uses the same runtime APIs as JavaScript — Date.now() for milliseconds — but lets you type timestamps explicitly (e.g. number) and model branded epoch types for safety.
Get & convert epoch time in TypeScript
const d: Date = new Date(1700000000000); const iso: string = d.toISOString(); // 2023-11-14T22:13:20.000Z const now: number = Math.floor(Date.now() / 1000);
Gotcha: Typing a value as Date vs number won't catch a seconds/milliseconds mix-up — consider a branded type like type EpochMs = number & { __ms: true }.
Frequently asked questions
- Is timestamp handling different in TypeScript?
- The runtime behaviour is identical to JavaScript; TypeScript only adds compile-time types around it.
- How do I type a Unix timestamp?
- As number. For extra safety use a branded type to distinguish seconds from milliseconds.