basarat basarat -4 years ago 61
Javascript Question

Find the earliest of a series of JavaScript dates

How do I find the earliest of a set of dates. Currently I have the following code which works just fine:

var dates = [date1,date2]; // list of javascript dates
var start = moment(new Date(9999, 0, 1))
// I wished this was one line in momentjs + underscorejs
_.forEach(dates, (date) => {
if (moment(date).isBefore(start)) {
start = moment(date);

I was hoping there was a neater way (in one line). I would prefer to use a simpler function in Underscore (min does not work on dates) / momentjs.

Answer Source

I found _.min is reliable on moment instances. The following works:

    var dates =[date1,date2],function(date){return moment(date)});
    var start = _.min(dates);    


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download