Developer

TIMESTAMP CONVERTER

Convert Unix timestamps to human dates and back. Live updating, multiple formats, relative time.

Live — updates every second
0
Unix timestamp (seconds) · click to copy
Milliseconds
Human (local)
UTC
ISO 8601

Common Timestamps
Developer Reference

Core Algorithm & Standalone Script

Standalone, zero-dependency JavaScript implementation powering this tool. Free to inspect, copy, and build upon.

// ── Live clock ──────────────────────────────────────────────
  function updateLive() {
    const now = new Date();
    const s = Math.floor(now.getTime() / 1000);
    document.getElementById('liveTs').textContent = s;
    document.getElementById('liveMs').textContent = now.getTime();
    document.getElementById('liveHuman').textContent = now.toLocaleString();
    document.getElementById('liveUtc').textContent = now.toUTCString();
    document.getElementById('liveIso').textContent = now.toISOString();
  }
  updateLive();
  setInterval(updateLive, 1000);

  document.getElementById('liveTs').addEventListener('click', function() {
    copyLiveTs();
  });

  function copyLiveTs() {
    const val = document.getElementById('liveTs').textContent;
    navigator.clipboard.writeText(val).catch(() => {});
    const btn = document.querySelector('.live-card .btn-primary');
    const orig = btn.textContent;
    btn.textContent = 'Copied!';
    setTimeout(() => btn.textContent = orig, 2000);
  }

  // ── Tabs ────────────────────────────────────────────────────
  function switchTab(id, el) {
    document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
    document.querySelectorAll('.tab-panel').forEach(p => p.classList.remove('active'));
    el.classList.add('active');
    document.getElementById('panel-' + id).classList.add('active');
  }

  // ── Unix → Human ────────────────────────────────────────────
  function convertTs() {
    const raw = document.getElementById('tsInput').value.trim();
    const errEl = document.getElementById('tsError');
    const resEl = document.getElementById('tsResults');
    errEl.classList.add('hidden');
    resEl.style.display = 'none';
    if (!raw) return;

    let ms;
    const num = Number(raw);
    if (isNaN(num) || !isFinite(num)) {
      errEl.textContent = 'Invalid timestamp — enter a numeric Unix timestamp.';
      errEl.classList.remove('hidden');
      return;
    }
    // Auto-detect: if > 13 digits treat as ms, otherwise seconds
    if (raw.replace('-','').length >= 13) {
      ms = num;
    } else {
      ms = num * 1000;
    }

    const d = new Date(ms);
    if (isNaN(d.getTime())) {
      errEl.textContent = 'Could not parse timestamp.';
      errEl.classList.remove('hidden');
      return;
    }

    document.getElementById('r-local').textContent = d.toLocaleString();
    document.getElementById('r-utc').textContent = d.toUTCString();
    document.getElementById('r-iso').textContent = d.toISOString();
    document.getElementById('r-relative').textContent = relativeTime(d);
    document.getElementById('r-dow').textContent = d.toLocaleDateString(undefined, { weekday: 'long' });
    document.getElementById('r-doy').textContent = getDayOfYear(d);
    document.getElementById('r-week').textContent = getWeekNumber(d);

    resEl.style.display = 'block';
  }

  function relativeTime(d) {
    const diffMs = d - new Date();
    const abs = Math.abs(diffMs);
    const s = Math.floor(abs / 1000);
    const m = Math.floor(s / 60);
    const h = Math.floor(m / 60);
    const days = Math.floor(h / 24);
    const months = Math.floor(days / 30.44);
    const years = Math.floor(days / 365.25);
    const past = diffMs < 0;
    let str;
    if (s < 60) str = s + ' second' + (s !== 1 ? 's' : '');
    else if (m < 60) str = m + ' minute' + (m !== 1 ? 's' : '');
    else if (h < 24) str = h + ' hour' + (h !== 1 ? 's' : '');
    else if (days < 30) str = days + ' day' + (days !== 1 ? 's' : '');
    else if (months < 12) str = months + ' month' + (months !== 1 ? 's' : '');
    else str = years + ' year' + (years !== 1 ? 's' : '');
    return past ? str + ' ago' : 'in ' + str;
  }

  function getDayOfYear(d) {
    const start = new Date(d.getFullYear(), 0, 0);
    const diff = d - start;
    const oneDay = 1000 * 60 * 60 * 24;
    return Math.floor(diff / oneDay);
  }

  function getWeekNumber(d) {
    const onejan = new Date(d.getFullYear(), 0, 1);
    return Math.ceil((((d - onejan) / 86400000) + onejan.getDay() + 1) / 7);
  }

  // ── Human → Unix ────────────────────────────────────────────
  function convertDt() {
    const val = document.getElementById('dtInput').value;
    const resEl = document.getElementById('dtResults');
    if (!val) { resEl.style.display = 'none'; return; }
    const d = new Date(val);
    if (isNaN(d.getTime())) { resEl.style.display = 'none'; return; }
    const s = Math.floor(d.getTime() / 1000);
    document.getElementById('d-seconds').textContent = s;
    document.getElementById('d-ms').textContent = d.getTime();
    document.getElementById('d-iso').textContent = d.toISOString();
    document.getElementById('d-utc').textContent = d.toUTCString();
    resEl.style.display = 'block';
  }

  // ── Copy helper ──────────────────────────────────────────────
  function copyResult(id, btn) {
    const text = document.getElementById(id).textContent;
    navigator.clipboard.writeText(text).catch(() => {});
    btn.textContent = 'copied!';
    btn.classList.add('copied');
    setTimeout(() => { btn.textContent = 'copy'; btn.classList.remove('copied'); }, 2000);
  }

  // ── Presets ──────────────────────────────────────────────────
  const presets = [
    { label: 'Unix Epoch (1970)', ts: 0 },
    { label: 'Y2K (2000)', ts: 946684800 },
    { label: 'Jan 1 2010', ts: 1262304000 },
    { label: 'Jan 1 2020', ts: 1577836800 },
    { label: 'Jan 1 2024', ts: 1704067200 },
    { label: 'Jan 1 2025', ts: 1735689600 },
  ];

  const container = document.getElementById('presets');
  presets.forEach(p => {
    const btn = document.createElement('button');
    btn.className = 'preset-btn';
    btn.textContent = p.label;
    btn.onclick = () => {
      document.getElementById('tsInput').value = p.ts;
      convertTs();
    };
    container.appendChild(btn);
  });

  // Set default datetime-local to now
  (function() {
    const now = new Date();
    const pad = n => String(n).padStart(2, '0');
    const val = now.getFullYear() + '-' + pad(now.getMonth()+1) + '-' + pad(now.getDate()) + 'T' + pad(now.getHours()) + ':' + pad(now.getMinutes());
    document.getElementById('dtInput').value = val;
    convertDt();
  })();