This is a discussion on "Days between 2 Dates" within the JavaScript Forum section. This forum, and the thread "Days between 2 Dates are both part of the Program Your Website category.
|
|
|
|
|
![]() |
||
Days between 2 Dates
|
||
| Notices |
![]() |
|
|
LinkBack | Thread Tools |
|
|||
|
Days between 2 Dates
Hi.
I have found the below javascript code which sucessfully works out the numbers of days between two dates. The only problem is that the only way it works is if the dates are writen into the page then saved etc... What i want to happen is for it to be able to work with text boxes, and it give the number of days in a text box. Any help would be brilliant Thanks Aaron URL to the code: http://javascript.internet.com/time-...two-dates.html 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 :: http://javascript.internet.com Created by: JavaScript Demos :: http://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="http://javascriptsource.com">The JavaScript Source</a></font> </div><p> Thanks alot |
|
|
|
|||
|
Re: Days between 2 Dates
Create your form with text input fields.
Give each of the fields a unique id. Have a button to start the calculation. Your function needs lines like:
|
![]() |
| Tags |
| dates, javascript |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Web Design in Seven Days | mikerot | Starting Out | 6 | Nov 20th, 2007 02:19 |
| total days calculation | melisa | JavaScript Forum | 0 | May 25th, 2007 01:17 |
| why iv been away for 3 days | bruno89 | Webforumz Cafe | 11 | May 24th, 2007 15:07 |
| Please help, i have 5 days till my project is due | moneypammy | Flash & Multimedia Forum | 5 | May 2nd, 2007 22:14 |
| 2 days to make, too ambitious? | AlreadyInUse | Web Page Design | 3 | Apr 30th, 2007 22:31 |