2

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

3
  • 2
    Wondering why you are using jQuery.makeArray() when holidays is already an actual Array. Commented Jul 23, 2010 at 22:02
  • Patrick is right, you use makeArray for things that look like an array but are not (like the arguments variable inside a function) Commented Jul 23, 2010 at 22:06
  • My intention was to convert an array of strings to an array of date objects. I am not sure how to use makeArray to accomplish this. myDate is a date object. It's not a string. I assume inArray() works on the same type for the two arguments. Commented Jul 23, 2010 at 22:17

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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) {....}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.