Result of toJSON() on a date is different between IE8 and IE9+
I don't believe the different number of places is a case of something 'not working'. From https://en.wikipedia.org/wiki/ISO_8601#Times:
Decimal fractions may also be added to any of the three time elements. [...] A fraction may only be added to the lowest order time element in the representation. To denote "14 hours, 30 and one half minutes", do not include a seconds figure. Represent it as "14:30,5", "1430,5", "14:30.5", or "1430.5". There is no limit on the number of decimal places for the decimal fraction. However, the number of decimal places needs to be agreed to by the communicating parties.
Therefore, as toJSON converts a time to ISO-8601 format, and both strings you mention are valid ISO-8601, it appears that both are correct - they just happen to be different.
In terms of a fix, a simple regex replace should do the trick - replace all matches for \.\d+Z
with just Z
(I'm assuming you don't need millisecond-level accuracy!). This should give you a string that works on IE8 even if it was generated from IE9
Prior to ES5, parsing of dates was entirely implementation dependent. IE 8 (and lower) won't parse the ISO 8601 format specified in ES5, so just parse it yourself:
// parse ISO format date like 2013-05-06T22:00:00.000Z
function dateFromISO(s) {
s = s.split(/\D/);
return new Date(Date.UTC(s[0], --s[1]||'', s[2]||'', s[3]||'', s[4]||'', s[5]||'', s[6]||''))
}
Assumes the string is UTC.