Skip to content

Instantly share code, notes, and snippets.

@surajp
Last active August 14, 2020 13:34
Show Gist options
  • Save surajp/d4b9522f979a851ed5cd6213088e9d31 to your computer and use it in GitHub Desktop.
Save surajp/d4b9522f979a851ed5cd6213088e9d31 to your computer and use it in GitHub Desktop.
Format JS date to ISO 8601 date time representation (YYYY-MM-DDThh:mm:ss.sTZD) in UTC
const formatToTwoDigits = (input)=>input?input.toString().padStart(2,'0'):null
const formatToDateTime = (inpDate) => inpDate && inpDate instanceof Date ? `${inpDate.getUTCFullYear()}-${formatToTwoDigits(inpDate.getUTCMonth()+1)}-${formatToTwoDigits(inpDate.getUTCDate())}T${formatToTwoDigits(inpDate.getUTCHours())}:${formatToTwoDigits(inpDate.getUTCMinutes())}:${formatToTwoDigits(inpDate.getUTCSeconds())}.${inpDate.getMilliseconds().toFixed(3)}Z`:null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment