0

I have a jQuery datepicker tool returning back a maximum and a minimum date. The dates are to filter out results from an array. I use jQuery.grep to filter based on the date. For some reason, while >= will work, <= only returns less than.

// Function to filter based on date minimum
function filterListGreaterThan(filter, list, min){
    var result = jQuery.grep(list, function (obj){
        return new Date(obj[filter]) >= min;
    });
    return result;
};  

function filterListLessThan(filter, list, max){
    var result = jQuery.grep(list, function (obj){
        return new Date(obj[filter]) <= max;
    });
    return result;
};

So if i put in Nov 1, 2013 - Nov 5, 2013 it will only return Nov 1 - Nov 4... and I have no idea why.

Edit: Mac gave me the correct answer. When comparing the dates jQuery Sets the time to midnight. So even though I had it searching on the correct day it was not looking past midnight. This is the Corrected function:

// Function to filter based on date maximum
function filterListLessThan(filter, list, max){
    var result = jQuery.grep(list, function (obj){
        //add time to date because jQuery sets the time at 00:00:00
        max.setHours(23,59,59);
        return new Date(obj[filter]) <= max;
    })
    return result;
};
knightsbore
  • 470
  • 2
  • 8
  • 24
  • `Date`s are `objects` and object equivalence is not so simple. – Evan Davis Nov 26 '13 at 20:57
  • 1
    @Mathletics: actually, JavaScript `Date`s do support some comparison: see [here](http://stackoverflow.com/a/493018/214149) for example. My guess (and it's just a guess): the date representing max (i.e. Nov 5) is actually 00:00 AM on November 5, and it's filtering out items from November 5 in the array because they occur after 00:00 AM. – Mac Nov 26 '13 at 21:21
  • Wow you got it right on the nose, i set the hours and minutes to just before midnight and now all the times during the max day work. (throw it in an answer and ill mark it if you want) – knightsbore Nov 26 '13 at 21:58
  • 1
    @Mac good tip; TIL! :) – Evan Davis Nov 27 '13 at 00:09

2 Answers2

1

It seems the problem is likely due to the max date having a time component set to 00:00 AM - all items in the array occurring on the max date are probably being filtered out because they occur some time after 00:00 AM.

To fix this, the best approach is either to change the max date to have a time component of 11:59:59 PM, or to set the max date to 00:00 AM the following day and use a less-than (rather than a less-than-or-equal).

Mac
  • 14,615
  • 9
  • 62
  • 80
0

Not entirely sure I understand what you are trying to do, so apologies if this is not what you need but if you just want to filter an array of dates I'd try something like below. You need to make sure you are comparing a Date object with another Date object and that the values in your array are formatted so as to make a valid Date object.

I'm not sure how that jQuery function works but using vanilla javascript I would do something like this to filter dates:

var list = ['2013,10,01','2013,10,02','2013,10,03','2013,10,04','2013,10,05',
           '2013,10,06'];

function filterListGreaterThan(list, min_date){

    var filtered_dates = [];

    for(var i=0; i < list.length; i++){

        var parts = list[i].split(','),
            test_date = new Date(parts[0],parts[1],parts[2]);

        if(test_date >= min_date){
            filtered_dates.push(test_date);

        }
    }

    return filtered_dates;
}  

var min_date = new Date('2013','10','04'),
    dates = filterListGreaterThan2(list, min_date);

console.log(dates);

//RETURNS:
//Mon Nov 04 2013 00:00:00 GMT+0000 (GMT Standard Time)
//Tue Nov 05 2013 00:00:00 GMT+0000 (GMT Standard Time)
//Wed Nov 06 2013 00:00:00 GMT+0000 (GMT Standard Time)
//
Matt Herbstritt
  • 4,754
  • 4
  • 25
  • 31
  • Formatting works fine, the problem was the datetimes I was comparing to had a time of midnight, so any time during the day after midnight was not working. – knightsbore Nov 26 '13 at 21:57