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 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2002
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript bug....

    I've been pouring hours into a script, wondering why it was no longer working and then I figured "wait a minute, maybe it's not my fault." And guess what, it isn't. I think I've been able to isolate the bug, but why does it happen? The problem is with the IE's date object and todays date 2002-07-31. The set/get month functions go haywire and do not return proper values.
    Here's a sample function:

    function doanything()
    {
    today = new Date(2002, 07, 31, 0, 0, 0);
    today.setMonth(10);
    alert(today.getDate()+'/'+(today.getMonth()+1)+'/'+today.getFullYear());
    today.setMonth(9);
    alert(today.getDate()+'/'+(today.getMonth()+1)+'/'+today.getFullYear());
    }

    The first alert box returns 1-12-2002 and the second 1-10-2002.
    As you can see the first date is off by one..... If I initialize the date to another date say 2002-07-30, the problem goes away.
    Hum... My head hursts..... What others bugs should I be on the lookout on IE?
    V

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    New York, USA
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your original date is july 31...
    you set the month to november, but since there's no November 31, it rolls to December 1. (1-12-2002)
    Then you set that date's month to October so it returns 1-10-2002.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    185
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's how the Date object is designed to work.

    You might find this article useful: http://www.brainjar.com/js/calendar/. It explains how you can add custom methods to the Date object to add or subtract days, months and years.

  • #4
    New Coder
    Join Date
    Jul 2002
    Posts
    46
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh, I see...
    That logic seems counterintuitive though... I understand that happening with days. For instance, if you're on the first, and your set the the number of days to +32, then I would expect a rollover. But to do that on the months seems... wrong...
    Ok, thanks alot for your help...
    I was panicking for a while there...
    V

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Originally posted by Vermelh0
    Oh, I see...
    That logic seems counterintuitive though... I understand that happening with days. For instance, if you're on the first, and your set the the number of days to +32, then I would expect a rollover. But to do that on the months seems... wrong...
    Ok, thanks alot for your help...
    I was panicking for a while there...
    V
    Think of it this way, the date field will always contain a valid date. Setting one part of the date field to something that would cause another part to be invalid will cause the invalid part to be "adjusted".


  •  

    Posting Permissions

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