-2

Something is really off about my script..

    //Day of the Week
    checkDate = "03/18/15";
    year = checkDate.substr(6,2);
    month = checkDate.substr(0,2);
    day = checkDate.substr(3,2);

    var d = new Date("20"+year, month, day);

    var weekday = new Array(7);
    weekday[0]=  "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";

    var n = weekday[d.getDay("20"+year, month, day)];
    //RESULT: Sat Apr 18 2015 00:00:00 GMT-0500 (Central Daylight Time)

When I put in 03/18/15.. some reason it returns April. If I put in 04/14/15, it returns May. Its always a month ahead of time. Why is that? Something off in my code?

user4713659
  • 25
  • 1
  • 6
  • What do you mean minimum? – user4713659 Mar 27 '15 at 16:18
  • 1
    I mean that when you select tags for your questions, the *very first tag* tag you add should be for the language you're using. **The minimum tags should include a tag for the language**; you can then add additional tags you think are relevant. – Ken White Mar 27 '15 at 16:19
  • Sorry, don't know why that slipped my mind. Its in javascript. I edited it. – user4713659 Mar 27 '15 at 16:19
  • Everything after the (commented out) `console.log` seems not relevant to your question. Please make sure you provide the actual code that actually shows the result -- nothng more. Even your title focuses on the wrong thing. – Jongware Mar 27 '15 at 16:20
  • @Jongware I edited the original post. – user4713659 Mar 27 '15 at 16:23

1 Answers1

2

Months in JavaScript are zero-based. So January is 0, February is 1, ...

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

An easy fix would be to just subtract one from the month value you get.

j08691
  • 204,283
  • 31
  • 260
  • 272