Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-27-2009, 05:13 AM   PM User | #1
lamborn
New to the CF scene

 
Join Date: Nov 2003
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
lamborn is an unknown quantity at this point
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>
lamborn is offline   Reply With Quote
Old 01-27-2009, 07:47 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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 09:25 AM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:18 AM.


Advertisement
Log in to turn off these ads.