hello temp11
hello temp11

Reputation: 141

Comparing 2 dates in Javascript

I am just comparing 2 dates.

C#

  string args = string.Format("'{0}','{1}'
                            ,(DateTime)System.Data.SqlTypes.SqlDateTime.MaxValue
                            , DateTime.Now);

        add.OnClientClick = String.Format("JSFunctn1({0}); return false;", args);

Js File:

JSFunctn1(maxDate, currentDate)
{
alert(maxDate);
alert(currentDate); // Both Dates displayed properly

if (currentDate >= maxDate) {
        alert("error"); //IT Comes here
    }

Can anyone tell me where am I wrong, It should not come in the loop, because max Date is 12/31/9999 12:00:00 AM

Upvotes: 0

Views: 56

Answers (1)

anarchocurious
anarchocurious

Reputation: 600

I suspect the problem is that you are comparing strings rather than dates.

Try this on the client:

if (Date.parse(currentDate) >= Date.parse(maxDate)) {
  alert('error');
}

Upvotes: 1

Related Questions