I have a React native application with events where I have event dates in ISO format. I need to work with events on a yearly repeating basis. Events include dates like birthdays and also yearly events. I need to display upcoming events for the rest of the current year then display events for the next year. How to work with events without a year and sort by current date?
events.sort((a, b) => {
const today = new Date();
const currentYear = today.getFullYear();
const dateA = new Date(a.date);
dateA.setFullYear(currentYear);
const dateB = new Date(b.date);
dateB.setFullYear(currentYear);
return dateA - dateB;
})
.filter(event => {
const today = new Date();
const currentYear = today.getFullYear();
const eventDate = new Date(event.date);
eventDate.setFullYear(currentYear);
return eventDate > today;
})
.splice(0, 5)
.map(event => {
...