1

It is the first time when I met some of the bad example of data format. All date goes for me regarding to a region of a customer. I cannot change it. I got string:

var str = "23.01.2019 23:05:58";

in order to get

var dt = new Date(str);

I need to execute at least

str.replace(/./g,'/');

But got a weird result. (tried "split" and "join") also nothing. Thanks a lot for any help

1 Answers1

1

The . in your regular expression means any character.

Try this instead...

str.replace(/\./g,'/');

By using \. you're "escaping" the dot and saying "this has to be a dot character"

freefaller
  • 19,368
  • 7
  • 57
  • 87
  • thanks, you are right! It works good – Blade Runner Jan 23 '19 at 16:11
  • You're welcome @BladeRunner - good luck with the rest of your project – freefaller Jan 23 '19 at 16:12
  • While this answers the issue of replacing "." with "/", it doesn't answer the bigger question of parsing the string to a Date. The OP format and that generated by this answer are not compatible with ECMA-262, so parsing is implementation dependent. If this answer "works" with *Date.parse*, it's just luck (e.g. Safari returns "Invalid Date" for both formats). – RobG Jan 23 '19 at 21:20