Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

date comparison I am really stuck with

Status
Not open for further replies.

ianjenn

Programmer
Sep 25, 2001
14
GB
Can anyone give me a clue as to why this isn't working, I want to compare the date entered in my form to 1/4/06 I am using
onsubmit="return minus_Validator(this)" to call up the check

<SCRIPT LANGUAGE=JavaScript>

function minus_validator(theform)
{
var start_date =new Date("1/4/2006");
if (theform.datefrom.getTime() > start_date.getTime())
{
alert("Date is greater than 1st April 2006");
return (false);
}
return (true);
}

</script>

any help greatly appreciated
 

What happens when you run the code? Any errors? What date are you passing in? Does anything happen?

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
it just doesn't work, no matter what date I enter. the error appears to be with

if (theform.datefrom.getTime() > start_date.getTime())

but I can't see anything wrong with it. so maybe something else
 
ianjenn,

When you read theform.datefrom, it is a string, no more. You've to convert it to a date object before comparing data. Something like this.
[tt]
function minus_Validator(theform)
{
var start_date =new Date("4/1/2006");
var enter_date=new Date(theform.datefrom.value)
if (isNaN(enter_date.getTime())) {
alert("Enter a valid date");
return false;
} else {
if (enter_date.getTime() > start_date.getTime()) {
alert("Date is greater than 1st April 2006");
return false;
}
}
return (true);
}
[/tt]
regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top