Days between 2 Dates

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.



Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Apr 17th, 2007, 16:19
New Member
Join Date: Apr 2007
Location: UK
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
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
Reply With Quote

  #2 (permalink)  
Old Apr 19th, 2007, 19:55
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
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:
Code: Select all
date1 = document.getElementById('id').value
Reply With Quote
Reply

Tags
dates, javascript

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT. The time now is 22:44.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43