|
1 |
| -const months=[31,28,31,30,31,30,31,31,30,31,30,31]; |
2 |
| - |
3 |
| -function ageCalc(){ |
4 |
| - let today=new Date(); |
5 |
| - let inputDate= new Date(document.getElementById("date").value); |
6 |
| - let birthMonth,birthDate,birthYear; |
7 |
| - let birthDetails={ |
8 |
| - date:inputDate.getDate(), |
9 |
| - month:inputDate.getMonth()+1, |
10 |
| - year:inputDate.getFullYear() |
| 1 | +@ @ - 0, 0 + 1, 72 @ @ |
| 2 | +const months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; |
| 3 | + |
| 4 | +function ageCalc() { |
| 5 | + let today = new Date(); |
| 6 | + let inputDate = new Date(document.getElementById("date").value); |
| 7 | + let birthMonth, birthDate, birthYear; |
| 8 | + let birthDetails = { |
| 9 | + date: inputDate.getDate(), |
| 10 | + month: inputDate.getMonth() + 1, |
| 11 | + year: inputDate.getFullYear() |
11 | 12 | }
|
12 |
| - let currentYear=today.getFullYear(); |
13 |
| - let currentMonth=today.getMonth()+1; |
14 |
| - let currentDay=today.getDate(); |
| 13 | + let currentYear = today.getFullYear(); |
| 14 | + let currentMonth = today.getMonth() + 1; |
| 15 | + let currentDay = today.getDate(); |
15 | 16 |
|
16 | 17 |
|
17 | 18 | leapChecker(currentYear);
|
18 | 19 |
|
19 | 20 |
|
20 |
| - if(birthDetails.year>currentYear || (birthDetails.month > currentMonth && birthDetails.year == currentYear) || |
21 |
| - (birthDetails.date > currentDay && birthDetails.month == currentMonth && birthDetails.year == currentYear)){ |
| 21 | + if (birthDetails.year > currentYear || (birthDetails.month > currentMonth && birthDetails.year == currentYear) || |
| 22 | + (birthDetails.date > currentDay && birthDetails.month == currentMonth && birthDetails.year == currentYear)) { |
22 | 23 | alert("Enter Valid Date");
|
23 |
| - |
24 |
| - } |
25 |
| - birthYear=currentYear-birthDetails.year; |
26 |
| - if(currentMonth>=birthDetails.month){ |
27 |
| - birthMonth=currentMonth-birthDetails.month; |
| 24 | + |
28 | 25 | }
|
29 |
| - else{ |
| 26 | + birthYear = currentYear - birthDetails.year; |
| 27 | + if (currentMonth >= birthDetails.month) { |
| 28 | + birthMonth = currentMonth - birthDetails.month; |
| 29 | + } else { |
30 | 30 | birthYear--;
|
31 |
| - birthMonth=12+currentMonth-birthDetails.month; |
| 31 | + birthMonth = 12 + currentMonth - birthDetails.month; |
32 | 32 | }
|
33 |
| - if(currentDay>=birthDetails.date){ |
34 |
| - birthDate=currentDay-birthDetails.date; |
| 33 | + if (currentDay >= birthDetails.date) { |
| 34 | + birthDate = currentDay - birthDetails.date; |
35 | 35 |
|
36 |
| - } |
37 |
| - else{ |
| 36 | + } else { |
38 | 37 | birthMonth--;
|
39 |
| - let days=months[currentMonth-2]; |
40 |
| - birthDate=days+currentDay- birthDetails.date; |
41 |
| - if(birthMonth<0){ |
42 |
| - birthMonth=11; |
| 38 | + let days = months[currentMonth - 2]; |
| 39 | + birthDate = days + currentDay - birthDetails.date; |
| 40 | + if (birthMonth < 0) { |
| 41 | + birthMonth = 11; |
43 | 42 | birthYear--;
|
44 | 43 | }
|
45 | 44 |
|
46 | 45 | }
|
47 | 46 |
|
48 |
| - display(birthDate,birthMonth,birthYear); |
49 |
| - |
| 47 | + display(birthDate, birthMonth, birthYear); |
| 48 | + |
50 | 49 | }
|
51 |
| -function display(bdate,bmonth,byear){ |
52 |
| - document.getElementById("years").textContent=byear; |
53 |
| - document.getElementById("months").textContent=bmonth; |
54 |
| - document.getElementById("days").textContent=bdate; |
| 50 | + |
| 51 | +function display(bdate, bmonth, byear) { |
| 52 | + document.getElementById("years").textContent = byear; |
| 53 | + document.getElementById("months").textContent = bmonth; |
| 54 | + document.getElementById("days").textContent = bdate; |
55 | 55 |
|
56 | 56 |
|
57 | 57 | }
|
58 | 58 |
|
59 |
| -function leapChecker(year){ |
60 |
| - if(year%4==0 || (year%100==0 && year%400==0)){ |
61 |
| - months[1]=29; |
62 |
| - } |
63 |
| - else{ |
64 |
| - months[1]=28; |
| 59 | +function leapChecker(year) { |
| 60 | + if (year % 4 == 0 || (year % 100 == 0 && year % 400 == 0)) { |
| 61 | + months[1] = 29; |
| 62 | + } else { |
| 63 | + months[1] = 28; |
65 | 64 | }
|
66 | 65 |
|
67 | 66 |
|
68 | 67 |
|
69 |
| - |
70 | 68 |
|
71 |
| - } |
72 | 69 |
|
| 70 | +} |
0 commit comments