shanuragu
01-17-2004, 12:18 AM
Hi
How can I compare two dates (text box values ie, from dt & to Date) using javascript.
Here is the script which I have used to compare two dates which I feel is not very feasibel.
function dateCompare(dt1, dt2, cond)
{
var dt1String, dt1_dt, dt1_date, dt1_month, dt1_year;
var dt2String, dt2_dt, dt2_date, dt2_month, dt2_year;
dt1String = dt1;
dt1_dt = new Date(dt1String.replace(/[-]/g,""));
dt1_date = dt1_dt.getDate();
dt1_month = dt1_dt.getMonth()+1;
dt1_year = dt1_dt.getYear();
dt2String = dt2;
dt2_dt = new Date(dt2String.replace(/[-]/g,""));
dt2_date = dt2_dt.getDate();
dt2_month = dt2_dt.getMonth()+1;
dt2_year = dt2_dt.getYear();
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).
Urgent please help.
shara
How can I compare two dates (text box values ie, from dt & to Date) using javascript.
Here is the script which I have used to compare two dates which I feel is not very feasibel.
function dateCompare(dt1, dt2, cond)
{
var dt1String, dt1_dt, dt1_date, dt1_month, dt1_year;
var dt2String, dt2_dt, dt2_date, dt2_month, dt2_year;
dt1String = dt1;
dt1_dt = new Date(dt1String.replace(/[-]/g,""));
dt1_date = dt1_dt.getDate();
dt1_month = dt1_dt.getMonth()+1;
dt1_year = dt1_dt.getYear();
dt2String = dt2;
dt2_dt = new Date(dt2String.replace(/[-]/g,""));
dt2_date = dt2_dt.getDate();
dt2_month = dt2_dt.getMonth()+1;
dt2_year = dt2_dt.getYear();
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).
Urgent please help.
shara