I am trying to compare dated in typescript/angular 4. In my case, I assigned system date into a variable named 'today' and database date assigned into a variable named 'dateToBeCheckOut'. Then I compared dates using if statement, In my case I expected to get the bookings which having checkout date is less than(before) today's date. But I get unexpected result instead of getting the expected result. I attached my code bellow and expect someone's help for understanding why it happens.
Here is the code
for(let k = 0; k < this.bookingDetailsArrayRealObject.length; k++){
let dateCheckOut = this.bookingDetailsArrayRealObject[k].package.chackout;
let dateToBeCheckOut = new Date(dateCheckOut);
let today = new Date(Date.parse(Date()));
//let today_test = new Date();
if(this.bookingDetailsArrayRealObject[k].status){
if(dateToBeCheckOut.toDateString() < today.toDateString()){
this.bookingIdsToBeUpdated.push(this.bookingDetailsArrayRealObject[k]._id);
window.alert('true');
console.log(today.toDateString());
console.log(dateToBeCheckOut.toDateString());
console.log(this.bookingDetailsArrayRealObject[k]);
}
}
}
In console result, the 3rd one is unexpected according to the if statement. Any suggestion would be appreciated.