views:

117

answers:

3

I created an date array using this:

 var holidays = ["7/24/2010","7/25/2010"];
 var holidaysArray = jQuery.makeArray(holidays);

and then testing to see if myDate (a date object) exists in the array:

if ($.inArray(myDate, holidaysArray ) == -1) {....}

However the test always return -1 even though myDate is one of the two days. I was trying to avoid using date strings to do the test.

How can I use inArray function with date objects and array of date objects? (I am not sure if holidaysArray is actually an array of date objects and maybe that's why the test is failing.)

+2  A: 

I think you are comparing Date and String objects, that's why you'll get always false.

see:

new Date("12/12/2000") == "12/12/2000" // this is false

EDIT:

Also! note that:

new Date("12/12/2000") == new Date("12/12/2000") // this is false too!

You should compare dates using their epoch time value like this

new Date("12/12/2000").valueOf() == new Date("12/12/2000").valueOf() // this is TRUE
Pablo Fernandez
A: 

To summarize others' answers:

  1. You already have an array, you don't need to use makeArray().
  2. Your array doesn't contain dates, it contains strings.
  3. Dates can't be compared directly in JavaScript.
njk
A: 

Basically you want to perform a search (lookup), right?

If you formulate your array like this:

var holidays = [{ date: "7/24/2010" }, { date: "7/25/2010" }];

And create a jOrder table out of it, indexed by date (unique):

var table = jOrder(holidays)
    .index('date', ['date']);

Then you can check the presence of a certain date in the table like this:

if (!table.where([{ date: myDate }]).length) {....}
Dan Stocker