Click here to Skip to main content
15,922,650 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
JavaScript

Hii friends,

I have used following code to find min and max value of date from an array of date using Jquery.

var Dates[
C#
Date {Wed Jun 08 2016 00:00:00 GMT+0530 (India Standard Time)}, Date {Thu Jun 09 2016 00:00:00 GMT+0530 (India Standard Time)}, Date {Fri Jun 10 2016 00:00:00 GMT+0530 (India Standard Time)}

];
var minDate;
mindate = new Date(Math.min.apply(null, dates));

But it returns invalid Date.

please help.

What I have tried:

var Dates[
C#
Date {Wed Jun 08 2016 00:00:00 GMT+0530 (India Standard Time)}, Date {Thu Jun 09 2016 00:00:00 GMT+0530 (India Standard Time)}, Date {Fri Jun 10 2016 00:00:00 GMT+0530 (India Standard Time)}

];
var minDate;
mindate = new Date(Math.min.apply(null, dates));
Posted
Updated 6-Jun-16 18:46pm
Comments
Sergey Alexandrovich Kryukov 7-Jun-16 0:35am    
The code you show is not JavaScript code. I don't know what is it.
What's the problem? First, you need to have an array. Traverse it in a for loop, get each element by index,
compare with current min and max...
—SA

convert the Json array into a date array and then apply the Math.min.apply function

example:

JavaScript
var date1 = new Date();
       date1.setDate(date1.getDate() + 1);
       var date2 = new Date();
       date2.setDate(date2.getDate() + 2);
       var date3 = new Date();
       date3.setDate(date3.getDate() + 3);
       var date4 = new Date();
       date4.setDate(date4.getDate() - 5);
       var _Dates = [{ date: date1 }, { date: date2 }, { date: date3 }, { date: date4 }];


      // adding the values from json array into a date array.
       var dateArray = [];
       for (var i = 0; i < _Dates.length; i++) {
           dateArray.push(_Dates[i].date);
       }
       var minDate = new Date(Math.min.apply(null, dateArray));
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 7-Jun-16 1:04am    
This is a pretty sophisticated way of using Math.min and Math.max, but there are some problems.

First of all, who told you there is a JSON array? There is nothing like that, not even close. All I can see is some non-JavaScript code which won't be interpreted at all.
Traversing array in a loop with a loop variable and range is quite redundant; it is only needed when the range is calculated and not the same as the array range; otherwise for-in loop should be used.
But the worst thing is the artificially introduced two different array objects and populating one array from another. This is a totally redundant manipulation.

Please see another solution, Solution 2. Makes sense?

—SA
Please see my comment to the question. Even the formulation of the problem should be written in JavaScript. For example:
JavaScript
var points = [
   new Date(2016, 0),
   new Date(2018, 1),
   new Date(1999, 1),
   new Date(302, 0)];

Please see: Date — JavaScript.

How to compare the points of time? You simply can use the fact the the operators '<' and '>' are defined for the Date objects.

The function to calculate both minimum and maximum can, for example, return a stricture with the properties min and max:
JavaScript
function getMinMaxTime(points) {
    if (!points) return undefined;
    if (!points.length) return undefined; // not array
    if (points.length < 0) return null;
    var min = points[0];
    var max = points[0];
    for (var index in points) {
        if (points[index] > max) max = points[index];
        if (points[index] < min) min = points[index];
    }
    return { min: min, max: max };	
}


The return for pathological cases when there is no array or the object passed as the argument is not an array or there are no points are just for example. You may decide to do no check-ups, and then deal with possible exceptions, which is also would be a sound decision.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900