jQuery 检查日期是否属于日期数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17067296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:22:09  来源:igfitidea点击:

Checking if date belongs to array of dates

javascriptjqueryarraysdate

提问by foozed

I'm trying to check if a date from a jQuery UI datepicker belongs to an array of dates that are holidays. Can't figure out what I'm doing wrong :(

我正在尝试检查来自 jQuery UI 日期选择器的日期是否属于假期日期数组。无法弄清楚我做错了什么:(

var holidayArray2013 = [new Date('October 3, 2013 00:00:00 GMT+0200'), new Date('December 25, 2013 00:00:00 GMT+0100'), new Date('December 26, 2013 00:00:00 GMT+0100')];
var DateOfOrder = $('#datepicker').datepicker('getDate');
if ($.inArray(DateOfOrder, holidayArray2013) > -1) {
  console.log("is holiday");
}

edit: console.log(DateOfOrder);returns Thu Oct 03 2013 00:00:00 GMT+0200just like holidayArray2013[0]but $.inArray(DateOfOrder, holidayArray2013)still returns -1

编辑:console.log(DateOfOrder);返回Thu Oct 03 2013 00:00:00 GMT+0200就像holidayArray2013[0]$.inArray(DateOfOrder, holidayArray2013)仍然返回-1

回答by Jamiec

You're getting a false negative because comparing 2 date objects compares their referencesand not their valuesas you perhaps expected.

你得到了一个假阴性,因为比较 2 个日期对象比较它们的引用而不是它们的值,正如你可能预期的那样。

There are a few options, you could store the result of Date.getTime()in your array which is just a numerical representation of the date:

有几个选项,您可以将结果存储Date.getTime()在您的数组中,这只是日期的数字表示:

var holidayArray2013 = [
        new Date('October 3, 2013 00:00:00 GMT+0200').getTime(), 
        new Date('December 25, 2013 00:00:00 GMT+0100').getTime(), 
        new Date('December 26, 2013 00:00:00 GMT+0100').getTime()];

And then compare that:

然后比较一下:

var DateOfOrder = n$('#datepicker').datepicker('getDate').getTime();
if ($.inArray(DateOfOrder, holidayArray2013) > -1) ...

This works fine, as demonstrated here: http://jsfiddle.net/rRJer/

这很好用,如下所示:http: //jsfiddle.net/rRJer/

If, however you are constrained to not changing the holiday array you could loop to try to locate the right date value:

但是,如果您不能更改假期数组,则可以循环尝试找到正确的日期值:

var isHoliday = false;
for(var i=0;i<holidayArray2013.length;i++){
    if(holidayArray2013[i].getTime() == DateOfOrder.getTime()){
        isHoliday = true;
        break;
    }
}

Demo is here: http://jsfiddle.net/3R6GD/

演示在这里:http: //jsfiddle.net/3R6GD/

回答by Ouadie

check this http://jsfiddle.net/WNYRs/

检查这个http://jsfiddle.net/WNYRs/

var holidayArray2013 = [new Date('October 3, 2013 00:00:00 GMT+0200').getTime(),
                        new Date('December 25, 2013 00:00:00 GMT+0100').getTime(),
                        new Date('December 26, 2013 00:00:00 GMT+0100').getTime()];

And use something like that:

并使用类似的东西:

var DateOfOrder = new Date($('#datepicker').datepicker({ dateFormat: 'yy-mm-dd' }).val()).getTime();

you have to use Date.getTime()to compare two dates, see this : How to compare two date values with jQuery

您必须使用Date.getTime()来比较两个日期,请参阅:如何使用 jQuery 比较两个日期值

回答by IcanDivideBy0

You can use the .valueOf()function over your dates, then the $.inArraywill do the job :

您可以.valueOf()在您的日期使用该功能,然后$.inArray将完成这项工作:

var holidayArray2013 = [
    new Date('October 3, 2013 00:00:00 GMT+0200').valueOf(),
    new Date('December 25, 2013 00:00:00 GMT+0100').valueOf(),
    new Date('December 26, 2013 00:00:00 GMT+0100').valueOf() ];

var DateOfOrder = $('#datepicker').datepicker('getDate').valueOf();
if ($.inArray(DateOfOrder, holidayArray2013) > -1) {
    console.log("is holiday");
}

See http://jsfiddle.net/mmeRD/

http://jsfiddle.net/mmeRD/