Skip to content

Commit

Permalink
added javascript in 24_age_calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
nitishkhobragade committed Apr 14, 2024
1 parent d633ec0 commit 03a5d0e
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 24_age_calculator/age.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
let cal_btn = document.querySelector(".calculator");
let getDate, dobDate, dobMonth, dobYear;
let year, month, date;

cal_btn.addEventListener('click', () => {
let dob = document.querySelector("#date").value;
getDate = new Date(dob);
dobDate = getDate.getDate();
dobMonth = getDate.getMonth() + 1;
dobYear = getDate.getFullYear();

// You can log the variables here if you want to see them every time the button is clicked
console.log(getDate, dobDate, dobMonth, dobYear);

let currentDate = new Date();
let currDate = currentDate.getDate();
let currYear = currentDate.getFullYear();
let currMonth = currentDate.getMonth();

// Calculate the age
year = currYear - dobYear;
console.log(year);
if (currMonth >= dobMonth) {
month = currMonth - dobMonth;
} else {
year--;
date = new Date(year, month, 0).getDate() + currDate - dobDate;
}
console.log(year, date, month);
});

// Now you can access getDate, dobDate, dobMonth, dobYear, year, date, and month outside the event listener function

0 comments on commit 03a5d0e

Please sign in to comment.