11

How can I find the earliest date, i.e. minimum date, in an array using JavaScript?

Example:

["10-Jan-2013", "12-Dec-2013", "1-Sep-2013", "15-Sep-2013"]

My output should be:

["10-Jan-2013", "1-Sep-2013", "15-Sep-2013", "12-Dec-2013"]

How can I do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user3010195
  • 167
  • 1
  • 1
  • 3

4 Answers4

24

I'd suggest passing an anonymous function to the sort() method:

var dates = ['10-Jan-2013','12-Dec-2013','1-Sep-2013','15-Sep-2013'],
    orderedDates = dates.sort(function(a,b){
        return Date.parse(a) > Date.parse(b);
    });

console.log(orderedDates); // ["10-Jan-2013", "1-Sep-2013", "15-Sep-2013", "12-Dec-2013"]

var dates = ['10-Jan-2013', '12-Dec-2013', '1-Sep-2013', '15-Sep-2013'],
  orderedDates = dates.sort(function(a, b) {
    return Date.parse(a) > Date.parse(b);
  });

console.log(orderedDates);

JS Fiddle demo.

Note the use of an array ['10-Jan-2013','12-Dec-2013','1-Sep-2013','15-Sep-2013'] of quoted date-strings.

The above will give you an array of dates, listed from earliest to latest; if you want only the earliest, then use orderedDates[0].

A revised approach, to show only the earliest date – as requested in the question – is the following:

var dates = ['10-Jan-2013', '12-Dec-2013', '1-Sep-2013', '15-Sep-2013'],
    earliest = dates.reduce(function (pre, cur) {
        return Date.parse(pre) > Date.parse(cur) ? cur : pre;
    });

console.log(earliest); // 10-Jan-2013

var dates = ['10-Jan-2013', '12-Dec-2013', '1-Sep-2013', '15-Sep-2013'],
  earliest = dates.reduce(function(pre, cur) {
    return Date.parse(pre) > Date.parse(cur) ? cur : pre;
  });

console.log(earliest);

JS Fiddle demo.

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • 3
    Is there a reason why this wouldn't be working in 2021? When I run your first code snippet (sorting the dates) in the browser console, the dates in `orderedDates` remain in the same order as they started. – Kyle Vassella Apr 03 '21 at 04:01
  • 2
    @Kyle: I can't think of a reason it would no longer work for you, but having just checked my console (and edited both snippets to include the console output in the snippet window) I get the correct results as predicted in the code comments. Are you using a particularly ancient, or niche, browser or platform? Though there's nothing in the code that requires anything remotely 'modern' that would imply your browser choice might be problematic. – David Thomas Apr 03 '21 at 13:00
  • Shouldn't you be returning a positive or negative number from the sort, instead of the `cur` or `pre` itself? Maybe something more like `Date.parse(pre) - Date.parse(cur)`? – machineghost Jan 22 '22 at 02:00
0

This question is very old, but maybe you will find it helpful. It is going to give you the oldest date in the array.

const result = Math.min(...list.map((stringDate) => Date.parse(stringDate).getTime()))

It is returning number, so if you would love to use it as a date you have to use

new Date(result)
0

For anyone still here, try adding '-' instead of '>'

var dates = ['10-Jan-2013','12-Dec-2013','1-Sep-2013','15-Sep-2000'],
    orderedDates = dates.sort(function(a,b){
        return Date.parse(a) - Date.parse(b);
    });

console.log(orderedDates);
-1

Assuming you have an array of Date objects.

function findEarliestDate(dates){
    if(dates.length == 0) return null;
    var earliestDate = dates[0];
    for(var i = 1; i < dates.length ; i++){
        var currentDate = dates[i];
        if(currentDate < earliestDate){
            earliestDate = currentDate;
        }
    }
    return earliestDate;
}
driangle
  • 11,601
  • 5
  • 47
  • 54