I'm using a filter helper to create a new array of items that match today's date. I've confirmed that the two comparisons are both a string with a length of 10, yet they're not being recognized as a match when compared in a filter.
I know it has to do with the new Date() method because the first comparison works when I test it against a string of the date while the new Date method does not.
if(chosenDate === 'today') {
// this does not work even though both values are equal
const scheduled = this.props.scheduled.filter(event => event.scheduled_at.substring(0, 10) === new Date(), 'yyyy-MM-dd');
// this works
const scheduled = this.props.scheduled.filter(event => event.scheduled_at.substring(0, 10) === '2019-11-14');
// this does not work
const scheduled = this.props.scheduled.filter(event => '2019-11-14' === new Date(), 'yyyy-MM-dd');
console.log(scheduled)
}
console.log(this.props.scheduled[0].scheduled_at.substring(0, 10));
console.log(dateFnsFormat(new Date(), 'yyyy-MM-dd'));
Why is new dates string not comparing equally?
It seems like you meant
const scheduled = this.props.scheduled.filter(event =>
event.scheduled_at.substring(0, 10) === dateFnsFormat(new Date(), 'yyyy-MM-dd')
);
but wrote
const scheduled = this.props.scheduled.filter(event =>
event.scheduled_at.substring(0, 10) === new Date()
, 'yyyy-MM-dd'
);