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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JS noovie needs a little help.

    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?

    Code:
    <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>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,171
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    You can't just add to a date like that. You have to get the milliseconds, add to them, and then put them "back".

    Code:
    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) );
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,171
    Thanks
    75
    Thanked 4,339 Times in 4,305 Posts
    Want to see an easier way?
    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks so much.. i spent most of the day trying to get this working..



  •  

    Posting Permissions

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