Ad

Convert Timestamp String With Timezone To 'dd/mm/yyyy' Format

- 1 answer

I have an array which has the following dates as entries:

Mon Feb 17 2020 00:00:00 GMT+0530 (India Standard Time)
Tue Feb 18 2020 00:00:00 GMT+0530 (India Standard Time)
Wed Feb 19 2020 00:00:00 GMT+0530 (India Standard Time)
Thu Feb 20 2020 00:00:00 GMT+0530 (India Standard Time)

How can I remove the time zone and convert this date to dd/mm/yyyy format? I need these with the / and return them to a grid column by column.

Example

 -               |[17/02/2020]   |   [18/02/2020]  |  [19/02/2020]
 1 -----         |     -         |    -            |  -
 2 -----         |     -         |    -            |  -
Ad

Answer

You may drop timezone part, convert that string into date and then back to the string of the suitable format:

const dates = [
        'Mon Feb 17 2020 00:00:00 GMT+0530 (India Standard Time)',
        'Tue Feb 18 2020 00:00:00 GMT+0530 (India Standard Time)',
        'Wed Feb 19 2020 00:00:00 GMT+0530 (India Standard Time)',
        'Thu Feb 20 2020 00:00:00 GMT+0530 (India Standard Time)',
      ],
      result = dates.map(date => new Date(date.replace(/ GMT.*/,'')).toLocaleDateString('en-GB'))
      
console.log(result)      

Ad
source: stackoverflow.com
Ad