JavaScript Korea Age Calculator
JavaScript
const ageDate = document.querySelector(".ageDate");
const ageCalc = document.querySelector(".ageCalc");
ageCalc.addEventListener("click", (e) => {
if (ageDate.value != '' ) {
let birthday = new Date(ageDate.value + 'T00:00:00.000Z');
const d = new Date();
let year = d.getFullYear();
let month = clockFormat(d.getMonth() + 1);
let date = clockFormat(d.getDate());
let today = new Date(`${year}-${month}-${date}T00:00:00.000Z`);
let years = today.getFullYear() - birthday.getFullYear();
birthday.setFullYear(today.getFullYear());
if (today < birthday){
years--;
}
document.querySelector(".age-result").innerHTML = "Korea Age " + years;
}
});
const clockFormat = (t) => {
return String(t).padStart(2, '0');
};