13

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?

4

4 Answers 4

25

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:

Sign up to request clarification or add additional context in comments.

3 Comments

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: 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.
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)?
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)

Comments

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 Comment

It works, but to be exact, let's correct the last input date as in the question.
-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;
}

1 Comment

note the example asks a different question than the title. You are answering the question in the title.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.