I'm currently working on a comparison of dates in typescript/angular 4. In my scenario, I've stored the system date in a variable called 'today' and the database date in a variable named 'dateToBeCheckOut'. My goal was to filter out bookings where the checkout date is earlier than today's date. However, I'm encountering unexpected results instead of the desired output. Below is the code snippet I used, and I'm seeking assistance in understanding why this discrepancy occurs.
Here's 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]);
}
}
}
Click here to view the booking object retrieved from the database
The third line in the console result is not as expected based on the if statement conditions. Any suggestions or insights would be greatly appreciated.