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.

Community
  • 1
  • 1
TomFarCry
  • 33
  • 8

2 Answers2

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
Nishant123
  • 1,968
  • 2
  • 26
  • 44
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

Community
  • 1
  • 1
Ranjit Singh
  • 3,715
  • 1
  • 21
  • 35