-3

I get from API request the date in this format: 2021-03-18T15:08:52.000Z
How can I convert it to this format: 2021-03-18 15:08:52

Kleaaa
  • 19
  • 3
  • 3
    What have you tried so far? – NullDev Mar 18 '21 at 20:58
  • Does this answer your question? [Format JavaScript date as yyyy-mm-dd](https://stackoverflow.com/questions/23593052/format-javascript-date-as-yyyy-mm-dd) – Mike Ezzati Mar 18 '21 at 21:02
  • `new Date('2021-03-18T15:08:52.000Z').toLocaleString('defualt', {hour12: false}).replace(',','')` should work for you. – quicVO Mar 18 '21 at 21:06

1 Answers1

0

You could use JavaScript substr function to extracts specific parts of a string, e.g.:

function convertDate(date) {
  return date && date.length == 24 ? date.substr(0, 10) + ' ' + date.substr(11, 8) : null;
}
let date1 = '2021-03-18T15:08:52.000Z';
let date2 = convertDate(date1);
console.log(date2);
Alessio Cantarella
  • 4,659
  • 3
  • 21
  • 29