0

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;
  }
}
RobG
  • 142,382
  • 31
  • 172
  • 209
user10096621
  • 225
  • 2
  • 4
  • 16
  • This question is very similar to [this one](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) – ren.rocks Feb 05 '19 at 18:35
  • Parsing of non–standard timestamps with the built–in parser is problematic and not recommended. Either write your own function (parsing DD MMM YYYY HH:mm should be no more than 4 lines of code) or use a library. – RobG Feb 05 '19 at 20:21
  • Most built–in parsers (all?) will parse "05/02/2019 20:04" as 2 May or an invalid date, it's not equivalent to "05-FEB-2019 19:20". – RobG Feb 05 '19 at 22:53

2 Answers2

1

Looks like the dashes may be causing your problem.

try something like this new Date('05 February 2019 19:20 UTC');

This Mozilla page shows some valid cases. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_date

  • I can't change the string format. This is what I'm reading from the database. – user10096621 Feb 05 '19 at 18:38
  • You'll have to alter the string after you get it from the DB. Check out the link from @rtrigoso. [link](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) The answer there should help you. – fourFerrets Feb 05 '19 at 18:45
0

i have update the input format.

var local_timestamp_str = "05-FEB-2019 19:20".split('-').join(' ');
var local_timestamp = new Date(local_timestamp_str);
console.log(local_timestamp.toString());
RobG
  • 142,382
  • 31
  • 172
  • 209
Saurabh Yadav
  • 3,303
  • 1
  • 10
  • 20
  • Can there be a solution to handle conversion of two types of strings together: "02/05/2019 10:43" and "05-FEB-2019 19:20" – user10096621 Feb 05 '19 at 18:39
  • @user10096621—some libraries will allow that. If you want to avoid a library, write 2 simple parse functions and determine which to use based on a test, say using a regular expression. – RobG Feb 06 '19 at 00:37