11

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 :(

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+0200 just like holidayArray2013[0] but $.inArray(DateOfOrder, holidayArray2013) still returns -1

foozed
  • 123
  • 1
  • 2
  • 7
  • 2
    Nor can we unless you tell us if there is an error, or perhaps not giving the result you expect. – Jamiec Jun 12 '13 at 13:53
  • 1
    What is the value of `DateOfOrder`? – Felix Kling Jun 12 '13 at 13:54
  • 1
    You are looking for two seperate objects even though they may be the same date. You will need to store the dates as a string and search for a string (one possible way). – Xotic750 Jun 12 '13 at 13:57
  • 1
    When you `console.log` it executes `toString` on the date object. – Xotic750 Jun 12 '13 at 14:00
  • 2
    Have a look at [Two identical JavaScript dates aren't equal](http://stackoverflow.com/q/15470403/218196). – Felix Kling Jun 12 '13 at 14:03
  • If you want a small library that can perform date comparisons, rather than writing POJS (but in this case why not?) then take a look at moments.js: http://momentjs.com/ – Xotic750 Jun 12 '13 at 14:08
  • It is actually different, you say October, DateOfOrder is Oct – Rob M. Jun 12 '13 at 14:10

3 Answers3

33

You're getting a false negative because comparing 2 date objects compares their references and not their values as you perhaps expected.

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:

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/

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/

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • thanks jamie, accepted this for being the most precise answer and providing an alternative i needed. – foozed Jun 12 '13 at 14:28
1

check this 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

Community
  • 1
  • 1
Ouadie
  • 13,005
  • 4
  • 52
  • 62
0

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

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/

IcanDivideBy0
  • 1,627
  • 11
  • 17