http://jsfiddle.net/bcg47/3/
This bug only occurs in new versions of Chrome. Its been tested on mac with Chrome version 29.0.1547.57 and on Windows with Chrome version 29.0.1547.62 m.
The script above is pretty simple. Here is the code:
<script>
function displayDate() {
console.log('date1', new Date('2013-08-30T14:06:56-04:00'))
setTimeout(function() { displayDate(); }, 1000);
}
displayDate();
</script>
<iframe src="http://jsfiddle.net/wzqgN/2/" width="100%" height="300"></iframe>
The first thing that is loaded is a javascript function and a call to that function. In the function I am logging the value returned back from the javascript new Date() constructor (passing Aug. 30). Simple enough, and as you can see in your console this works the first time. The problem occurs when the iframe is loaded. The iframe has a src of http://jsfiddle.net/wzqgN/2/. In this iframe jsfiddle there is also nothing special going on. It has the following code:
TargetDate = "12/31/2020 5:00 AM";
var dthen = new Date(TargetDate);
As you can see in the console the first line of output is:
date1 Fri Aug 30 2013 14:06:56 GMT-0400 (EDT)
But as soon as the iframe is loaded and the other new Date() is called, then the output changes to:
date1 Thu Dec 31 2020 05:00:00 GMT-0500 (EST)
I am not sure what is going on here. My initial thought is that this is a browser bug but I wanted to ask here to make sure. In the script I am using the iframe is actually set to a different domain than what the code is running on. That is what makes me wonder even more what the heck is going on. I'm not understanding how a script from an iframe, let alone a cross domain script, can affect the original script that calls it. Any help is greatly appreciated. Thanks.
In certain contexts Chrome will cache Date() constructors. This is a recent bug in Chrome, as shown in the following bug report:
https://code.google.com/p/chromium/issues/detail?id=280531
This bug report provides a code example detailing the same problem - cached Date() constructors when when switching between windows under the same domain. In your case, the <iframe> is creating a new window context and triggering the same bug.
As a workaround you can use Date.parse() and setTime() to bypass Chrome's internal Date constructor caching.
var date1 = new Date();
var timestamp = Date.parse('2013-08-30T14:06:56-04:00');
date1.setTime(timestamp);
http://jsfiddle.net/bcg47/4/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With