-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added javascript in 24_age_calculator
- Loading branch information
1 parent
d633ec0
commit 03a5d0e
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |