1

When I run var d = new Date; alert(d.toLocaleString()) in jsFiddle, or in the w3Schools "Tryit Editor", it is able to detect the location that I have set my computer to and report the correct time for that location (or at least, the one I had chosen at page load), taking into account its time zone and whether it uses Daylight Saving Time. The getTimezoneOffset() method also reports the correct offset for my chosen location. However, when I use this javascript in my webpage and leave the browser to interpret it, I get different behaviors by browser:

  • In Chrome, it always alerts the time in my real-life location, rather than the one I've chosen in the OS.
  • In Firefox, it fails to take DST into account in any location besides my real-life one, and reports the time one hour before my system time.
  • In Safari, it works like jsfiddle.

Can anyone explain these discrepancies, and suggest how I can get my website to behave like jsfiddle does?

edit: http://jsfiddle.net/5p8tL/, versus:

<html>
<body>
<script>
     var d = new Date;
     alert(d.toLocaleString());
</script>
</body>
</html>
Ben
  • 45
  • 1
  • 5
  • jsFiddle IS in your browser - it's just a web page. I'm not understanding what is different between your scenarios. Can you show the actual code and the actual jsFiddle? – jfriend00 Jul 13 '14 at 17:55
  • It's always the browser interpreting it, no matter which site it comes from. – Barmar Jul 13 '14 at 17:55
  • Are you passing any parameters when you call `toLocaleString`? What OS, and what do you mean by the "real life" locale? – Matt Johnson-Pint Jul 13 '14 at 18:15
  • @jfriend00, Barmar -- See, I thought so, which makes the discrepancy even more confusing. – Ben Jul 13 '14 at 18:17
  • @MattJohnson: no parameters. Mac OS 10.5 (yeah, I know). By "real life locale" I mean the actual, physical location I occupy in meatspace. – Ben Jul 13 '14 at 18:20
  • And you've varied your OS settings precisely how? What output do you actually get? Examples would be useful. – Matt Johnson-Pint Jul 13 '14 at 18:21
  • Also, are you saying that you get the same results in jsfiddle in all three browsers but you get different results on your own web page in the same browsers? If so, please provide samples of the differences you're seeing. – Matt Johnson-Pint Jul 13 '14 at 18:24
  • @MattJohnson -- yes, same results in jsFiddle in all 3 browsers. Example: if I am in London and it's 12 noon, it's 4AM in Palo Alto. If I alter the "time zone" system setting (sorry, can't post an image without 10 reputation) to say I am in California, which changes my system clock to say 4AM, the jsFiddle will report 4AM in all 3 browsers. The non-fiddle webpage will report 4AM in Safari, 3AM in Firefox, and 12 noon in Chrome. – Ben Jul 13 '14 at 18:46
  • Ok, well time zone settings are not the same as locale settings (like mm/dd/yyyy vs dd/mm/yyyy). Are you just talking about time zones? – Matt Johnson-Pint Jul 13 '14 at 18:47
  • @MattJohnson yes, sorry, just time zone. I will update the question. – Ben Jul 13 '14 at 18:54
  • Are you fully restarting the browsers before checking? Some browsers won't pick up time zone changes until they are shut down and restarted. – Matt Johnson-Pint Jul 13 '14 at 18:57
  • @MattJohnson -- Thanks Matt, all 3 browsers report the correct time when restarted! I'm still confused about how jsFiddle reported the correct time without restarts, though -- after all, like the other commenters said, jsFiddle relies on your browser to interpret the javascript too, right? – Ben Jul 13 '14 at 19:07
  • @MattJohnson btw if you want to post "restart your browser" as an answer I'll accept it :) – Ben Jul 13 '14 at 19:08

1 Answers1

1

The discrepancies you're seeing are likely due to the way that each browser reads the time zone settings from the system.

  • Chrome only reads the time zone when it is first started. If you change the system time zone, it won't pick up on those changes until it is restarted.

  • Internet Explorer on Windows will read the time zone on startup, and also listens for messages from the OS that alert it to the time zone changing. I don't have access to a Mac at the moment, but I assume that Safari on OSX does something similar.

  • Firefox is buggy in this regard. It appears to read the time zone settings at some interval, because if you change them and check without restarting, it doesn't see it right away. But then eventually it does. Also, there seem to be differences in how the time zone is applied between toLocaleString and toString when it's in this limbo state. If you wait awhile, then eventually it all syncs up. There are some other known issues with Firefox reporting time zones incorrectly, as I describe as part of this blog post. However this is the first time I've heard of it not applying DST properly on the clock itself, and I cannot reproduce that on Windows so it may be an Firefox-OSX specific bug. It wouldn't surprise me.

At any rate, it's probably a good idea to fully restart the browser after changing time zone.

As to why you don't see the same behavior in jsFiddle as you do on your own web page - I have no idea. In principal, that doesn't make any sense. They should have identical behaviors. Perhaps those instances were being restarted while the others were not? Just guessing.

Also, you should probably use new Date() instead of new Date. It's valid, but JSLint will complain. See also this post.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575