I have this arrangement:
var arr = ["2018-06-07","2018-06-30","2018-06-01","2018-06-21","2018-07-20"];
And I need to get the maximum and minimum date with javascript, I have tried with this function:
var min = new Date(Math.max.apply(null,arr));
var max = new Date(Math.min.apply(null,arr));
But I get a "NaN" value back.
what will i be doing wrong? or is there any other way to get the major and minor date of an array?, thanks
Using the
Date
JavaScript class you can do it easily as it allows you to do comparison operations on dates and make dates from strings. Then it's just a matter of iterating the array and saving the majors or minors.You should do the following:
Try this from your browser console, it should work for you, then you adapt it to your needs, in this case you put the corresponding dates.
The detail is that you are looking for the minimum and maximum value of a string, you must change your array to Date() and you will not have problems
Another option but you still convert the array to Date , looping through it and getting an element back is with Array.reduce