Javascript Date format date like "2020-10-05T14:48:00.000Z"

Description

Javascript Date format date like "2020-10-05T14:48:00.000Z"

function pad(number) {
  if (number < 10) {
    return '0' + number;
  }/*from ww w. j a  v  a  2s  .  c  o  m*/
  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';
};
let today = new Date('05 October 2020 15:48 UTC')

console.log(today.toISOString())  //  



PreviousNext

Related