0

I would like to compare the given date in the below format in JaveScript. I have tried the following,

Thu May 19 2016 00:00:00 GMT+0530 (India Standard Time)

Thu May 20 2016 00:00:00 GMT+0530 (India Standard Time)

var ExpiryDate = userAccount.ExpiryDate();
var datetoday = new Date();
var Expired = (DateTime.Compare(ExpiryDate, datetoday) == -1 ) ? true : false; 
//if expiry date is less than today date then var expired should be true

But didn't worked. I could not compare those two dates. It results in un handled exception. Is there any other way to do this date comparison in JaveScript ?

I have referred the following answers in SO but they are in different date format. So that I have raised this question,

  1. javascript compare two dates and throw an alert
  2. Javascript comparing two dates has wrong result
  3. Compare two dates in JavaScript
  4. Javascript compare two dates to get a difference

Any suggestion would be helpful.

4
  • 1
    Possible duplicate of JavaScript Date Object Comparison Commented May 20, 2016 at 10:13
  • If they are date objects, you can use d1 < d2 or d1 > d2 Commented May 20, 2016 at 10:13
  • what is DateTime object? Commented May 20, 2016 at 10:16
  • You can use getTime() method to turn Date objects to timestamps. ExpiryDate.getTime() == datetoday.getTime() Commented May 20, 2016 at 10:45

2 Answers 2

1
var date = new Date(); 
//# => Fri May 20 2016 16:09:43 GMT+0530 (India Standard Time)


var date2 = new Date();
date2.setDate(date.getDate() - 1); 
//# => Thu May 19 2016 16:09:43 GMT+0530 (India Standard Time)

date > date2 //# => true
Sign up to request clarification or add additional context in comments.

Comments

0

use getTime()

var date1 = (new Date("20 May 2016")).getTime();
var date2 = (new Date("19 May 2016")).getTime();

date1>date2

You will find some good method here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.