I'm trying to convert a string to a date. The string is in this format: 05-FEB-2019 19:20
But when I do the conversion it gives me RangeError: InvalidDate
var local_timestamp_str = "05-FEB-2019 19:20"
var local_timestamp = new Date(local_timestamp_str);
How can I convert such strings to date type?
UPDATE:
I'm trying to handle the above specific case as follows, where the try block converts string of "05/02/2019 20:04" format to date. That works fine. But for cases like "05-FEB-2019 19:20", it goes into the catch block giving RangeError. I want to handle this error and convert this specific format as var local_timestamp_str = local_timestamp_str.split('-').join(' ');
and continue running the code. But it's not working and still just throws the error.
try {
var local_timestamp = new Date(local_timestamp_str);
} catch (err) {
if (err.name == 'RangeError') {
var local_timestamp_str = local_timestamp_str.split('-').join(' ');
var local_timestamp = new Date(local_timestamp_str);
console.log(local_timestamp);
} else {
throw err;
}
}