Age Calculator
Calculate exact age in years, months and days. Also shows days until next birthday. Useful for school and college admission age verification.
Advertisement
Advertisement
document.addEventListener('DOMContentLoaded',()=>{
const today=new Date();
document.getElementById('ondate').value=today.toISOString().split('T')[0];
document.getElementById('related-grid').innerHTML=TOOLS.filter(t=>t.category==='student'&&t.slug!=='age-calculator').slice(0,4).map(buildToolCard).join('');
});
function calculate(){
const dob=new Date(document.getElementById('dob').value);
const on=new Date(document.getElementById('ondate').value);
if(!document.getElementById('dob').value){alert('Please enter date of birth.');return;}
if(dob>on){alert('Date of birth cannot be in the future.');return;}
let years=on.getFullYear()-dob.getFullYear();
let months=on.getMonth()-dob.getMonth();
let days=on.getDate()-dob.getDate();
if(days<0){months--;const prev=new Date(on.getFullYear(),on.getMonth(),0);days+=prev.getDate();}
if(months<0){years--;months+=12;}
const totalDays=Math.floor((on-dob)/(1000*60*60*24));
const nextBday=new Date(on.getFullYear(),dob.getMonth(),dob.getDate());
if(nextBday<=on)nextBday.setFullYear(on.getFullYear()+1);
const daysToB=Math.ceil((nextBday-on)/(1000*60*60*24));
document.getElementById('result').style.display='block';
document.getElementById('result').innerHTML=`