Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
Get the Second Highest Date in JavaScript/ES6

I have a problem getting the second highest date in ES6. I'm using moment.js too. Its supposed to be getting the id of 3.

const datas = [
    {
        id: 1,
        date: moment(String('Apple & Banana - 20072021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
    },
    {
        id: 2,
        date: moment(String('Apple & Oranges - 30082021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
    },
    {
        id: 3,
        date: moment(String('Lemon & Oranges - 30102021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
    },
    {
        id: 4,
        date: moment(String('Honeydew - 30112021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
    }
];

const secondLatestDate = new Date(datas.map(file => new Date(file.date)).sort().reverse()[1]);

const finalResult = datas.find(file => file.date.getTime() === secondLatestDate.getTime());

console.log(finalResult)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You should use custom sort function as:

datas.sort((a, b) => a.date - b.date)

There is no need to use find when you are reverseing the array and getting the index 1 from it.

Note: I deliberately change the order of the datas array

const datas = [{
    id: 1,
    date: moment(String('Apple & Banana - 20072021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
  },
  {
    id: 2,
    date: moment(String('Apple & Oranges - 30082021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
  },
  {
    id: 4,
    date: moment(String('Honeydew - 30112021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
  },
  {
    id: 3,
    date: moment(String('Lemon & Oranges - 30102021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
  }
];

const secondLatestDate = datas.sort((a, b) => a.date - b.date).reverse()[1];
console.log(secondLatestDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

or you can directly find the second largest after sort. There is no need to reverse the array

datas.sort((a, b) => a.date - b.date)[datas.length - 2]

const datas = [{
    id: 1,
    date: moment(
      String('Apple & Banana - 20072021').match(/[0-9]/g).join(''),
      'DDMMYYYY'
    ).toDate(),
  },
  {
    id: 2,
    date: moment(
      String('Apple & Oranges - 30082021').match(/[0-9]/g).join(''),
      'DDMMYYYY'
    ).toDate(),
  },
  {
    id: 4,
    date: moment(
      String('Honeydew - 30112021').match(/[0-9]/g).join(''),
      'DDMMYYYY'
    ).toDate(),
  },
  {
    id: 3,
    date: moment(
      String('Lemon & Oranges - 30102021').match(/[0-9]/g).join(''),
      'DDMMYYYY'
    ).toDate(),
  },
];

const secondLatestDate = datas.sort((a, b) => a.date - b.date)[datas.length - 2];
console.log(secondLatestDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

about 4 years ago · Juan Pablo Isaza Report

0

If you're at all concerned with time complexity you could do this in one 'n' by implementing a function that keeps track of both the highest and second highest values. Maybe something like this:


const datas = [{
    id: 1,
    date: moment(String('Apple & Banana - 20072021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
  },
  {
    id: 2,
    date: moment(String('Apple & Oranges - 30082021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
  },
  {
    id: 4,
    date: moment(String('Honeydew - 30112021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
  },
  {
    id: 3,
    date: moment(String('Lemon & Oranges - 30102021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
  }
]


function getSecondHighest(dates){
    let highest = 0;
    let secondHighest = 0;

    for(const val of dates){
        const currentDate = val.date
        if(currentDate > highest){
            secondHighest = highest;
            highest = currentDate;
        } else if(currentDate > secondHighest){
            secondHighest = currentDate
        } else {
            continue;
        }
    }

    return secondHighest;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!