JavaScript Unix Timestamps Cheatsheet

    Last updated May 2026

    JavaScript Timestamps Cheatsheet

    Every timestamp operation you need. Print or save as PDF.

    Get Current Timestamp

    // Unix seconds (integer)
    Math.floor(Date.now() / 1000)
    // → 1733529600
    
    // Unix milliseconds
    Date.now()
    // → 1733529600000
    
    // As Date object
    new Date()
    // → Sat Dec 07 2024...
    
    // High resolution (not Unix)
    performance.now()
    // → 1234.567 (ms since page load)

    Timestamp → Human Readable

    // Seconds → Date object
    new Date(ts * 1000)
    
    // Milliseconds → Date object  
    new Date(tsMs)
    
    // → ISO 8601 string
    new Date(ts * 1000).toISOString()
    // "2024-12-07T04:00:00.000Z"
    
    // → UTC string
    new Date(ts * 1000).toUTCString()
    // "Sat, 07 Dec 2024 04:00:00 GMT"
    
    // → Local string
    new Date(ts * 1000).toLocaleString()

    Date → Timestamp

    // Date string → Unix seconds
    Math.floor(new Date('2024-12-07').getTime() / 1000)
    // → 1733529600
    
    // Date object → Unix seconds
    Math.floor(someDate.getTime() / 1000)
    
    // ISO string → Unix ms
    new Date('2024-12-07T04:00:00Z').getTime()
    // → 1733529600000
    
    // Specific date → Unix seconds
    Math.floor(new Date(2024, 11, 7).getTime() / 1000)
    // Note: month is 0-indexed! Dec = 11

    Timezone-Aware Formatting

    const date = new Date(1733529600 * 1000);
    
    // Intl.DateTimeFormat (recommended)
    new Intl.DateTimeFormat('en-US', {
      timeZone: 'America/New_York',
      dateStyle: 'full',
      timeStyle: 'long',
    }).format(date)
    
    // Multiple timezones
    ['UTC','America/New_York','Asia/Tokyo']
      .map(tz => ({
        tz,
        time: date.toLocaleString('en-US', { timeZone: tz })
      }))

    Date Arithmetic

    const now = Math.floor(Date.now() / 1000);
    
    // Add time (in seconds)
    const ONE_HOUR  = 3600;
    const ONE_DAY   = 86400;
    const ONE_WEEK  = 604800;
    const ONE_MONTH = 2592000; // ~30 days
    
    const tomorrow = now + ONE_DAY;
    const lastWeek = now - ONE_WEEK;
    
    // Duration between two timestamps
    const diff = ts2 - ts1; // seconds
    const days  = Math.floor(diff / 86400);
    const hours = Math.floor((diff % 86400) / 3600);
    const mins  = Math.floor((diff % 3600) / 60);

    ⚠️ Common Mistakes

    // ✗ WRONG — date shows 1970
    new Date(1733529600)
    // ✓ RIGHT — multiply seconds by 1000
    new Date(1733529600 * 1000)
    
    // ✗ WRONG — month 12 = January next year
    new Date(2024, 12, 1)
    // ✓ RIGHT — December = 11
    new Date(2024, 11, 1)
    
    // ✗ WRONG — float loses precision at huge scales
    const ts = parseFloat("1733529600.999")
    // ✓ RIGHT — use BigInt for nanoseconds-scale literals
    const ns = BigInt("1733529600999000000")

    Format Quick Reference

    DigitsUnitExample
    10 digitsSeconds1733529600
    13 digitsMilliseconds1733529600000
    16 digitsMicroseconds1733529600000000
    19 digitsNanoseconds1733529600000000000
    // Auto-detect format
    function detectFormat(ts) {
      const d = String(Math.abs(ts)).replace('.','').length
      if (d <= 10) return 'seconds'
      if (d <= 13) return 'milliseconds'
      if (d <= 16) return 'microseconds'
      return 'nanoseconds'
    }

    JWT / Session Expiry

    const now = Math.floor(Date.now() / 1000);
    
    // Check if token is expired
    const isExpired = payload.exp < now;
    
    // Time until expiry
    const secondsLeft = payload.exp - now;
    const isExpiringSoon = secondsLeft < 300; // 5 min
    
    // Create expiry timestamp
    const expiry = now + 3600; // 1 hour from now
    
    // Session timeout
    const SESSION_TTL = 30 * 60; // 30 minutes
    const sessionExpiry = now + SESSION_TTL;
    const isSessionValid = now < sessionExpiry;

    Relative Time Display

    function timeAgo(ts) {
      const diff = Math.floor(Date.now()/1000) - ts;
      if (diff < 60)   return 'just now';
      if (diff < 3600) return Math.floor(diff/60) + 'm ago';
      if (diff < 86400)return Math.floor(diff/3600) + 'h ago';
      return Math.floor(diff/86400) + 'd ago';
    }
    
    // Or use Intl.RelativeTimeFormat
    const rtf = new Intl.RelativeTimeFormat('en');
    rtf.format(-1, 'day')   // "1 day ago"
    rtf.format(2, 'hour')   // "in 2 hours"

    Useful Constants

    const SECOND = 1;
    const MINUTE = 60;
    const HOUR   = 3_600;
    const DAY    = 86_400;
    const WEEK   = 604_800;
    const MONTH  = 2_592_000;  // 30 days
    const YEAR   = 31_536_000; // 365 days
    
    // Famous timestamps
    const UNIX_EPOCH  = 0;          // 1970-01-01
    const Y2K         = 946684800;  // 2000-01-01
    const Y2038_LIMIT = 2147483647; // 2038-01-19
    const MAX_SAFE_MS = Number.MAX_SAFE_INTEGER;
    // = 9007199254740991

    unixcalculator.com — Free developer tools & timestamp reference · /tools/timestamp-debugger · /tools/jwt-decoder