0

In my code when i equals 5 myDate should be equal to it. The alert shows me that they are the same. I can never get the function to return 1;

function checkForHoliday(date) {
    var myDate = new Date(date);
    myDate = myDate.getMonth() + "/" + myDate.getDate() + "/" + myDate.getFullYear();

    alert(myDate + "\n" + holidays[5]);

    for (i = 0; i <= 9; i++) {
       if (myDate == holidays[i]) {
           return 1;
           alert("got it");
       }       
    }

    return 0;
}

This is what the string in the array looks like:

year = 2013
holidays[5] = "7/2/" + year

My alert shows me this: enter image description here

13
  • @SimonAndréForsberg: It looks like he's converting it to a string. Commented Aug 28, 2013 at 17:44
  • @SimonAndréForsberg it is a string. He overwrote it. Commented Aug 28, 2013 at 17:44
  • 4
    BTW, in the future I recommend using console.log for this kind of "printf debugging". Learning to use the development tools and debugger in your favourite browser also helps a ton. Commented Aug 28, 2013 at 17:44
  • 5
    You are returning 1 before you execute your alert statement. Try returning 1 after you do you alert so you can tell if your condition is met in the if statement. Commented Aug 28, 2013 at 17:46
  • 1
    I'd recommend you to convert the holidays and the dates to a unix timestamp and to compare them both like you're doing now, that way you won't have to mess with strings. Commented Aug 28, 2013 at 17:46

3 Answers 3

1

I have run your code locally, and have it working.

I'm going to guess that your issue stems from the fact that Date.getMonth() returns month numbers where January === 0. That throws a lot of people off.

To recreate your code, I simply used Chrome's console. I also changed your alert to a console.log to save myself the hassle of using alert.

Here's the code:

function checkForHoliday(date) {
  var myDate = new Date(date);
  myDate = myDate.getMonth() + "/" + myDate.getDate() + "/" + myDate.getFullYear();

  console.log(myDate + "\n" + holidays[5]);

  for (i = 0; i <= 9; i++) {
    if (myDate == holidays[i]) {
      return 1;
    }
  }

  return 0;
}

And a fake holiday array:

holidays = [0,1,2,3,4,'7/2/2013']

(The 7 here actually corresponds to August)

Upon running checkForHoliday('8/2/2013'), the console reports back a response of 1. The code successfully matches the date.

If you actually intended for holiday[5] to represent July 2nd, 2013, you'll need to set holiday[5] = '6/2/2013'.

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

Comments

0

Try Using this,

 <script type="text/javascript">

    var startYear = parseInt(document.getElementById('startYear'), 10);
    var startMonth = parseInt(document.getElementById('startMonth'), 10) - 1; 
    var startDay = parseInt(document.getElementById('startDay'), 10);
    var myDate = new Date(startYear, startMonth, startDay);

</script>

Comments

0

Convert the date object to string in desired format. Use triple equals operator to test equality.

Try:

if (myDate.toLocaleDateString() === holidays[i]) {
    return 1;
    alert("got it");
}

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.