0

I suppose my question is simple but I am not finding the solution, How to compare a date with the previous or minor or the next date?

At the moment I have this code:

if(newDate > oldDate || newDate < oldDate){}

And its working but Its nasty, I tried newDate != oldDate but it doesn´t work for me, What am I doing wrong?

inane
  • 626
  • 10
  • 26

2 Answers2

1

I think you want to check this answer.

Comparing 2 JavaScript dates.

Compare two dates with JavaScript

Community
  • 1
  • 1
0

Use the date timestamp to compare the dates, like this:-

var oldDate = new Date('2013-05-23');
var newDate = new Date('2013-05-24');

if(newDate.getTime() > oldDate.getTime()) {
 console.log("Old date is the past date");
}

Note:- Higher timestamp value(number) denotes latest date. So timestamp of yesterday is less than the timestamp of today.

Anand Gupta
  • 5,640
  • 3
  • 27
  • 37
Sujata Chanda
  • 3,355
  • 2
  • 21
  • 35