convert dateTime format in react js

i have this date :

Mon Aug 23 2021 15:03:00 GMT+0430 (Iran Daylight Time)

how to convert this format:

2021-10-10T00:00:00

Answer

You can do it as follows:

new Date('Mon Aug 23 2021 15:03:00 GMT+0430 (Iran Daylight Time)')
.toISOString().split('.')[0]

=> '2021-08-23T10:33:00'

If you don’t prefer the native way of converting it you can use the library Moment.js.

Your code would look as follows:

moment('Mon Aug 23 2021 15:03:00 GMT+0430 (Iran Daylight Time)')
.format('YYYY-MM-DDTHH:mm:ss');

=> '2021-08-23T10:33:00'

If you don’t want to keep the hours, minutes and seconds these examples will work.

Native way:

new Date('Mon Aug 23 2021 15:03:00 GMT+0430 (Iran Daylight Time)').toISOString().split('T')[0] + 'T00:00:00'

=> '2021-08-23T10:33:00'

With Moment.js:

moment('Mon Aug 23 2021 15:03:00 GMT+0430 (Iran Daylight Time)')
.format('YYYY-MM-DDT00:00:00');

=> '2021-08-23T10:33:00'

Edit

As RobG mentioned in the comments, the toISOString() function will return the UTC time format. So even one more reason to use moment.js!