...

View Full Version : JS noovie needs a little help.



Vt102
09-19-2011, 04:54 PM
Hello.. somewhat new to JS and trying to learn as much as a I can.

I've been trying to do a little tool that will calculate dates for me, but I can;t get it to work properly.

The tool is meant to take a date and loop it by 26 2 week intervals. but i can only get today's date to show up. Any suggestions on how to make this work?


<script language="JavaScript">

var date = new Date();
for(var index = 0; index < 26; index++) {
document.write((date.getMonth()+1) + " " + date.getDate() + " " + date.getFullYear() + "<br/>");
date = date + (1000 * 60 * 60 * 24 * 14);
}
</script>

Old Pedant
09-19-2011, 07:50 PM
You can't just add to a date like that. You have to get the milliseconds, add to them, and then put them "back".



var date = new Date();
for(var index = 0; index < 26; index++) {
document.write((date.getMonth()+1) + " " + date.getDate() + " " + date.getFullYear() + "<br/>");
date.setTime( date.getTime() + (1000 * 60 * 60 * 24 * 14) );
}

Old Pedant
09-19-2011, 07:52 PM
Want to see an easier way?


var date = new Date();
for(var index = 0; index < 26; index++) {
document.write((date.getMonth()+1) + " " + date.getDate() + " " + date.getFullYear() + "<br/>");
date.setDate( date.getDate() + 14 );
}

Yeah, it works. JS does the wrapping into new month (and year) for you.

Vt102
09-19-2011, 07:52 PM
thanks so much.. i spent most of the day trying to get this working..

:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum