As the title says I am struggling to get this thing to work. I have looked at the similar posts on here and many reference MomentJS, which on their own site they advise against using it now.
const today = new Date().toLocaleDateString('en-gb')
return loading ? <Spinner /> :
<Fragment>
<div className="calendar_events">
<p className="ce_title">Upcoming Events</p>
{
appointments.map(appointment => {
let date = new Date(appointment.date).toLocaleDateString('en-gb')
{console.log('date', typeof date)}
{console.log('today', typeof today)}
if(today == date) {
<Appointment key={appointment._id} appointment={appointment} date={date}/>
}
})
}
</div>
</div>
</Fragment>
I have tried with moment but no success. I am now trying vanilla JS, when I console.log each date, today and an appointment date, they're the same. when I console.log the typeof these, they're both strings, but my if statement never runs.
I only want to call the component for appointments that match todays date or any other given date on the calendar.
i realise I am recreating the wheel here, but this is more about learning and taking my mind off things than the actual end result.
I would be happy for any guidance as to what on Earth I am missing.
Console output for typeof
date string Calendar.js:39
today string Calendar.js:40
Console output for each variable
date 29/11/20 Calendar.js:39
today 29/11/20 Calendar.js:40 *2 as there are two demo appointments on this date.
I have also tried the == and === operators but nada.
Please send help, my sanity is lost.
Edit
I managed to get it working, I think with the help of @Uzair Ashraf.
It even appears to work more succinctly with the following code
{
appointments.map(appointment => {
let date = new Date(appointment.date).toLocaleDateString('en-gb')
if(today === date) {
return <Appointment key={appointment._id} appointment={appointment} />
}
})
}