0

If I enter same date for startdate and enddate fields(ex: startdate is 2012/12/29 and enddate is also 2012/12/29) then the following if condition executes and return false in javascript.

var strtdate = document.getElementById("x_START_DATE").value;    //getting task startdate
var enddate = document.getElementById("x_END_DATE").value;    //getting task enddate
var task_StartDate = new Date(strtdate);
var task_EndDate = new Date(enddate);

if (task_StartDate.getTime() > task_EndDate.getTime())
{   
    window.scrollTo(0,0);
    alert("End date should not be less than start date.");
    return false;
}

and even if I check like this, else part is get executing.

if (task_StartDate.getTime() == task_EndDate.getTime())
{   
    //some stuff
}
else
{
    alert("End date should not be less than start date.");
    return false;
}

How to execute code when both dates are equal?

3
Contributors
6
Replies
7
Views
4 Years
Discussion Span
Last Post by venkyb47
0

Try just comparing the two variables of the dates.

var strtdate = document.getElementById("x_START_DATE").value; 
var enddate = document.getElementById("x_END_DATE").value;
if(strtdate >= enddate) {
    window.scrollTo(0,0);
    alert("End date should not be less than start date.");
    return false;
} else {
    alert("Good");
}
0

Are you sure that you created your date object correctly? I tested by plugging in the date string you gave and it worked.

var d1 = new Date("2012/12/29")
var d2 = new Date("2012/12/29")
if (d1.getTime()>d2.getTime()) { alert("d1:"+d1+"\n GREATER THAN\nd2:"+d2) }
else if (d1.getTime()==d2.getTime()) { alert("d1:"+d1+"\n EQUAL TO\nd2:"+d2) }
else { alert("d1:"+d1+"\n LESS THAN\nd2:"+d2) }

What you may do is to plug in alert(task_StartDate+"\n"+task_EndDate) at line 5 and see what the date you get.

0

Thanks Taywin for your reply.
I cannot compare directly as you mentioned. I need to get values from text fields. I tried like this

var d1 = new Date(document.getElementById("x_START_DATE").value);
var d2 = new Date(document.getElementById("x_END_DATE").value);
if (d1.getTime()>d2.getTime()) { alert("d1:"+d1+"\n GREATER THAN\nd2:"+d2); }
else if (d1.getTime()==d2.getTime()) { alert("d1:"+d1+"\n EQUAL TO\nd2:"+d2); }
else { alert("d1:"+d1+"\n LESS THAN\nd2:"+d2); }

and I entered 2012/12/29 for both fields. Now the output is

d1:Sat Dec 29 2012 05:30:00 GMT+0530 (India Standard Time)
LESS THAN
d2:Sun Dec 30 2012 00:00:00 GMT+0530 (India Standard Time)

This is not correct output.

0

Hmm.. I think it is fine. The d1 is the start time and the d2 is the end time. So your start time should always be less than or equal to the end time. Correct? If it is correct, you could plug in the error in the first if, remove the else if, and implement whatever you want to do inside the else.

0

Sorry Taywin. In previous post I showed you wrong output. Check this corrected one. I want to execute some code if dates are equal and some other code if dates are not equal.

var d1 = new Date(document.getElementById("x_START_DATE").value);
var d2 = new Date(document.getElementById("x_END_DATE").value);
if (d1.getTime()>d2.getTime()) { alert("d1:"+d1+"\n GREATER THAN\nd2:"+d2); }
else if (d1.getTime()==d2.getTime()) { alert("d1:"+d1+"\n EQUAL TO\nd2:"+d2); }
else { alert("d1:"+d1+"\n LESS THAN\nd2:"+d2); }

and I entered 2012/12/29 for both fields. Now the output according to your code is

d1:****Sat Dec 29 2012** 05:30:00** GMT+0530 (India Standard Time)
GREATER THAN
d2:Sat Dec 29 2012 00:00:00 GMT+0530 (India Standard Time)

It should show "EQUAL TO" statement right? Where could be the problem?

0

I understood the problem. I am getting Start date from db. It is a different format consider from end date.I was solved by formatting date like this.

var enddate = document.getElementById("x_END_DATE").value;    //getting task enddate
enddate=enddate.split("/");
var newEndDate=enddate[1]+"/"+enddate[2]+"/"+enddate[0];
var task_EndDate = new Date(newEndDate);

Thanks for support.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.