Click to See Complete Forum and Search --> : Help with comparing dates


damon2003
07-11-2003, 08:10 AM
Hi,
I have a date textfield that takes a date in the form dd/mm/yyyy. I obtain the current date using a Javascript function.

I want to compare these two dates using javascript. i.e. the number of days between these 2 dates.

It is not simply a case of putting the dates into two variables and subtracting them. They must need parsing first but dont know how to do this. Does anyone know how to do this?
thanks a lot

Khalid Ali
07-11-2003, 08:48 AM
I think the link below will help you do that

http://68.145.35.86/skills/javascripts/DateRangeSelector.html

damon2003
07-11-2003, 08:54 AM
thanks, will have a look at this to see if I can adapt it. Should just be able to hide the first menu and use it for current date.
Why is there a 'month' option in the section box - it actually gives a value back.

What is the diff measured in. The second one is days, is the measured in seconds?

thanks

Mr J
07-11-2003, 03:19 PM
Will this do?



<SCRIPT LANGUAGE="JavaScript">
<!--
function calc_date2() {
from=document.f3.date_from.value
to=document.f3.date_to.value

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]);

document.f3.daynums.value=(todate-fromdate)/86400000

}
//-->
</SCRIPT>
<P>Calculate the days from start date to end date.
<center>
<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()">
</td></tr></table>
</form>
</center>

Charles
07-11-2003, 04:04 PM
Since the Date constructor understands this date format we can shorten that quite a bit. And since the constructor also understands other formats as well, the user doesn't have to be limited to just one format.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {margin-right:1em; width:5em}
-->
</style>
<script type="text/javascript">
<!--
Date.ONE_SECOND = 1000;
Date.ONE_MINUTE = Date.ONE_SECOND * 60;
Date.ONE_HOUR = Date.ONE_MINUTE *60;
Date.ONE_DAY = Date.ONE_HOUR * 24;
// -->
</script>
<form onsubmit="this.diff.value = (new Date(this.stop.value) - new Date(this.start.value)) / Date.ONE_DAY; return false">
<div>
<label><input type="text" name="start">Start</label>
<label><input type="text" name="stop">Stop</label>
<hr>
<label><input type="text" name="diff" onfocus="this.blur()">Difference</label>
<button type="submit">Calculate</button>
</div>
</form>

damon2003
07-12-2003, 08:43 AM
Khalid, do you have the date-control.js and range-gui.js files?
thanks a lot

Charles
07-12-2003, 08:56 AM
You can find them at http://68.145.35.86/skills/javascripts/scripts/date-control.js and http://68.145.35.86/skills/javascripts/scripts/date-range-gui.js. Note however that he's simply duplicating a lot of stuff that's already built in to the JavaScript Date class and is using at least one depricated feature. You would do better to study how the Date class works. See http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/date.html.

damon2003
07-12-2003, 09:03 AM
Thanks a lot Charles,
I will study the documentation as do need to understand more