Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2003
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problem adding a year in date script

    Hello,
    I am trying to modify the following script.
    What I am trying to do is add months to a date which is dependent on user input on a form The user input field is "select2".
    This currently works fine if your input in "select 2" does not take you into the next year <as in adding nine months to October>. If it does, the year doesn't move forward one year, it remains as-is <says 2009>
    Any thoughts - I am trying to learn.


    <!-- Begin
    var months=new Array(13);
    months[1]="January";
    months[2]="February";
    months[3]="March";
    months[4]="April";
    months[5]="May";
    months[6]="June";
    months[7]="July";
    months[8]="August";
    months[9]="September";
    months[10]="October";
    months[11]="November";
    months[12]="December";
    var time=new Date();
    var lmonth=months[time.getMonth() + 1];
    var date=time.getDate();
    var year=time.getFullYear();
    if (year < 2000)
    year = year + 1900;
    document.write("<center>" + lmonth + " ");
    document.write(date + ", " + year + "</center>");
    // End -->
    </SCRIPT>
    <!-- Script Size: 1.00 KB -->
    </td>
    <td width="10%" bgcolor="#99CCFF">To:</td>
    <td width="54%"><SCRIPT LANGUAGE="JavaScript1.2">


    <!-- Begin
    var months=new Array(13);
    months[1]="January";
    months[2]="February";
    months[3]="March";
    months[4]="April";
    months[5]="May";
    months[6]="June";
    months[7]="July";
    months[8]="August";
    months[9]="September";
    months[10]="October";
    months[11]="November";
    months[12]="December";
    var time=new Date();
    var lmonth=months[time.getMonth() + $FORM{'select2'}];
    var date=time.getDate();
    var year=time.getFullYear();
    if (year < 2000)
    year = year + 1900;
    document.write("<center>" + lmonth + " ");
    document.write(date + ", " + year + "</center>");
    // End -->
    </SCRIPT>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,240
    Thanks
    203
    Thanked 2,554 Times in 2,532 Posts
    Your objective is not entirely clear, but if the idea is that the user enters a number of months ahead to generate a future date, then in principle:-

    if (currentMonthValue + numberOfMonthsAhead > 11) { // months are 0-11
    futureYear = currentYear +1;
    }

    <SCRIPT LANGUAGE="JavaScript1.2"> is deprecated - use <script type = "text/javascript"> instead. This should give a clue that this is old code. Of course, <script type = "text/javascript"> must be placed at the top of the script, and not in the middle as you have it.

    Also, note that the document.write command must be carried out during the loading of the page. So, if it is attached to any event that executes after the page has loaded, then the whole page will be replaced with the contents of the document.write command.

    You cannot have multiple scripts on the same page with the same function and/or global variable names, as the second will simply over-write the first.


    “A man ceases to be a beginner in any given science and becomes a master in that science when he has learned that he is going to be a beginner all his life.” Robin G. Collingwood (English Philosopher, 1889-1943)
    Last edited by Philip M; 01-27-2009 at 10:25 AM.


  •  

    Posting Permissions

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