Results 1 to 4 of 4

Thread: adding days to date

  1. #1
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default adding days to date

    I have a weird problem with adding days to a date.
    To add days i use: newEnddate.setDate(newEnddate.getDate()+extra);

    When i use a fix number it works fine.
    (var extra = 35)

    But if i ask the user how many days he want to add to a date, the new date isn't correct.
    (var extra = prompt("How many days?","10");
    in the prompt i change 10 to 35

    Here are the results:
    If i use fix number to add (example 35)
    old date: 2009/7/12
    new date : 2009/8/16
    the is correcy

    But if i use a prompt and ask there a number (again 35)
    old date: 2009/7/12
    new date: 2012/11/17

    If i print the var extra, for both i get 35...

    How is this possible
    Last edited by NDK; 06-30-2009 at 01:44 PM.

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,810
    Thanks
    2
    Thanked 423 Times in 417 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    /*<![CDATA[*/
    function zDate(){
     var today=new Date();
     var extra = prompt("How many days?","10");
    
     today.setDate(today.getDate()+parseInt(extra));
     alert(today);
    }
    /*]]>*/
    </script></head>
    
    <body onload="zDate();">
    
    </body>
    
    </html>
    the prompt returns a string

  3. The Following User Says Thank You to vwphillips For This Useful Post:

    NDK (06-30-2009)

  4. #3
    Join Date
    Feb 2009
    Posts
    23
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    yeah, that was the only thing i could think the problem was. Can i convert it to an integer?

    did not know there was a difference between string and int in javascript...

  5. #4
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,810
    Thanks
    2
    Thanked 423 Times in 417 Posts

    Default

    Code:
    Can i convert it to an integer?
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    /*<![CDATA[*/
    function zDate(){
     var today=new Date();
     var extra = prompt("How many days?","10");
    
     today.setDate(today.getDate()+parseInt(extra));
     alert(today);
    }
    /*]]>*/
    </script></head>
    
    <body onload="zDate();">
    
    </body>
    
    </html>

Bookmarks

Posting Permissions

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