I'm struggling with this question? How can I find next birthday Date from my list of date ? I got
const myArray = [
{
name: 'Joe Blow',
date: 'Wednesday, November 25, 1992'
},
{
name: 'Sam lol',
date: 'Thursday, April 16, 1992'
},
{
name: 'Eva lol',
date: 'Thursday, February 26, 1991'
}
];
myArray.sort(function compare(a, b) {
const dateA = new Date(a.date);
const dateB = new Date(b.date);
return dateA.getUTCDate() - dateB.getUTCDate();
});
// console.log(this.usersBirthdayDate);
console.log(myArray);
but I got Sam first cause he got 1992.. But I would like to display Thursday, February 26, 1991 first
Updated my solution :
sortByDateNoYear(adate, bdate) {
let results;
const lhdate = moment(adate.birthdayDate);
const rhdate = moment(bdate.birthdayDate);
results =
lhdate.month() > rhdate.month()
? 1
: lhdate.month() < rhdate.month()
? -1
: 0;
if (results === 0) {
results =
lhdate.date() > rhdate.date()
? 1
: lhdate.date() < rhdate.date()
? -1
: 0;
}
return results;
}
[array].sort(this.sortByDateNoYear);