Skip to content Skip to sidebar Skip to footer

How To Return The Lowest Date Value And Highest Date Value From An Array In Javascript?

How to return the lowest date value and highest date value from an array? For example for the below array, I'd like to create a function that returns the lowest date, as well as an

Solution 1:

We just have to apply some Javascript Magic.

var result = Math.max.apply( null, data.map(function( v ) {
    return +newDate( v.date );
}));

There, we map those Objects into just their timestamp representation from the ISO-Date string. Then we simply apply a Math.max, respectively Math.min on the resulting Array. All we have left to do now, is to re-convert that timestamp into a Date object

newDate( result );

And just for the heck of it, an alternative solution using Array.prototype.reduce and pure String comparison (if you are scared of browser incompatibilities on parsing ISO Date Strings).

var max = data.reduce(function( prev, current ) {
  return prev.date > current.date ? prev : current;
});
// for the min version just ">" to "<"

Solution 2:

You can sort the array using a custom comparator:

data.sort(function(o1,o2){
    returnnewDate(o1.date).getTime() - newDate(o2.date).getTime();
});

and then get the largest/smallest dates by getting the first/last items:

var smallest = data[0].date;
var largest  = data[data.length - 1].date;

Solution 3:

You could sort by those keys, and then take the first and last values:

sortedData = data.sort(function(a, b) {
    returnnewDate(a.date) > newDate(b.date) ? 1 : -1;
});
var min = sortedData[0],
    max = sortedData[sortedData.length - 1];

Solution 4:

Try this:

var data = [
  {date: "2011-11-01T16:17:54Z", quantity: 2, total: 190, tip: 100, type: "tab"},
  {date: "2011-11-14T16:20:19Z", quantity: 2, total: 190, tip: 100, type: "tab"},
  {date: "2011-11-14T16:28:54Z", quantity: 1, total: 300, tip: 200, type: "visa"},
  {date: "2011-11-14T16:30:43Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T16:48:46Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T16:53:41Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T16:54:06Z", quantity: 1, total: 100, tip: 0, type: "cash"},
  {date: "2011-11-14T16:58:03Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:07:21Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:22:59Z", quantity: 2, total: 90, tip: 0, type: "tab"},
  {date: "2011-11-14T17:25:45Z", quantity: 2, total: 200, tip: 0, type: "cash"},
  {date: "2011-11-31T17:29:52Z", quantity: 1, total: 200, tip: 100, type: "visa"}
];

var lowest = data.reduce(function (a, b) {
    var timeA = newDate(a.date).getTime();
    var timeB = newDate(b.date).getTime();
    return timeA < timeB ? a : b;
});

alert("lowest: " + JSON.stringify(lowest, null, 4));

var highest = data.reduce(function (a, b) {
    var timeA = newDate(a.date).getTime();
    var timeB = newDate(b.date).getTime();
    return timeA > timeB ? a : b;
});

alert("highest: " + JSON.stringify(highest, null, 4));

Solution 5:

Lots of suggestions to convert the strings to dates, however using the Date constructor to parse strings is unreliable and will fail, even for the format specified in ES5, for a reasonable number of browsers in use.

Since you are using ISO 8601 format date strings, you can sort them without conversion (that's one of the benefits of using that format, as well as its being unambiguous). So you can sort the array using:

data.sort(function(a, b) {return a.date > b.date? 1 : a.date < b.date? -1 : 0});

Now you just get the first and last member of the array:

var earliestDate = data[0].date;
var latestDate   = data[data.length - 1].date;

As others have said, the last date string is an invalid date, how should the sort deal with that? The above suggestion doesn't test whether the date is valid or not, it just looks at the characters.

Post a Comment for "How To Return The Lowest Date Value And Highest Date Value From An Array In Javascript?"