How to return smallest date value and maximum date value from an array in Javascript?

How do I return the smallest date value and the maximum date value from an array?

For example, for the following array, I would like to create a function that returns the lowest date and also another function that returns the highest date?

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"}
]; 

      

I'm not sure if this is relevant, but I'm using this array with crossfilter, so I'm not sure if crossfilter has useful methods.

+3


source to share


6 answers


We just need to apply Javascript Magic .

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

      

Here we only map these objects to their metric label from the ISO-Date string. Then we just apply Math.max

accordingly Math.min

on the resulting array. All we have left to do is convert this timestamp to a Date object

new Date( result );

      




And just for that, an alternative solution using a Array.prototype.reduce

pure String comparison too (if you fear browser incompatibility when parsing ISO date strings).

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

      

+7


source


You can sort the array using a custom comparator:

data.sort(function(o1,o2){
    return new Date(o1.date).getTime() - new Date(o2.date).getTime();
});

      



and then get the highest / lowest dates by getting the first / last elements:

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

      

+1


source


A simple string comparison should give you what you expect. Since the dates are in ISO format, this should be fairly easy to do.

var lowestIndex = 0;
var lowestDate = data[0].date;
for (var i=0; i<data.length; i++) {
    if (lowestDate > data[i].date) {
        lowestDate = data[i].date;
        lowestIndex = i;
    }
}
return data[lowestIndex];

      

If you are comfortable with apply

and call

, then you shouldn't have much trouble converting this simple logic into a function.

+1


source


You can sort by these keys and then take the first and last values:

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

      

0


source


Try the following:

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 = new Date(a.date).getTime();
    var timeB = new Date(b.date).getTime();
    return timeA < timeB ? a : b;
});

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

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

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

Run code


0


source


Lots of suggestions for converting 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 date strings, you can sort them without conversion (this is one of the advantages of using this format, as well as its unambiguousness). Thus, 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 element of the array:

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

      

As others have said, the last date string is not a valid date, how should this type be treated? The above suggestion doesn't check if the date is valid or not, it just looks through the characters.

0


source







All Articles