Dieser Artikel erklärt, wie man die Zeit in JavaScript mit dem ISO-8601-Datenformat ausgibt.
Der Standard heißt ISO-8601 und das Format ist: YYYY-MM-DDTHH:mm:ss.sssZ
new Date().toISOString();
"2018-08-04T18:44:09.971Z"
JavaScript toISOString() Method
Hier ist, wie man Zeitformate basierend auf verschiedenen ISO-Standards mit JavaScript ausgibt.
ISO 8601 Format
Das ISO 8601-Format wird weithin für die Darstellung von Datum und Uhrzeit verwendet.
const now = new Date();
// Standard-ISO 8601-Format
const iso8601 = now.toISOString();
console.log('ISO 8601:', iso8601);
// Für benutzerdefinierte Formatierung (z.B.: YYYY-MM-DDTHH:mm:ssZ)
const iso8601Custom = now.toISOString().replace(/\.\d{3}Z$/, 'Z');
console.log('Custom ISO 8601:', iso8601Custom);
RFC 3339 Format
RFC 3339 ist eine Standardisierung eines Teils der auf ISO 8601 basierenden Zeitdarstellung.
const now = new Date();
// RFC 3339-Format
const rfc3339 = now.toISOString().replace(/\.\d{3}Z$/, 'Z');
console.log('RFC 3339:', rfc3339);
Basisformat
Basisformat für Zeit ohne Datum.
const now = new Date();
// Basisformat (Stunde:Minute:Sekunde)
const basicFormat = now.toTimeString().split(' ')[0];
console.log('Basic Format:', basicFormat);
Diese Codes holen das aktuelle Datum und die aktuelle Uhrzeit und formatieren sie gemäß den jeweiligen ISO-Standards. Jedes Ausgabeergebnis kann in tatsächlichen Werten basierend auf dem aktuellen Datum und der aktuellen Uhrzeit unterschiedlich sein.