简体   繁体   English

JavaScript日期小于或等于仅返回小于

[英]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. 我有一个jQuery datepicker工具,可以返回最大和最小日期。 The dates are to filter out results from an array. 日期是为了从数组中筛选出结果。 I use jQuery.grep to filter based on the date. 我使用jQuery.grep根据日期进行过滤。 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. 因此,如果我输入2013年11月1日至2013年11月5日,它将只返回11月1日至11月4日...我也不知道为什么。

Edit: Mac gave me the correct answer. 编辑:Mac给了我正确的答案。 When comparing the dates jQuery Sets the time to midnight. 比较日期时,jQuery将时间设置为午夜。 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. 看来问题可能是由于最大日期的时间分量设置为00:00 AM-发生在最大日期的数组中的所有项目都可能被过滤掉,因为它们发生在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). 要解决此问题,最好的方法是将最大日期更改为具有11:59:59 PM的时间分量,或者将最大日期设置为第二天的00:00 AM,并使用小于号(而不是小于或等于)。

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. 您需要确保将一个Date对象与另一个Date对象进行比较,并确保格式化数组中的值以使之成为有效的Date对象。

I'm not sure how that jQuery function works but using vanilla javascript I would do something like this to filter dates: 我不确定jQuery函数的工作方式,但是使用香草javascript可以执行以下操作来过滤日期:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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