PDA

View Full Version : Calculate No. of month based on start date and end date ?

ntc2000
12-06-2006, 03:35 AM
Hi,

How can I calculate the number of month based on given start date and end date?

Let said, the start date and end date is based on yyyyMMdd,
End date = 20060312 End Date = 20060215
Start date = 20060111 or Start Date = 20051212

Number of month = 3 month(ceiling it)

i can get the number of day and number of weeks, but i m stuck in number of month coz it's due to 28,29,30,31 days and leap year.

Any Solution??? Help... I am new in javascript....

Thank...

brandonH
12-06-2006, 10:09 AM
this shoud do the trick.

<html>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

<script LANGUAGE="javascript">
startdate=new Date();
enddate=new Date();
startdate.setFullYear(2002,0,12);
enddate.setFullYear(2006,11,15);
st=startdate.valueOf();
en=enddate.valueOf();
diff=en-st-(-0);
diffdate=new Date();
diffdate.setTime(diff);
yeardiff=enddate.getFullYear()-startdate.getFullYear();
monthdiff=diffdate.getMonth();
daydiff=enddate.getDate()-startdate.getDate();

alert("the difference between "+startdate+" and "+enddate+"
is:\n"+yeardiff+" year(s), "+monthdiff+" month(s), "+daydiff+" day(s)");

</script>