using only the built-in methods of the Date object
No. JavaScript will not allow you to output ISO 8601 strings with a custom time zone value .toISOSTring always uses Z (UTC).
You will need to use various getter methods and build the string yourself. Based on How to output a string in ISO 8601 format in JavaScript? and How to convert ISOString to local ISOString in javascript? :
function customISOstring(date, offset) { var date = new Date(date), // copy instance h = Math.floor(Math.abs(offset)/60), m = Math.abs(offset) % 60; date.setMinutes(date.getMinutes() - offset); // apply custom timezone function pad(n) { return n < 10 ? '0' + n : n } return date.getUTCFullYear() + '-' // return custom format + pad(date.getUTCMonth() + 1) + '-' + pad(date.getUTCDate()) + 'T' + pad(date.getUTCHours()) + ':' + pad(date.getUTCMinutes()) + ':' + pad(date.getUTCSeconds()) + (offset==0 ? "Z" : (offset<0 ? "+" : "-") + pad(h) + ":" + pad(m)); }
Bergi Feb 20 2018-12-23T00: 00Z
source share