Click to See Complete Forum and Search --> : Compare two dates


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

Pittimann
01-17-2004, 04:45 AM
Hi!

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:

<!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>

Cheers - Pit

shanuragu
01-17-2004, 05:52 AM
Hi
Exactly !!
I have maintained 'dd-mmm-yyyy' date format for my project. When I use ur code to compare dates

var dt1_dt = Date.parse(dt1);
var dt2_dt = Date.parse(dt2);
are taking NaN values. Why??

To the date function I am just sending the text field value itself.

shara

Pittimann
01-17-2004, 06:09 AM
Hi!

Using the code I provided, the date format entered should be like:

'mm-dd-yyyy', 'mm/dd/yyyy', 'mmm-dd, yyyy', 'mmm/dd, yyyy'...

If you use mmm, the day has to follow and to be followed by a comma as a separtor before the year

Cheers - Pit

shanuragu
01-17-2004, 07:19 AM
How can I format 'mmm/dd/yyyy' in to 'mmm/dd, yyyy' format in Js

shara

Pittimann
01-17-2004, 07:28 AM
Hi!

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.).

If you like, I can provide a code for that...

Cheers - Pit