PDA

View Full Version : Help with dates


damon2003
07-11-2003, 07:58 AM
Hi,
I have a date textfield that takes a date in the form dd/mm/yyyy.
I want to compare this date with the current date using javascript. i.e. the number of days between these 2 dates. I know how to get the current date using a javascript date function but do not know how to compare the two 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

BaldEagle
01-26-2005, 08:23 PM
You could try something like this:

function GetTimeTest(testdate)
{
var d, s, t;
var MinMilli = 1000 * 60;
var HrMilli = MinMilli * 60;
var DyMilli = HrMilli * 24;
d = new Date();
t = Date.parse(testdate);
s = "There are "
s += Math.round(Math.abs(t / DyMilli)) + " days "
s += "between " + testdate + " and 1/1/70";
return(s);
}

the parse method returns the time in milliseconds from the date and 1/1/70 so you will have to modify it to accept two parameters as well as do the math to find the difference from date1 to date2. Vbscript has DateDiff function but don't believe javascript does. Someone else know?

BaldEagle