View Full Version : number of days between 2 dates

04-11-2003, 03:21 PM
I have a form where people can request a project from me. On the form are two fields, due date and request date. I ask for 10 business days to turn any project around. I'd like to be able to...

1. first calculate the number of BUSINESS (monday - Friday) days there are between the due date and request date.

2. If that number is below 10, prompt a message basically saying that I may not be able to get their project done.

3. Ask them to confirm their order even with this warning message.

4. If they say Yes to the confirmation, then the form continues on. If they say No, then the form submission stops and I do not receive the submission.

Any ideas on how I can do this? If you could provide the calculation code, I'd appreciate it!


04-11-2003, 03:53 PM
First, go here, a good Date object reference

Now, here's the process I'd take

1) read input fields and create date objects from their values
2) Verify validity of dates
3) Loop, incrementing the older date by one day each iteration
3a) If day is a weekday, add to a counter
3b) Exit loop when older date is eqal to new date
4) return result

I'll let you have a stab at it first.

04-11-2003, 09:55 PM
thanks Beetle, I'll see if I can do it based on what you said. if not, I'll be back!!

Mr J
04-12-2003, 11:35 PM
<TITLE>Document Title</TITLE>

function calc_date2() {

var fromArray = from.split('/');
fromdate = new Date( fromArray[2], fromArray[1]-1, fromArray[0]);

var toArray = to.split('/');
todate = new Date( toArray[2], toArray[1]-1, toArray[0]);


<P>Calculate the days from start date to end date.
<form name="f3">
<P><table border=1>
<tr><td>Start Date</td><td>End Date</td><td>Number of days</td></tr>
<tr><td><input type="text" name="date_from" size="10" value="25/09/2002"></td>
<td><input type="text" name="date_to" size="10" value="26/09/2002"></td>
<td align="center"><input type="text" name="daynums" size="4" maxlength="4" value=""></td></tr>
<tr><td colspan=3 align=center>
<input type="button" value="calculate" onclick="calc_date2()">