s.n s.n - 22 days ago 7
Javascript Question

How to extract mm/dd/yyyy from ISO-8601 date format 2014-12-29T22:04:56.000Z in JavaScript?

I have this information: 2014-12-29T22:04:56.000Z

I would like to get the 2014-12-29 part of it in 12/29/2014 format.

How can I do it in javascript?

Thanks!

Answer

You could format the date like this to get your desired formatting.

var date = new Date("2014-12-29T22:04:56.000Z");

function pad(number) {
  if (number < 10) {
    return '0' + number;
  }
  return number;
}

function formatDate() {
  return pad(date.getUTCMonth() + 1) +
    '/' + pad(date.getUTCDate()) + '/' + date.getUTCFullYear();
}
console.log(formatDate())

Comments