1

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);
Seysay
  • 13
  • 6
  • 1
    Just a heads up -> `new Date(a.date);` using `'Wednesday, November 25, 1992` is none standard, it might work in most cases, but it's down to browser interpretation of the date string. – Keith Feb 19 '20 at 14:16
  • [Please don't add "solved" to your title or question](https://meta.stackexchange.com/a/116105/248627). Instead, [mark an answer correct by clicking on the checkmark](https://meta.stackexchange.com/q/5234/248627). You can also [add your own answer](https://stackoverflow.com/help/self-answer) and accept it if none of the ones you received solved your problem. – ChrisGPT was on strike Feb 21 '20 at 02:27

3 Answers3

4

You are currently sorting by the day of month by using .getUTCDate().

Instead, use .getTime() in your comparison.

From MDN

The getTime() method returns the number of milliseconds* since the Unix Epoch.

myArray.sort(function compare(a, b) {
  const dateA = new Date(a.date);
  const dateB = new Date(b.date);
  return dateA.getTime() - dateB.getTime();
});

Also, if you're using Angular, you're safe to use arrow functions

myArray.sort((a, b) => {
  const dateA = new Date(a.date);
  const dateB = new Date(b.date);
  return dateA.getTime() - dateB.getTime();
});
Kurt Hamilton
  • 12,490
  • 1
  • 24
  • 40
  • 3
    The method `.getTime()` is functionally equivalent to `.valueOf()` – rafaelcastrocouto Feb 19 '20 at 14:15
  • This work on this way but if you got some one with 'Thursday, February 26, 1998' it will not longuer working as except – Seysay Feb 20 '20 at 13:21
  • @Seysay Correct. This answer is about how to sort using dates, not how to convert a string into a date. – Kurt Hamilton Feb 20 '20 at 13:24
  • That why if I add in my array like : { name: 'Anna', birthdayDate: 'Thursday, February 26, 1998' } It'will not longuer working, I mean the function sort – Seysay Feb 20 '20 at 13:27
0

you need to set the same year for all date and next year for birthday already passed. Then you can compare

myArray.sort(function compare(a, b) {
          const now = new Date();
          const dateA = new Date(a.date);
          dateA.setYear(now.getFullYear());
          if (dataA.getTime() - now.getTime() < 0) { 
             dateA.setYear(now.getFullYear() + 1)
          }
          const dateB = new Date(b.date);
          dateA.setYear(new Date().getFullYear());
          dateA.setYear(now.getFullYear());
          if (dataB.getTime() - now.getTime() < 0) { 
             dateB.setYear(now.getFullYear() + 1)
          }
          return dateA.getTime() - dateB.getTime();
        });
0

You can try like this

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((a, b) => {
  const ad = new Date(a.date).getTime();
  const bd = new Date(b.date).getTime();
  return ad - bd;
});
// console.log(this.usersBirthdayDate);
console.log(myArray);
Raj Kumar
  • 839
  • 8
  • 13