getTime() bug

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Russ Chinoy

    getTime() bug

    Has anyone ever noticed that getTime() returns the same value for both today
    (5/31/2005) and tomorrow (6/1/2005)?

    You can quickly see it with this page:

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitl ed Document</title>
    <script>
    function DoIt() {
    today = new Date(2005,5,31) ;
    tomorrow = new Date(2005,6,1);
    alert('today=' + today.getTime() + '\ntomorrow=' + tomorrow.getTim e());
    }
    </script>
    </head>

    <body>
    <a href="#" onClick="DoIt() ">Click me</a>
    </body>
    </html>

    This bug is present in Firefox 1.0.4, IE 6.0, and Netwscape 7.1 (the three
    browsers I have on my machine). It appears to be year-independent, but some
    other month boundaries produce the same problem (such as 3/31 and 4/1).
    Anyone have a workaround for this? It's causing problems where I'm trying to
    compare dates.

    Thanks,
    Russ Chinoy


  • Ulrik Skovenborg

    #2
    Re: getTime() bug

    Russ Chinoy wrote:[color=blue]
    > Has anyone ever noticed that getTime() returns the same value for both today
    > (5/31/2005) and tomorrow (6/1/2005)?
    >
    > You can quickly see it with this page:
    >
    > <html>
    > <head>
    > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    > <title>Untitl ed Document</title>
    > <script>
    > function DoIt() {
    > today = new Date(2005,5,31) ;
    > tomorrow = new Date(2005,6,1);
    > alert('today=' + today.getTime() + '\ntomorrow=' + tomorrow.getTim e());
    > }
    > </script>
    > </head>[/color]

    You have to remember that the month-parameter starts from 0 (January = 0
    , December = 11). That means your script should look like this:

    today = new Date(2005,4,31) ;
    tomorrow = new Date(2005,5,1);

    Before the date's were actually the same because June 31. are the same
    as July the 1st.

    Comment

    • Michael Winter

      #3
      Re: getTime() bug

      On 31/05/2005 19:30, Russ Chinoy wrote:
      [color=blue]
      > Has anyone ever noticed that getTime() returns the same value for both today
      > (5/31/2005) and tomorrow (6/1/2005)?[/color]

      [snip]
      [color=blue]
      > today = new Date(2005,5,31) ;
      > tomorrow = new Date(2005,6,1);[/color]

      Month indicies are zero- not one-based, like arrays. The values you've
      chosen just so happen to represent the same date: June has 30 days, so
      31 wraps to the 1st of July.

      The bug in your code, not the user agent's.

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Evertjan.

        #4
        Re: getTime() bug

        Russ Chinoy wrote on 31 mei 2005 in comp.lang.javas cript:
        [color=blue]
        > Has anyone ever noticed that getTime() returns the same value for both
        > today (5/31/2005) and tomorrow (6/1/2005)?
        >
        > You can quickly see it with this page:[/color]

        No you cannot. It is no bug, but your mistake.
        The mistake is not reading nor heeding the specs:

        [color=blue]
        > today = new Date(2005,5,31) ;[/color]

        this is non-existing 31 June 2005,
        which evaluates it 1 July 2005
        [color=blue]
        > tomorrow = new Date(2005,6,1);[/color]

        this is 1 July 2005

        [color=blue]
        > alert('today=' + today.getTime() + '\ntomorrow=' +
        > tomorrow.getTim e());[/color]

        so they SHOULD be the same.

        The month value is ZERO BASED !

        --
        Evertjan.
        The Netherlands.
        (Replace all crosses with dots in my emailaddress)

        Comment

        • Russ Chinoy

          #5
          Re: getTime() bug

          Thanks for refreshing my memory on this. I hadn't used getTime in so long
          that I forgot about this!

          "Ulrik Skovenborg" <"skovenborg hos gmail dot com"> wrote in message
          news:429cb034$0 $18636$14726298 @news.sunsite.d k...[color=blue]
          > Russ Chinoy wrote:[color=green]
          > > Has anyone ever noticed that getTime() returns the same value for both[/color][/color]
          today[color=blue][color=green]
          > > (5/31/2005) and tomorrow (6/1/2005)?
          > >
          > > You can quickly see it with this page:
          > >
          > > <html>
          > > <head>
          > > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
          > > <title>Untitl ed Document</title>
          > > <script>
          > > function DoIt() {
          > > today = new Date(2005,5,31) ;
          > > tomorrow = new Date(2005,6,1);
          > > alert('today=' + today.getTime() + '\ntomorrow=' + tomorrow.getTim e());
          > > }
          > > </script>
          > > </head>[/color]
          >
          > You have to remember that the month-parameter starts from 0 (January = 0
          > , December = 11). That means your script should look like this:
          >
          > today = new Date(2005,4,31) ;
          > tomorrow = new Date(2005,5,1);
          >
          > Before the date's were actually the same because June 31. are the same
          > as July the 1st.[/color]


          Comment

          • Evertjan.

            #6
            Re: getTime() bug

            Russ Chinoy wrote on 31 mei 2005 in comp.lang.javas cript:[color=blue]
            > "Ulrik Skovenborg" <"skovenborg hos gmail dot com"> wrote in message
            > news:429cb034$0 $18636$14726298 @news.sunsite.d k...[color=green]
            >> Russ Chinoy wrote:[color=darkred]
            >> > Has anyone ever noticed that getTime() returns the same value for
            >> > both[/color][/color]
            > today[color=green][color=darkred]
            >> > (5/31/2005) and tomorrow (6/1/2005)?
            >> >
            >> > You can quickly see it with this page:
            >> >
            >> > <html>
            >> > <head>
            >> > <meta http-equiv="Content-Type" content="text/html;
            >> > charset=iso-8859-1"> <title>Untitl ed Document</title>
            >> > <script>
            >> > function DoIt() {
            >> > today = new Date(2005,5,31) ;
            >> > tomorrow = new Date(2005,6,1);
            >> > alert('today=' + today.getTime() + '\ntomorrow=' +
            >> > tomorrow.getTim e());
            >> > }
            >> > </script>
            >> > </head>[/color]
            >>
            >> You have to remember that the month-parameter starts from 0 (January
            >> = 0 , December = 11). That means your script should look like this:
            >>
            >> today = new Date(2005,4,31) ;
            >> tomorrow = new Date(2005,5,1);
            >>
            >> Before the date's were actually the same because June 31. are the
            >> same as July the 1st.[/color][/color]

            [please do not toppost on usenet]
            [color=blue]
            > Thanks for refreshing my memory on this. I hadn't used getTime in so
            > long that I forgot about this![/color]

            But your "bug"/problem is not in getTime().
            Try this:

            today = new Date(2005,4,31) ;
            tomorrow = new Date(2005,5,1);
            alert(today)
            alert(tomorrow)
            alert(today.get Time())
            alert(tomorrow. getTime())

            --
            Evertjan.
            The Netherlands.
            (Replace all crosses with dots in my emailaddress)

            Comment

            • Matt Kruse

              #7
              Re: getTime() bug

              Russ Chinoy wrote:[color=blue]
              > This bug is present in Firefox 1.0.4, IE 6.0, and Netwscape 7.1 (the
              > three browsers I have on my machine).[/color]

              Readers of this thread should take note.

              If a "bug" exists in multiple competing browsers, and they behave the same,
              then you can usually bet the farm that it's Programmer Error and not a bug.
              ;)

              --
              Matt Kruse



              Comment

              • Grant Wagner

                #8
                Re: getTime() bug

                "Ulrik Skovenborg" <"skovenborg hos gmail dot com"> wrote in message
                news:429cb034$0 $18636$14726298 @news.sunsite.d k...[color=blue]
                > Russ Chinoy wrote:[color=green]
                >> Has anyone ever noticed that getTime() returns the same value for
                >> both today
                >> (5/31/2005) and tomorrow (6/1/2005)?
                >>
                >> You can quickly see it with this page:
                >>
                >> <html>
                >> <head>
                >> <meta http-equiv="Content-Type" content="text/html;
                >> charset=iso-8859-1">
                >> <title>Untitl ed Document</title>
                >> <script>
                >> function DoIt() {
                >> today = new Date(2005,5,31) ;
                >> tomorrow = new Date(2005,6,1);
                >> alert('today=' + today.getTime() + '\ntomorrow=' +
                >> tomorrow.getTim e());
                >> }
                >> </script>
                >> </head>[/color]
                >
                > You have to remember that the month-parameter starts from 0 (January =
                > 0 , December = 11). That means your script should look like this:
                >
                > today = new Date(2005,4,31) ;
                > tomorrow = new Date(2005,5,1);
                >
                > Before the date's were actually the same because June 31. are the same
                > as July the 1st.[/color]

                And some musings as to why June 31 is the same as July 1 in
                J(ava)Script, read this <url:
                http://blogs.msdn.com/ericlippert/ar.../06/53150.aspx />

                "That's why JScript's attitude towards error cases is not "die at the
                first sign of trouble" but rather "muddle along as best you can".
                Assign to an undeclared variable? Declare it dynamically. Forget a
                semicolon? Insert it. Try to create a date for the 31st of September?
                Move it to October 1st. Divide by zero? Return a NaN . Reference an
                element beyond the end of an array? Grow the array. All these things
                that would be compile time or runtime errors in other languages are just
                handled for you in JScript, for better or for worse."

                --
                Grant Wagner <gwagner@agrico reunited.com>
                comp.lang.javas cript FAQ - http://jibbering.com/faq


                Comment

                • Dr John Stockton

                  #9
                  Re: getTime() bug

                  JRS: In article <v32ne.10421$zb .3278@trndny01> , dated Tue, 31 May 2005
                  18:30:51, seen in news:comp.lang. javascript, Russ Chinoy
                  <rchinoy@platin umwebworx.com> posted :
                  [color=blue]
                  >Has anyone ever noticed that getTime() returns the same value for both today
                  >(5/31/2005) and tomorrow (6/1/2005)?[/color]

                  This is an international newsgroup.

                  Therefore, it is unwise to give dates in FF format which is only
                  preferred in one or two countries.

                  You mean 2005-05-31 and 2005-06-01.

                  You are, of course, also in error in your claim; while there *may* be
                  true errors (as distinct from peculiar behaviours) remaining in the
                  javascript date object, it is most unlikely that there could be one in
                  three well-known browsers relating to the fundamentals of the Gregorian
                  Calendar.

                  Comparing dates by comparing the results of getTime is of itself
                  reliable - though date objects can be compared without explicit getTime
                  - but there are pitfalls nearby.

                  --
                  © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 MIME. ©
                  Web <URL:http://www.merlyn.demo n.co.uk/> - w. FAQish topics, links, acronyms
                  PAS EXE etc : <URL:http://www.merlyn.demo n.co.uk/programs/> - see 00index.htm
                  Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

                  Comment

                  Working...