W większości nowszych przeglądarek masz .toISOString()
metodę, ale w IE8 lub starszych można użyć następujących (zaczerpnięte z json2.js Douglas Crockforda):
// Override only if native toISOString is not defined
if (!Date.prototype.toISOString) {
// Here we rely on JSON serialization for dates because it matches
// the ISO standard. However, we check if JSON serializer is present
// on a page and define our own .toJSON method only if necessary
if (!Date.prototype.toJSON) {
Date.prototype.toJSON = function (key) {
function f(n) {
// Format integers to have at least two digits.
return n < 10 ? '0' + n : n;
}
return this.getUTCFullYear() + '-' +
f(this.getUTCMonth() + 1) + '-' +
f(this.getUTCDate()) + 'T' +
f(this.getUTCHours()) + ':' +
f(this.getUTCMinutes()) + ':' +
f(this.getUTCSeconds()) + 'Z';
};
}
Date.prototype.toISOString = Date.prototype.toJSON;
}
teraz można bezpiecznie wywołać metodę `.toISOString().
Staraj się nie upuszczać informacji o strefie czasowej. Zobacz http://stackoverflow.com/a/15302113/277267 –