Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. Javascript flaw

Javascript flaw

Scheduled Pinned Locked Moved Clever Code
helpjavascripthtmlcom
6 Posts 3 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Siderite Zaqwedex
    wrote on last edited by
    #1

    This is my blog entry about it: Weirdest Javascript bug ever[^] Essentially, if you do a 'var d=new Date(2005,2,27);' in javascript you will get the date 26/03/2005 in both IE and FireFox for Windows XP. I believe this to be an error in the way Windows handles dates, but Javascript should have taken this into account. Beat that!:cool:

    ---------- Siderite

    T 1 Reply Last reply
    0
    • S Siderite Zaqwedex

      This is my blog entry about it: Weirdest Javascript bug ever[^] Essentially, if you do a 'var d=new Date(2005,2,27);' in javascript you will get the date 26/03/2005 in both IE and FireFox for Windows XP. I believe this to be an error in the way Windows handles dates, but Javascript should have taken this into account. Beat that!:cool:

      ---------- Siderite

      T Offline
      T Offline
      TNCaver
      wrote on last edited by
      #2

      This is not a bug nor error in either the Javascript nor Windows XP. The month argument is a number between 0 and 11, where 0 is January. In your example, if you want February 27, your code should be 'var d=new Date(2005,1,27);'. Why the creators of Javascript decided to make the months zero-based and the days 1-based I'll never understand, but that's the way it is. On a side note, your exact code returns 'Sunday March 27, 2005 12:00:00 AM' if I display d.toLocalString() on my Windows XP box using IE6, and not 26/03/2005. -- modified at 10:09 Wednesday 4th October, 2006

      S M 2 Replies Last reply
      0
      • T TNCaver

        This is not a bug nor error in either the Javascript nor Windows XP. The month argument is a number between 0 and 11, where 0 is January. In your example, if you want February 27, your code should be 'var d=new Date(2005,1,27);'. Why the creators of Javascript decided to make the months zero-based and the days 1-based I'll never understand, but that's the way it is. On a side note, your exact code returns 'Sunday March 27, 2005 12:00:00 AM' if I display d.toLocalString() on my Windows XP box using IE6, and not 26/03/2005. -- modified at 10:09 Wednesday 4th October, 2006

        S Offline
        S Offline
        Siderite Zaqwedex
        wrote on last edited by
        #3

        You didn't quite get it. The month is 0 based, I know that, but the date was offset by one day. And the reason that is working on your end is because you're not in Romania, where 27.03.2005 is a daylight saving day. I assure you that on my computer this is exactly what happens. Imagine trying to understand what was wrong in the code: C# ,SQL and Javascript and the problem to be something like this. To make it more clear: new Date(27,2,2005) returns 26 March 2005 23:00:00... But thanks, I didn't realize that this might never happen on computers set in other countries. We have a non standard daylight saving. I will update my blog.

        ---------- Siderite

        T 1 Reply Last reply
        0
        • T TNCaver

          This is not a bug nor error in either the Javascript nor Windows XP. The month argument is a number between 0 and 11, where 0 is January. In your example, if you want February 27, your code should be 'var d=new Date(2005,1,27);'. Why the creators of Javascript decided to make the months zero-based and the days 1-based I'll never understand, but that's the way it is. On a side note, your exact code returns 'Sunday March 27, 2005 12:00:00 AM' if I display d.toLocalString() on my Windows XP box using IE6, and not 26/03/2005. -- modified at 10:09 Wednesday 4th October, 2006

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          TNCaver wrote:

          Why the creators of Javascript decided to make the months zero-based and the days 1-based I'll never understand, but that's the way it is.

          Probably because that's what struct tm in the C standard library does. Now, why that does it is a mystery. It's presumably been inherited from original UNIX. struct tm is a real dog's dinner. I divine that you are in the UK. I think the other 'problem' is appearing because the argument for date's constructor is interpreted as UTC, while toLocalString gives you the value in the local time zone.

          Stability. What an interesting concept. -- Chris Maunder

          1 Reply Last reply
          0
          • S Siderite Zaqwedex

            You didn't quite get it. The month is 0 based, I know that, but the date was offset by one day. And the reason that is working on your end is because you're not in Romania, where 27.03.2005 is a daylight saving day. I assure you that on my computer this is exactly what happens. Imagine trying to understand what was wrong in the code: C# ,SQL and Javascript and the problem to be something like this. To make it more clear: new Date(27,2,2005) returns 26 March 2005 23:00:00... But thanks, I didn't realize that this might never happen on computers set in other countries. We have a non standard daylight saving. I will update my blog.

            ---------- Siderite

            T Offline
            T Offline
            TNCaver
            wrote on last edited by
            #5

            Siderite Zaqwedex wrote:

            We have a non standard daylight saving.

            I'll say. What is a daylight saving day?

            S 1 Reply Last reply
            0
            • T TNCaver

              Siderite Zaqwedex wrote:

              We have a non standard daylight saving.

              I'll say. What is a daylight saving day?

              S Offline
              S Offline
              Siderite Zaqwedex
              wrote on last edited by
              #6

              A day in which time is moved forward or backward with one hour. http://en.wikipedia.org/wiki/Daylight_saving[^]

              ---------- Siderite

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups