SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast Wouter's Avatar
    Join Date
    Jan 2002
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    add 1 day function problem - UTC shift problem

    The code below results in the following lines unfortunately when the code is executed on Sun 31 Oct 2004 I have an UTC shift? Why ? has it something to do with leap-year ?? -- It took me 3 hours to pinpoint the problem. How to solve ?
    Tx

    test1
    date1 : Sun Oct 31 00:00:00 UTC+0200 2004
    date2 : Sun Oct 31 23:00:00 UTC+0100 2004
    test 2
    date1 : Fri Oct 31 00:00:00 UTC+0100 2003
    date2 : Sat Nov 1 00:00:00 UTC+0100 2003




    <head>
    <script language="JavaScript">
    function add_days(adate,days)
    {return new Date(adate.getTime() + (days * 1000 * 60 * 60 * 24));}

    </script></head>
    <body>
    <script language="JavaScript">
    date1 = new Date(2004,9,31);
    date2 = add_days(date1,1);
    document.write('date1 : '+date1+'<br>date2 : '+date2+'<br>');
    date1 = new Date(2003,9,31);
    date2 = add_days(date1,1);
    document.write('date1 : '+date1+'<br>date2 : '+date2+'<br>');
    </script>

    </body></html>


    do not worry my head is still OK

  2. #2
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Because Sun 31 Oct 2004 is the date when you switch from daylight-saving time to standard time?

  3. #3
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Solution:
    Code:
    <html>
      <head>
        <script language="JavaScript">
          function add_days(adate, days)
          {
            //return new Date(adate.getTime() + (days * 1000 * 60 * 60 * 24));
            return new Date(adate.getYear(), adate.getMonth(), adate.getDate() + 1);
          }
          date1 = new Date(2004, 9, 31);
          date2 = add_days(date1, 1);
          document.write('date1 : ' + date1 + '<br>date2 : ' + date2 + '<br>');
          date1 = new Date(2003, 9, 31);
          date2 = add_days(date1, 1);
          document.write('date1 : ' + date1 + '<br>date2 : ' + date2 + '<br>');
          date1 = new Date(2003, 9, 26);
          date2 = add_days(date1, 1);
          document.write('date1 : ' + date1 + '<br>date2 : ' + date2 + '<br>');
        </script>
      </head>
      <body>
      </body>
    </html>

  4. #4
    SitePoint Enthusiast Wouter's Avatar
    Join Date
    Jan 2002
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tx
    the function that I used is thus dangerous and tricky

    with your solution I was afraid that it would give me 32 Oct - a date that doesn't exists but it is not the case

    new Date(adate.getYear(), adate.getMonth(), adate.getDate() + 1);


    Tx - Have a nice year ending

  5. #5
    Sultan of Ping jofa's Avatar
    Join Date
    Mar 2002
    Location
    SvÝ■jˇ­
    Posts
    4,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Wouter
    ... Have a nice year ending
    I will

    And what comes after Dec 31 2002?

    var d = new Date(2002, 11, 31);
    d = new Date(d.getYear(), d.getMonth(), d.getDate() + 1);
    alert(d.toString());
    // javascript is kind of smart...


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
  •