Date & Time
DATE DIFF
Calculate the exact difference between two dates in years, months, days, hours, and more.
0
Years
0
Months
0
Weeks
0
Days
Total days—
Total weeks—
Total hours—
Total minutes—
Total seconds—
Business days (Mon–Fri)—
Developer Reference
Core Algorithm & Standalone Script
Standalone, zero-dependency JavaScript implementation powering this tool. Free to inspect, copy, and build upon.
function setToday() {
const today = new Date().toISOString().slice(0,10);
document.getElementById('dateA').value = today;
calculate();
}
function swap() {
const a = document.getElementById('dateA').value, b = document.getElementById('dateB').value;
document.getElementById('dateA').value = b; document.getElementById('dateB').value = a;
calculate();
}
function toggleTime() {
document.getElementById('timeRow').classList.toggle('hidden', !document.getElementById('includeTime').checked);
calculate();
}
function businessDays(d1, d2) {
if (d1 > d2) [d1,d2] = [d2,d1];
let count = 0, cur = new Date(d1);
while (cur <= d2) {
const day = cur.getDay();
if (day !== 0 && day !== 6) count++;
cur.setDate(cur.getDate() + 1);
}
return count;
}
function calculate() {
const da = document.getElementById('dateA').value, db = document.getElementById('dateB').value;
if (!da || !db) { document.getElementById('results').classList.add('hidden'); return; }
const incTime = document.getElementById('includeTime').checked;
const ta = incTime ? document.getElementById('timeA').value : '00:00';
const tb = incTime ? document.getElementById('timeB').value : '00:00';
let dtA = new Date(`${da}T${ta}`), dtB = new Date(`${db}T${tb}`);
const swapped = dtA > dtB;
if (swapped) [dtA, dtB] = [dtB, dtA];
const diffMs = dtB - dtA;
const diffSec = Math.floor(diffMs / 1000);
const diffMin = Math.floor(diffSec / 60);
const diffHrs = Math.floor(diffMin / 60);
const diffDays = Math.floor(diffHrs / 24);
const diffWeeks = Math.floor(diffDays / 7);
// Years/months
let yrs = dtB.getFullYear() - dtA.getFullYear();
let mos = dtB.getMonth() - dtA.getMonth();
let dys = dtB.getDate() - dtA.getDate();
if (dys < 0) { mos--; dys += new Date(dtB.getFullYear(), dtB.getMonth(), 0).getDate(); }
if (mos < 0) { yrs--; mos += 12; }
document.getElementById('statYears').textContent = yrs;
document.getElementById('statMonths').textContent = mos;
document.getElementById('statWeeks').textContent = diffWeeks;
document.getElementById('statDays').textContent = dys;
document.getElementById('bDays').textContent = diffDays.toLocaleString();
document.getElementById('bWeeks').textContent = diffWeeks.toLocaleString();
document.getElementById('bHours').textContent = diffHrs.toLocaleString();
document.getElementById('bMins').textContent = diffMin.toLocaleString();
document.getElementById('bSecs').textContent = diffSec.toLocaleString();
document.getElementById('bBiz').textContent = businessDays(dtA, dtB).toLocaleString();
const parts = [];
if (yrs) parts.push(`${yrs} year${yrs!==1?'s':''}`);
if (mos) parts.push(`${mos} month${mos!==1?'s':''}`);
if (dys) parts.push(`${dys} day${dys!==1?'s':''}`);
document.getElementById('resultSummary').innerHTML = `${swapped?'(dates swapped) ':''}<span>${parts.join(', ') || '0 days'}</span> between the two dates.`;
document.getElementById('results').classList.remove('hidden');
}
// Default: today and 1 year ago
const today = new Date();
const oneYearAgo = new Date(today); oneYearAgo.setFullYear(oneYearAgo.getFullYear() - 1);
document.getElementById('dateA').value = oneYearAgo.toISOString().slice(0,10);
document.getElementById('dateB').value = today.toISOString().slice(0,10);
calculate();