Forums

This topic is locked

Days between dates

Posted 17 Apr 2007 00:25:16
1
has voted
17 Apr 2007 00:25:16 aaron bird posted:
Hi. I have found this piece of code to work out the number of days between two dates.

However, how could i modify this so that it finds the days between two dates that were entered in a text box as opposed to simply text. Thanks

Aaron

THE CODE


<!-- Paste this code into an external JavaScript file named: dateDiff.js -->

/* This script and many more are available free online at
The JavaScript Source :: javascript.internet.com
Created by: JavaScript Demos :: www.javascript-demos.com/ */

function calcDays(){
var date1 = document.getElementById('d1').lastChild.data;
var date2 = document.getElementById('d2').lastChild.data;
date1 = date1.split("-";
date2 = date2.split("-";
var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]);
var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]);
var daysApart = Math.abs(Math.round((sDate-eDate)/86400000));
document.getElementById('diffDays').lastChild.data = daysApart;
}

onload=calcDays;


<!-- Paste this code into the HEAD section of your HTML document.
You may need to change the path of the file. -->

<script type="text/javascript" src="dateDiff.js"></script>


<!-- Paste this code into the BODY section of your HTML document -->

<table width='220' border='1' cellspacing='0' cellpadding='5' align="center">
<tr>
<td>Starting<br>Date</td>
<td>Ending<br>Date</td>
<td>Total<br>Days</td>
</tr><tr>
<td id='d1'>01-01-2006</td>
<td id='d2'>08-05-2006</td>
<td id='diffDays' align='center'> </td>
</tr>
</table>
<p><div align="center">
<font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
by <a href="javascriptsource.com">The JavaScript Source</a></font>
</div><p>

Replies

Replied 17 Apr 2007 00:26:26
17 Apr 2007 00:26:26 aaron bird replied:
Please visit here to see it working. You will see what i mean about the dates being text as opposed to a text box value.

javascript.internet.com/time-date/days-between-two-dates.html

Reply to this topic