Tutorial · JavaScript

    Unix timestamps in JavaScript

    Quick answer: Use Date.now() for UTC milliseconds since epoch, new Date() for parsing, and performance.now() only for monotonic deltas inside one page load. For new code, evaluate the Temporal proposal via polyfill for safer calendars; until then pair UTC milliseconds with explicit field names (createdAtMs).

    Date.now() vs new Date() vs performance.now()

    Date.now() mirrors the number inside a Date object for "now" and is stable for persistence as long as you stay below Number.MAX_SAFE_INTEGER. new Date(isoString) invokes the spec parsing algorithm — some ISO variants are implementation-dependent; always supply full offsets. performance.now() starts at navigation and is not coupled to wall time; ideal for animation frames, wrong for databases.

    const ms = Date.now();
    const d = new Date(ms);
    console.log(d.toISOString());
    
    const t0 = performance.now();
    await fetch('/api/profile');
    console.log(`fetch ms (monotonic): ${(performance.now() - t0).toFixed(1)}`);

    Temporal API direction

    Temporal separates absolute instants, time zones, and calendars. Polyfills such as @js-temporal/polyfill let you experiment before full engine support. Adoption checklist: bundle size, SSR parity, and serialization format to your backend (often still RFC 3339 strings).

    // npm i @js-temporal/polyfill
    import { Temporal } from '@js-temporal/polyfill';
    
    const inst = Temporal.Now.instant();
    console.log(inst.epochMilliseconds);

    Intl.DateTimeFormat

    const fmt = new Intl.DateTimeFormat('en-US', {
      timeZone: 'Asia/Tokyo',
      dateStyle: 'full',
      timeStyle: 'long',
    });
    console.log(fmt.format(new Date(1713794701123)));

    Common mistakes

    • Month index starts at 0 in Date.UTC and Date constructor.
    • Parsing date-only strings may default to UTC vs local — specify behavior in lint rules.
    • Mixing seconds and ms silently — enforce length or string prefixes in APIs.

    Library comparison

    LibrarySize / focusTimezone
    date-fnsModular tree-shakeUse date-fns-tz companion
    LuxonRich APIFirst-class zones
    dayjsTiny coreplugin for timezone

    Key takeaways

    • Always label persisted numbers with units; JS cannot do that for you.
    • Prefer ISO strings on the wire when debugging; compact to ints only after validation.
    • Temporal + polyfill is the long-term escape hatch from Date quirks.
    • Server render: never assume client clock matches build server.
    • Compare our precision guide before expanding schema.

    Written by Unix Calculator Editorial Team — Last verified May 2026.

    Get the Unix Timestamp Cheatsheet

    One email. Instant cheatsheet. No drip sequence.

    Advertisement