This article explains how to output time in JavaScript using the ISO-8601 data format.
The standard is called ISO-8601 and the format is: YYYY-MM-DDTHH:mm:ss.sssZ
new Date().toISOString();
"2018-08-04T18:44:09.971Z"
JavaScript toISOString() Method
Hereโs how to output time formats based on various ISO standards using JavaScript.
ISO 8601 Format
The ISO 8601 format is widely used for representing dates and times.
const now = new Date();
// Default ISO 8601 format
const iso8601 = now.toISOString();
console.log('ISO 8601:', iso8601);
// For custom formatting (e.g.: 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 is a standardization of part of the time representation based on ISO 8601.
const now = new Date();
// RFC 3339 format
const rfc3339 = now.toISOString().replace(/\.\d{3}Z$/, 'Z');
console.log('RFC 3339:', rfc3339);
Basic Format
Basic format for time without date.
const now = new Date();
// Basic format (hour:minute:second)
const basicFormat = now.toTimeString().split(' ')[0];
console.log('Basic Format:', basicFormat);
These codes get the current date and time and format them according to respective ISO standards. Each output result may differ in actual values based on the current date and time.