Date Object Doesn't Work.

Garrett Smith dhtmlkitchen at gmail.com
Wed May 28 16:36:39 PDT 2014


Yesterday, I posed the question about an event starts at 2am, 2014,
and ends immediately
after 3 am, on May 9, how long is it?

The event, in that case, should be 23 hours due to 1h DST loss.
("spring forward"). But then I noticed more anomalies:

<!doctype html>
<head>
<title>test DST 1</title>

<style>
head, script {
  display: block;
  white-space: pre;
  font-family: monospace;
}
</style>
<script>
var d = new Date(2013, 02, 10),
    d2 = new Date(2013, 02, 10);
d.setHours(1);
d2.setHours(2);

function showResult() {
  document.getElementById("out").
    firstChild.data =
      "d: " + d.toString()
      + "\nd2: " + d2.toString()
      + "\nd2 - d: " + (d2 - d)
}
window.onload = showResult;
</script>
</head>

<h2>result</h2>
<pre id='out'>-</pre>
</body>
</html>

Chrome, Opera:
d: Sun Mar 10 2013 01:00:00 GMT-0800 (PST)
d2: Sun Mar 10 2013 01:00:00 GMT-0800 (PST)
d2 - d: 0

Safari:
d: Sun Mar 10 2013 01:00:00 GMT-0800 (PST)
d2: Sun Mar 10 2013 03:00:00 GMT-0700 (PDT)
d2 - d: 3600000

Firefox:
d: Sun Mar 10 2013 01:00:00 GMT-0800 (PDT)
d2: Sun Mar 10 2013 03:00:00 GMT-0700 (PDT)
d2 - d: 3600000

What are Opera and Chrome doing?

Here in CA, we recognize DST, and so 0 would be wrong, as 2am gets set
forward to 3am but the offset changes, too.

I believe Safari is correct here.

Now from 2am - 3am, the difference should be 0 because 2am gets set
forward to 3am.

<!doctype html>
<head>
<title>test DST 2</title>

<style>
head, script {
  display: block;
  white-space: pre;
  font-family: monospace;
}
</style>
<script>
var r = new Date(2013, 02, 10),
    r2 = new Date(2013, 02, 10);
r.setHours(2);
r2.setHours(3);

function showResult() {
  document.getElementById("out").
    firstChild.data =
      "r: " + r.toString()
      + "\nr2: " + r2.toString()
      + "\nr2 - d: " + (r2 - r)
}
window.onload = showResult;
</script>
</head>

<h2>result</h2>
<pre id='out'>-</pre>
</body>
</html>


Firefox:
r: Sun Mar 10 2013 03:00:00 GMT-0700 (PDT)
r2: Sun Mar 10 2013 03:00:00 GMT-0700 (PDT)
r2 - d: 0

Safari:
r: Sun Mar 10 2013 03:00:00 GMT-0700 (PDT)
r2: Sun Mar 10 2013 04:00:00 GMT-0700 (PDT)
r2 - d: 3600000

Opera, Chrome:
r: Sun Mar 10 2013 01:00:00 GMT-0800 (PST)
r2: Sun Mar 10 2013 03:00:00 GMT-0700 (PDT)
r2 - d: 3600000

Firefox seems to get this right, forwarding 2am to 3am.

What is going on with the others?
-- 
Garrett
@xkit
ChordCycles.com
garretts.github.io


More information about the es-discuss mailing list