-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculateBirthAge.js
32 lines (22 loc) · 1014 Bytes
/
calculateBirthAge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* -------------------------------------------------------
* Programming Question : Calculate Age from BirthDate
* -------------------------------------------------------
**/
// Q. Create a function calculateAge that takes a birthDate as input and return the current age of the person. The birthDate will be provided in the format 'YYYY-MM-DD'.
//? The function must handle leap years and varying numbers of days in each month accuately.
//? Consider the timezone difference and ensure the age is calculated based on the current date in your local time zone.
//? The output should be the age in whole years.
//?
function calculateAge(dte) {
let cDate = new Date();
let bDate = new Date(dte);
let age = cDate.getFullYear() - bDate.getFullYear()
let month = cDate.getMonth() - bDate.getMonth();
if(month < 0 || month === 0 && cDate.getDate() < bDate.getDate()){
age --;
}
return age;
}
console.log(calculateAge("1995-04-17"));
console.log(calculateAge("1996-09-14"));