if (cond == 1)
{
if (dt1_month==dt2_month)
{
if (dt1_date>dt2_date)
{
alert("Error Date Field...From date should not be Greater than To Date");
return false;
}
if (dt1_year > dt2_year)
{
alert("Error Date Field...From Year should not be Greater than To Year");
return false;
}
}
else if (dt1_month > dt2_month)
{
if (dt1_year == dt2_year)
{
alert("Error Date Field...From Month should not be Greater than To Month");
return false;
}
}
else if (dt1_year > dt2_year)
{
alert("Error Date Field...From Year should not be Greater than Current Year");
return false;
}
} //cond==1
return true;
}
Here the parameter cond is for Greater Than or Less than (if cond=1 then Greater than if 2 than Less than).
I am not 100% sure of what you need, but from your code I deduct, that the to date has to be at least 1 day later than the from date. If so, please try this:
PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function dateCompare(dt1, dt2){
var dt1_dt = Date.parse(dt1.value);
var dt2_dt = Date.parse(dt2.value);
if (!dt1_dt){
alert('The entry in the from field is not a valid date');
dt1.focus();
return false;
}
else if (!dt2_dt){
alert('The entry in the to field is not a valid date');
dt2.focus();
return false;
}
else if (dt1_dt >= dt2_dt) {
alert("To date should be greater than From date.");
dt2.focus();
return false;
}
else{
alert('The difference between the two dates you entered is: '+(dt2_dt-dt1_dt)/86400000+' day(s).');
return true;
}
}
//-->
</script>
</head>
<body>
<form>
from: <input type="text" name="date1"><br>
to: <input type="text" name="date2"><br>
<input type="button" onClick="dateCompare(document.forms[0].date1,document.forms[0].date2)" value="Compare"><br>
</form>
</body>
</html>
I would suggest to work with dropdowns to select days, months and years for the two dates instead of text inputs. That would also avoid user errors (February 30th etc.).
Bookmarks