简体   繁体   中英

Javascript date less than or equal to returning only less than

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

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).

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)
//

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM