Date.prototype.toISOString()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

La méthode toISOString() renvoie une chaîne de caractères au format ISO (ISO 8601 Extended Format), qui peut être décrite de cette façon : YYYY-MM-DDTHH:mm:ss.sssZ (toujours longue de 24 caractères) ou de cette façon ±YYYYYY-MM-DDTHH:mm:ss.sssZ (27 caractères). Le fuseau horaire est toujours UTC, comme l'indique le suffixe « Z » (pour zéro décalage avec UTC).

Exemple interactif

const event = new Date("05 October 2011 14:48 UTC");
console.log(event.toString());
// Expected output: "Wed Oct 05 2011 16:48:00 GMT+0200 (CEST)"
// Note: your timezone may vary

console.log(event.toISOString());
// Expected output: "2011-10-05T14:48:00.000Z"

Syntaxe

js
dateObj.toISOString();

Valeur de retour

Une chaîne de caractères représentant la date indiquée au format ISO 8601 selon le temps universel.

Exemples

Utiliser toISOString()

js
var aujourdhui = new Date("05 October 2011 14:48 UTC");

console.log(aujourdhui.toISOString()); // Renvoie "2011-10-05T14:48:00.000Z"

L'exemple ci-dessus analyse une chaîne de caractères non-standard, qui peut donc être incorrectement intérprété par des navigateurs n'utilisant pas Gecko.

Prothèse d'émulation (polyfill)

Cette méthode fut standardisée avec la cinquième édition d'ECMAScript. Afin d'utiliser cette méthode avec les moteurs qui n'en disposent pas nativement, on pourra utiliser ce fragment de code :

js
if (!Date.prototype.toISOString) {
  (function () {
    function pad(number) {
      if (number < 10) {
        return "0" + number;
      }
      return number;
    }

    Date.prototype.toISOString = function () {
      return (
        this.getUTCFullYear() +
        "-" +
        pad(this.getUTCMonth() + 1) +
        "-" +
        pad(this.getUTCDate()) +
        "T" +
        pad(this.getUTCHours()) +
        ":" +
        pad(this.getUTCMinutes()) +
        ":" +
        pad(this.getUTCSeconds()) +
        "." +
        (this.getUTCMilliseconds() / 1000).toFixed(3).slice(2, 5) +
        "Z"
      );
    };
  })();
}

Spécifications

Specification
ECMAScript® 2025 Language Specification
# sec-date.prototype.toisostring

Compatibilité des navigateurs

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
toISOString

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

Voir aussi