JavaScript Date() issue since Chrome 67

Rik Driever
Jul 13, 2018 · 4 min read

The issue

DateTime dateTime = (DateTime)new SqlDateTime();
dateTime = dateTime.AddMinutes(10);
JavaScriptSerializer js = new JavaScriptSerializer();
string jsonOutput = js.Serialize(dateTime);
new Date(-2208991800000);
Sun Dec 31 1899 23:29:32 GMT+0019 (Midden-Europese standaardtijd)

The fix

var unixTime = new Date(0);
var jsonTime = new Date(-2208991800000);
var correction = (jsonTime.getTimezoneOffset() — unixTime.getTimezoneOffset()) * 60000;
new Date(jsonTime.getTime() + correction);
Mon Jan 01 1900 00:10:32 GMT+0019 (Midden-Europese standaardtijd)
var jsonTime = new Date(-2208991800000);
var correctedJsonTime = new Date(jsonTime.getTime() — (-1 * 60 * 60 * 1000));
correctedJsonTime.toUTCString();
“Mon, 01 Jan 1900 00:10:00 GMT”
new Date(“Mon, 01 Jan 1900 00:10:00 GMT”);
Mon Jan 01 1900 00:29:32 GMT+0019 (Midden-Europese standaardtijd)
new Date(“1900–01–01T00:10:00”);Mon Jan 01 1900 00:10:00 GMT+0019 (Midden-Europese standaardtijd)

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade