Getting values from a textarea and storing them correctly
<p>I am writing a JavaScript program to determine what day of the week a given date can fall on. The user types the date in a textbox and then presses submit.</p>

<p>The ways a date can be entered is:</p>

<pre><code>Day/Month/Year --&gt; 6/15/95 for June 6th, 1995
                   6/15/1995 for the same date
Day Month Year --&gt; 15 September 2006
Month Day Year --&gt; February 19, 1994
</code></pre>

<p>I got it to accept the <code>Day/Month/Year</code> and <code>Month Day, Year</code>, but for some reason I cannot get it to accept it with the day followed by the month. Here is my code so far.</p>

<pre><code>var monthArray = ["January", "February", "March",
                  "April", "May", "June", "July",
                  "August", "September", "October",
                  "November", "December"];

var month;
var day;
var year;

var text1 = getElementById("myTextArea").value;      
var text2 = text1.split(/[\s\/,]+/);  //15 Sep 2001 will be ["15", "Sep", "2001"]
                                      //6/13/95 will be ["6", "13", "95"]
                                      //Sep 15, 2001 will be ["Sep", "15", "2001"]

for(var i = 0; i &lt; text2.length; i++)    //Iterate through all elements in text2
{
   for(var j = 0; j &lt; monthArray.length; j++) //Iterate through all elements in monthArray
   {
      if(text2[i].substring(0, 3) == monthArray[j].substring(0, 3))   //See if one of the elements matches a month string.
      {
         month = j + 1;   //Set month equal to the number. For instance, if Sep month = 9
         text2.splice(i, i + 1);  //Remove the month element. Array should contain ["date", "year"] in that order
         day = text2[0];    //Set day equal to the "date" element.
         year = text2[1];   //Set year equal to the "year" element.
      }
   }
}

if(typeof month == "undefined")    //This will happen if month isn't a string. I.e. it is a number like 5/16/54.
{
   month = text2[0];
   day = text2[1];
   year = text2[2];
}

//This is for a specified year range.
if(year &gt;= 50 &amp;&amp; year &lt; 100)
{
   year = 1900 + parseInt(year);
}
else if(year &lt;= 49 &amp;&amp; year &gt;= 0)
{
   year = 2000 + parseInt(year);
}
</code></pre>

<p>When I enter something like 6/15/95 or June 15, 1995 it works fine. But for some reason if I try to enter 15 June 1995, it won't work and I have no idea why. Can anyone spot my mistake? I've been messing with this for hours with no avail. Is there an easier way to do this? Any way I can do this with just some regular expressions? I feel like I am making this way harder than I need to. Thanks.</p>