When page is loaded, my input box gets automatically populated with a date time. This date time can be in any valid format like DD-MMM-YYYY - HH:mm
, MMM DD, YYYY - HH:mm
, etc and I wanted to pick up this date, parse it and add some hours to it.
But when I try to parse a date which is something like 19-02-2020 - 00:00
(DD-MM-YYYY - HH:mm
), it gives me an invalid Date.
I have tried
new Date('19-02-2020 - 23:00')
// output - Invalid Date
and also tried using moment JS to format the date like
moment("19-02-2021 - 23:00","DD-MM-YYYY - HH:mm").toDate();
// output - Fri Feb 19 2021 23:00:00 GMT+0530 (India Standard Time)
but now all the other dates will be parsed wrong like this one
moment("19-02-2021 - 23:00","DD-MMM-YYYY - HH:mm").toDate();
// output - Tue Jan 19 2021 23:00:00 GMT+0530 (India Standard Time)
Is there a correct and reliable way to do this?