Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if date belongs to array of dates

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

like image 752
foozed Avatar asked Jun 12 '13 13:06

foozed


People also ask

How do you check if an element is in an array?

You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.

How do you check if a date exists between two dates?

To check if a date is between two dates: Use the Date() constructor to convert the dates to Date objects. Check if the date is greater than the start date and less than the end date. If both conditions are met, the date is between the two dates.

How do you check if a value exists in an array JS?

The indexof() method in Javascript is one of the most convenient ways to find out whether a value exists in an array or not. The indexof() method works on the phenomenon of index numbers. This method returns the index of the array if found and returns -1 otherwise.


2 Answers

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/

like image 63
Jamiec Avatar answered Oct 04 '22 19:10

Jamiec


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

like image 22
Ouadie Avatar answered Oct 04 '22 19:10

Ouadie