3

i want to get the min and max date form a json array:

my code

$.getJSON(url, function (data) {
    var dates = Object.keys( data['data']['horodate'] ).map(function ( key ) {
        return data['data']['horodate'][key].replace(/\-/g,'/' ) 
    });
    var min = new Date(Math.min.apply( null, dates ));
    var max =  new Date(Math.max.apply( null, dates));
});

data array is:

Array [ 
    "2016/10/13 00:00:00", 
    "2016/10/13 00:30:00", 
    "2016/10/13 01:00:00", 
    "2016/10/13 01:30:00", 
    "2016/10/13 02:00:00", 
    "2016/10/13 02:30:00", 
    "2016/10/13 03:00:00", 
    "2016/10/13 03:30:00", 
    "2016/10/13 04:00:00", 
    "2016/10/13 04:30:00"
]

but i have an error : Invalid date. can you help me ?

0

2 Answers 2

7

Use Array#sort with custom sort function and get the last(max) and first(min) values.

data = ["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"];

var sorted = data.slice() // copy the array for keeping original array with order
  // sort by parsing them to date
  .sort(function(a, b) {
    return new Date(a) - new Date(b);
  });

// get the first and last values
console.log(
  'max :', sorted.pop(), 'min :', sorted.shift()
);


Or with a simple Array#forEach loop.

data = ["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"];

// initially set max and min as first element
var max = data[0],
  min = data[0];

// iterate over array values and update min & max
data.forEach(function(v) {
  max = new Date(v) > new Date(max)? v: max;
  min = new Date(v) < new Date(min)? v: min;
});

console.log('max :', max, 'min :', min);

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

1 Comment

@user2203384 : glad to help
0

IMO, you should use new Date(value)

Nothing seems wrong in your code.

The reason of getting Invalid date is you're not converting date into Date object, you need to convert it in JavaScript date by new Date().

The Date object lets you work with dates (years, months, days, hours, minutes, seconds, and milliseconds)

var datesNew=["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"];
//This will convert your date into Date object
$.each(datesNew, function(key, value){
    dates.push(new Date(value));
});
var min = dates.reduce(function (a, b) { return a < b ? a : b; }); 
var max = dates.reduce(function (a, b) { return a > b ? a : b; }); 

var datesNew=["2016/10/13 00:00:00", "2016/10/13 00:30:00", "2016/10/13 01:00:00", "2016/10/13 01:30:00", "2016/10/13 02:00:00", "2016/10/13 02:30:00", "2016/10/13 03:00:00", "2016/10/13 03:30:00", "2016/10/13 04:00:00", "2016/10/13 04:30:00"];
var dates=[];
$.each(datesNew, function(key, value){
    //Conver date in Date object
    dates.push(new Date(value));
});

var min = new Date(Math.min.apply( null, dates));
var max =  new Date(Math.max.apply( null, dates));
//var min = dates.reduce(function (a, b) { return a < b ? a : b; }); 
//var max = dates.reduce(function (a, b) { return a > b ? a : b; }); 
console.log(new Date(min).toString());
console.log(new Date(max).toString());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Comments

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.