How do I calculate number of days from two dates in a form

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a form which asks a user for a start and finish date and I want to be
able to accurately calculate the number of days between those dates.
 
Hi SCG,

The answer depends upon what you plan to use for the page, HTML, ASP, or ASP.Net

In HTML you'll need to use either CGI or JavaScript at the client the latter being the easiest.

<script>
function dateDiff(startDate,stopDate) {
var startDate = new Date(startDate);
var stopDate = new Date(stopDate);
var days = (stopDate-startDate) / 1000/60/60/24;
alert(days);

}
</script>

<form>
<input type="text" value="1/1/2006" name="startDate" >
<input type="text" value="1/20/2006" name="stopDate" >
<input type=button onclick="dateDiff(this.form.startDate.value,this.form.stopDate.value);" value="click" >
</form>

The above will display an alert box with the number of days.
 
Hey Mike, thanks for this. I'm using plain old HTML. Tried just inserting
the code you wrote but just get an alert box with NaN in it??? Must be doing
something wrong I guess.
 
Hi Mike

Got it working. The script you gave me just happened to be using the same
variable name I already set up for the StartDate! Thanks very much for the
help; now to get it into the exsiting code......

Regards SCG
 
Hi Mike

Sorry to bother you, but how can you get the script to work with a
dd/mm/yyyy date rather than a mm/dd/yyyy?
 
Back
Top