SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    May 2010
    Location
    Omaha, NE
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help!!! I think I deleted Februrary!!!

    I can't bring February up using my Javascript. I am using the following code, which I got straight from a book, to list the months in an HTML form. The user would select the month (as well as a day and year) from the list, and a calculation would be performed (number of days, day of the week, etc). When I run the code, I get "Jan Mar Mar Apr May Jun Jul Aug Sep Oct Nov Dec". NO FEB!!! In the book's app, the user selects two dates and the script calculates the number of days between. I tried selecting the first Mar to see if it was just Feb misnamed, but the computer calculated it as the month of Mar, not Feb.

    Did I do something to accidentally write over February on my computer and replace it with March? The only way I could have done so was through JavaScript, since that is all I do (well that and HTML/CSS). How do I fix this???

    Here is the code in question:

    function writeMonthOptions()
    {
    var theMonth;
    var monthCounter;
    var theDate = new Date();
    for (monthCounter = 0; monthCounter < 12; monthCounter++)
    {
    theDate.setMonth(monthCounter);
    theMonth = theDate.toString();
    theMonth = theMonth.substr(4, 3);
    document.write("<option value=" + theMonth + ">" + theMonth);
    }
    }


    Thank you in advance for the help.

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your new Date() is probably December 31-
    if you change only the month to February you are getting February 31, which is resolved to March 3 (it would be March 2 in 2012).

    Set the starting date to the 1rst of the month, instead of whatever todays date may be.
    var theDate = new Date(2011,0,1); //january 1,2011

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,788
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by mrhoo View Post
    Your new Date() is probably December 31-
    It would actually need to be either 29th or 30th to get that result.

    31st would give:

    Jan Mar Mar May May Jul Jul Aug Oct Oct Dec Dec
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right you are, felgall.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •