0

I have an array of dates which returns:

 [Mon Aug 03 2020 00:00:00 GMT+0100 (British Summer Time), Wed Aug 05 2020 00:00:00 GMT+0100 (British Summer Time)] 

I need to convert these into a format as such

["2020-02-13T02:39:51.054", "2020-02-13T02:39:51.054"]

Using a function which would be the best way to do this?

I was unable to recreate problem in stack blitz as the dates in stackblitz by default are in my desired format but the project I am working on requires dates in the first format. I only need to change format in one array instance.

rkras
  • 121
  • 4
  • 16
  • 1
    Use array ``map`` function and Date ``toISOString`` method. ``dates.map(date => new Date(date).toISOString())``. – Sachin Singh Aug 10 '20 at 09:08
  • https://stackoverflow.com/questions/17415579/how-to-iso-8601-format-a-date-with-timezone-offset-in-javascript and https://stackoverflow.com/questions/2573521/how-do-i-output-an-iso-8601-formatted-string-in-javascript – Suraj Rao Aug 10 '20 at 09:12

1 Answers1

0

Use the DatePipe.

let x = ['Mon Aug 03 2020 00:00:00 GMT+0100 (British Summer Time)', 'Wed Aug 05 2020 00:00:00 GMT+0100 (British Summer Time)'];
   let a = x.map(a=>this.datePipe.transform(a, 'YYYY-MM-DDTHH:mm:ss.SSS'));
    console.log(a);
surendra kumar
  • 1,686
  • 11
  • 15