-
Notifications
You must be signed in to change notification settings - Fork 0
/
ageCalc.js
57 lines (45 loc) · 1.25 KB
/
ageCalc.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
function getAge(birth, death)
{
var now;
if (death == null){
now = new Date();
}
else {
now = death;
}
var aSecond = 1000;
var aMinute = aSecond * 60;
var aHour = aMinute * 60;
var aDay = aHour * 24;
var aWeek = aDay * 7;
var aMonth = aDay * 30;
var age = now.getTime() - birth.getTime();
if (age < 0) {
return "not born yet"
}
var years = (new Date(now.getTime() - aMonth* (birth.getMonth()) )).getFullYear()
- (new Date(birth.getTime() - aMonth* (birth.getMonth()) )).getFullYear();
offsetNow = (new Date(now.getTime() - aDay* (birth.getDate() -1) ));
offsetBirth = (new Date(birth.getTime() - aDay* (birth.getDate() -1) ));
if(years > 1){
months = years*12 + ( offsetNow.getMonth() - offsetBirth.getMonth()) ;
}else{
months = (now.getFullYear() - birth.getFullYear())*12 + ( offsetNow.getMonth() - offsetBirth.getMonth()) ;
}
agestr="";
if (months < 12){
days = Math.floor(age / aDay);
if(days <30)
{
agestr = days + 'd';
}
else
{
agestr = months + 'mo';
}
}else{
agestr = agestr + years;
agestr = agestr + "y";
}
return agestr;
}