...

View Full Version : problem adding a year in date script



lamborn
01-27-2009, 05:13 AM
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>

Philip M
01-27-2009, 07:47 AM
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)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum