0

I need to compare the startDate and endDate and if startDate>endDate then i need to take the starting date of the month of endDate as startDate suppose my startDate is "startDate": "2016-05-30T00:00:00.000Z" endDate is "endDate": "2016-06-05T00:00:00.000Z" then when compare need to get true but i am getting false

Code:

 var c=(new Date(nestedObj.startDate)).getDate();
  console.log("startdate"+" "+c);
  var d=(new Date(nestedObj.endDate)).getDate();
  console.log("endtdate"+" "+d);
  console.log(c>d);

As you can see in the below snippet

enter image description here

Any help would be appreciated.

Shikha thakur
  • 1,269
  • 13
  • 34

3 Answers3

2

try this

var c = new Date(nestedObj.startDate)
console.log("startdate"+" "+c);
var d=new Date(nestedObj.endDate);
console.log("endtdate"+" "+d);
console.log(c>d);
Piotr Białek
  • 2,569
  • 1
  • 17
  • 26
1

You should use getTime() function rather than getDate(). getTime() return number(epoch time) that will easy to compare. Here is code that may help you

var c = new Date(nestedObj.startDate).getTime();
console.log("startdate " + c);
var d = new Date(nestedObj.endDate).getTime();
console.log("endtdate " + d);
console.log(c>d);

Even you can directly compare Date object as following

var c = new Date(nestedObj.startDate);
console.log("startdate " + c);
var d = new Date(nestedObj.endDate);
console.log("endtdate " + d);
console.log(c>d);
Arif Khan
  • 5,039
  • 2
  • 16
  • 27
1

Why not going with moment.js

Look at moment(date1).isAfter(date2); method.

It's easy to do all manipulation of Date/Time

if(moment(endDate).isAfter(startDate)){
//Do whatever you want
}
abdulbarik
  • 6,101
  • 5
  • 38
  • 59