-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (86 loc) · 2.46 KB
/
index.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function calculateAge(date)
{
var age='invalid age'
if(date !== undefined || date !== null || date !== '') {
var currentDate = new Date();
var todayDate = currentDate.getDate();
var todayMonth = currentDate.getMonth();
var todayYear = currentDate.getFullYear();
var birthDate, birthMonth, birthYear;
if(date instanceof Date) {
birthDate = date.getDate();
birthMonth = date.getMonth();
birthYear = date.getFullYear();
}
else{
var completeBirthDate = date.split(/[/.-]/) ;
birthDate = parseInt( completeBirthDate[0]);
birthMonth = parseInt(completeBirthDate[1]);
birthYear= parseInt(completeBirthDate[2]);
if(!ValidateDate(birthDate,birthMonth,birthYear)) {
return age;
}
}
//Calculate Actual Age
if(birthYear <= todayYear)
{
age = (todayYear - birthYear);
if(birthMonth <= todayMonth)
{
if(birthMonth == todayMonth){
if(birthDate < todayDate && birthDate !== todayDate)
{
age--;
}
}
}
else{
age--;
}
}
}
return age;
}
function ValidateDate(birthDate,birthMonth, birthYear)
{
if(!(isNaN(birthDate) || isNaN(birthMonth) || isNaN(birthYear)))
{
var maximumNumberOfDaysInMonth =getValidBirthDate(birthMonth, birthYear);
if(birthDate !== 0 && birthDate <= maximumNumberOfDaysInMonth)
{
return true;
}
}
return false;
}
function getValidBirthDate(birthMonth, birthYear)
{
var numDays = 0;
switch (birthMonth) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
numDays = 30;
break;
case 2:
if (((birthYear % 4 == 0) &&
!(birthYear % 100 == 0))
|| (birthYear % 400 == 0))
numDays = 29;
else
numDays = 28;
break;
}
return numDays;
}
module.exports = calculateAge