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