# Thread: calculate the diffrence of start and end date in months

1. ## calculate the diffrence of start and end date in months

Hi Guys,

I would like to develop somewhat similar to Outlook calendar. Input is going to be a start date and end date. I need to calculate the diffrence in months and display those many months.

For example, start date = 12/07/2004 end date = 11/07/2005. I need to display all the months from Dec 2004 till Nov 2005 along with week numbers and weekdays. So, far I'm able to calculate the diffrence and bale to display months. But, I'm not able to display proper weekdays associated with date.

Here is my function --

Code:
```var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var month_days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var week_days = new Array("Sun","Mon","Tues","Wed","Thurs","Fri","Sat");

function cal_diff()
{
var dt1 = new Date(2004,11,07);
var dt2 = new Date(2005,10,07);

var one_day = 1000*60*60*24;
var diff = (dt2.getTime()-dt1.getTime())/one_day;

var year = dt1.getFullYear();
var start_date = dt1.getDate();
var s_date = start_date;
var end_date = dt2.getDate();
var start_day = dt1.getDay();
var start_month = dt1.getMonth();
var end_month = dt2.getMonth();
var days_in_month = month_days[dt1.getMonth()] - start_date;

var output = "";
var day_cnt = start_day;
output += "<tr>";
var week_counter = 0;
while (diff > 0) {
week_counter++;
output += "<tr> <td colspan=\"8\" align=\"center\">"+months[start_month]+" - "+year+"</td> </tr>";
/*        output += "<tr>";
for (var d = 0; d < week_days.length; d++)
{
output += "<td align=\"center\"><span class=\"smallertxt\">"+ week_days[d] +"</span></td>";
}
output += "</tr>"; */
output += "<tr>";
output += "<td bgcolor=\"#E8F0F7\" align=\"center\">Week#" + week_counter + "</td>";
var counter = 0;
if( start_month == 1 && (year%4) == 0) {
month_days[start_month] = 29;
}
if (end_month == start_month)
{
month_days[start_month] = end_date;
}
for (var a = start_date; a <= month_days[start_month]; a++)
{
var n_day = new Date(a,start_month,year);
counter++;
if (counter <= 7){
output += "<td bgcolor=\"#E8F0F7\" align=\"center\"><a href=\"javascript:get_week('"+a+"','"+start_month+"','"+year+"')\">"+ a +"</a></td>";
} else {
counter = 0;
week_counter++;
output += "</tr> <tr>";
output += "<td bgcolor=\"#E8F0F7\" align=\"center\">Week#" + week_counter + "</td>";
counter++;
output += "<td bgcolor=\"#E8F0F7\" align=\"center\"><a href=\"javascript:get_week('"+a+"','"+start_month+"','"+year+"')\">"+ a +"</a></td>";
}
}
output += "</tr> </table> </td>";
diff = diff - days_in_month;
start_month = start_month + 1;
if (start_month > (months.length - 1))
{ start_month = 0; year = year + 1;}
start_date = 1;
days_in_month = month_days[start_month] - start_date;
}
output += "</tr> </table>";
document.getElementById('months').innerHTML = output;
}```
Can somebody help me with this?

2. I guess using php, you can do this pretty easily.

Try php.net/date - that should give you the answer.

3. But I am using it in a JSP.

4. Yeah, server side languages have a strong library generally and that helps ...

5. first you need an ary

'Sun','Mo','Tu','We','Th','Fr','Sa','Sun','Mo','Tu','We','Th','Fr','Sa','Sun'

then find the first day of the month 0 to 6

then another row for the day names and fill the row with the day ary above aligning the start field with the first day number.

alternatly I can produce one modifying one of my own calendar scripts

see

http://www.vicsjavascripts.org.uk/Ca...endarInput.htm

6. Thanks Vic,

7. a couple of lines to assist in days in month and first day of month

function zxcDinMFirstD(zxcyr,zxcm){
zxcDinM=new Date(zxcyr,zxcm,1,-1).getDate();
zxcFirstD=new Date(zxcyr,zxcm-1,1).getDay();
}

8. Thanks Vic,

Million thanks to you

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•