0

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

zahra zamani
  • 1,323
  • 10
  • 27

1 Answers1

2

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!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Flashtube
  • 209
  • 1
  • 6
  • 1
    The "native way" returns UTC date and time masquerading as local, it is not a straight conversion of format. – RobG Aug 30 '21 at 15:14
  • 1
    The issue with *toISOString* isn't a reason to use a library, it's a reason not to use *toISOString* the way it's used here. ;-) – RobG Aug 31 '21 at 05:52
  • You can think of it in that way! Its just smarter to use moment.js because it offers more possibilities and flexability, but thank you for your input! – Flashtube Aug 31 '21 at 06:09