Saturday, August 2, 2008

Calculate Birthdate From Given Age

As there was a requirement in my project to calculate birthdate from age(Age in Year,Age in Month,Age in days) given.

I have created one function in javascript which will give the birthdate in dd/mm/yyyy format.
The function is as given below.



1 function CalculateBirthDateFromAge(AgeInYear,AgeInMonth,AgeInDays)
2
{
4 var currentDate=new
Date();
5 var getCMonth=(currentDate.getMonth()); //Get current month

6 var getCYear=currentDate.getFullYear(); //Get current Year
7 var getCDate=currentDate.getDate(); //Get Current Date
8 var getAgeInDays =
AgeInDays;
9 var getAgeInMonth =
AgeInMonth;
10 var getAgeInYear =
AgeInYear;
12 var yd =
getCYear;
13 var md = getCMonth +1
;
14 var mLength ="0"
;
15 var mon=0
;
16 var mb=0
;
17 var yb=0
;
18 var
db
19 if(getAgeInDays!=""
)
20
{
21 db = getCDate-
getAgeInDays;
22
var g31Day=new Array(1,3,5,7,8,10,12);
26 var g30Day=new Array(2,4,6,9,11
);
27 var gMonth=12
;
28 var gDay=0
;
29 var getCurentMont=(getCMonth +1 );//get current month

30 var getCurrentYear=getCYear;//get current year
31
for(var i=0;i < g31Day.length;i++)
33
{
34 if (g31Day[i]==
(getCurentMont))
35
{
36 gDay=31
;
37 break
;
38 }

39
}

41 if(gDay==0
)
42
{
43 for(var i=0;i < g30Day.length; i++
)
44
{
45
if(g30Day[i] == getCurentMont)
47
{
48 if(getCurentMont==2
)
49
{
50 if(getCurrentYear % 4 == 0 (getCurrentYear % 100 != 0 && getCurrentYear % 4 == 0
))
51
{
52 gDay=29
;
53 break
;
54 }

55 else

56
{
57 gDay=28
;
58 break
;
59 }

61 }

62 else

63
{
64 gDay=30
;
65 break
;
66 }

67 }

68 }

69 }

73 if( db <= 0
)
74
{
76 db=db+
parseInt(gDay);
77 }

80}

81else

82db=01
;
85 if(getAgeInMonth!=""
)
86 mb = md -getAgeInMonth;//month

87 else

88 mb = 01;//month

89
if(mb <= 0)
91
{
92 yd--
;
93 mb=mb+12
;
94 }

96if(getAgeInYear!=""
)
97 yb = yd -
getAgeInYear;
98 else

99 yb =
getCYear;
100 alert("My BirthDate Is "+db+"/"+mb+"/"+
yb);
101
}



You have to pass the Age in years,age in month and age in days as Parameters to the given function.

No comments: