0

Anybody got any clue why this won't work with more than 1 date...

it only takes the first date in the array...

var unavailableDates = ["10-6-2011","13-6-2011"];

function unavailable(date) {
    dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
    if ($.inArray(dmy, unavailableDates) == 0) {
        return [false, "", "Unavailable"];
    } else {
        var day = date.getDay();
        return [(day != 0 && day != 2 && day != 3 && day != 4 && day != 6)];
    }
}

see full example below

http://offline.raileisure.com/lee.php

Thanks in advance

Lee

6
  • 3
    You really should have posted the actual code that tries to use the array. Posting nothing more than a link to your site will not be helpful to Stackoverflow readers a year from now. Commented Jun 3, 2011 at 13:47
  • Try providing some additional details on how exactly it fails. Commented Jun 3, 2011 at 13:47
  • @Greg the first date is disabled but the others in the array don't disable, its as if the code isn't reading anything after the first value in the array Commented Jun 3, 2011 at 13:49
  • Having your dates in an ambiguous format is almost always a bad idea. If you're going to use date strings, it's almost always best to stick with yyyy-mm-dd format. Also, whatever format you use, avoid single-digit days or months, as these can throw things out as well: use 06 rather than just 6 Commented Jun 3, 2011 at 13:50
  • Define "this won't work" Commented Jun 3, 2011 at 13:51

4 Answers 4

8

It doesn't work because you're interpreting the return value of "$.inArray()" incorrectly. The function returns -1 when the search target cannot be found, and the index in the array when it can. Thus, when it returns 0, that means it did find what the code was looking for.

A cute trick — for those who like cute tricks — for checking the return value from functions like "$.inArray()" is to apply the "~" operator:

if (~$.inArray(needle, haystack)) {
  // found it
}
else {
  // did not find it
}

The "~" operator forms the bitwise complement (or "1's complement") of its argument. Because "~-1" is 0, and "~n" is non-zero for any other integer, it effectively converts the return value to a "truthy/falsy" value appropriately. Don't use it if you don't like cute tricks :-)

Also, that "dmy" variable used in a couple functions should be declared in each one with the var keyword.

Sign up to request clarification or add additional context in comments.

5 Comments

Its correct that when it finds the date it needs to return false and disable that date
+1 though you forgot the obligatory link to the manual page that the OP should already have been reading merely from the fact that he's used the function!
@Lee: $.inArray(dmy, unavailableDates) != -1 then. You only had it for == 0 (i.e. the first element).
@Lee: The lesson we're all learning here, I hope, is that your code is hard to read. It's not obvious what it does.
good work : ) in pure javascript: function inArrayOrStr (o, v) { return ~o.indexOf(v); }
3

jQuery.inArray returns the index of the item found, ie, when it matches the second value, it is returning 1, not the 0 you test for.

You should change your test to be >= 0 rather than == 0 when you do

if ($.inArray(dmy, unavailableDates) == 0) { ...

1 Comment

Yea, we've actually been telling you this for a while, Lee. Please follow advice given in comments. Still, +1 this is the most concise answer that solves the problem and explains it.
1

Is it reading it in as mm-dd-yyyy?

If so - then 13-6-2011 would not be a valid date.

Edit Okay - looking at your page; clearly not since 10th June is not available as expected.

I deleted the answer but where dates are concerned I think this is a valuable thing to remember (e.g. on a US client presumably I'd be right?) so I undeleted it.

I'll get rid of it again though if the community feels I should.

8 Comments

No the first date is working fine and its in the right format
+1 because this seems very plausible -- having your dates in an ambiguous format is almost always a bad idea. If you're going to use date strings, it's almost always best to stick with yyyy-mm-dd format. Also, whatever format you use, avoid single-digit days or months, as these can throw things out as well: use 06 rather than just 6.
-1 Locales have nothing to do with it. He's not reading anything in to a date parser; he's building strings in a fixed format and comparing them.
@Tomalak - good point. That's reason enough for me to delete then
@Tomalak - true. Although I reckon if it were, the Skeet would be the end-boss. Fearsome.
|
0

Try this: in pure javascript: I made ​​some modifications to make your code faster

function inArrayOrStr (o, v) {
    return ~o.indexOf(v);
}

unavailableDates = ["10-6-2011","13-6-2011"];

function unavailable(date) {
    var dmy = [date.getDate() ,(date.getMonth() + 1) , date.getFullYear()].join("-");
    if (inArrayOrStr(unavailableDates, dmy) {
        return [false, "", "Unavailable"];
    } else {
        var day = date.getDay();
        return [day > 7];
    }
}

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.