2

Got a problem here that is driving me mad. 2 variables being instantiated as dates, one with a string and one with a hard coded value. If you look at the debug window the value of mString is exactly the same as the hard coded value yet it produces an invalid date. No idea why but I sure would appreciate any pointers. I'm using IE 11. It works fine in all other browsers

mString is created like this: -

mString = dateParts[2] + "-" + mon.toString() + "-" + dateParts[1]

The errors in the console are not related to this

Debug Window

Shazoo
  • 685
  • 12
  • 25
  • Can you show the value of mString? – Matt Spinks Feb 07 '17 at 15:09
  • It's in the debug window in the attached image – Shazoo Feb 07 '17 at 15:10
  • what in the world is the value of `mString`? – Ahmed Masud Feb 07 '17 at 15:12
  • 1
    @Shazoo It is in the debug window, but perhaps the variable is updated elsewhere and isn't in fact "2017-02-07" at the time the `Date` object is instantiated? – Joseph Marikle Feb 07 '17 at 15:12
  • Well, this is not possible. The answer is, value of `mString` is not "2017-02-07" then, maybe it gets changed by something? – dfsq Feb 07 '17 at 15:12
  • @Shazoo you have errors on the console tab, what are they? – Ben Temple-Heald Feb 07 '17 at 15:13
  • This should work.You have 7 console errors, are you sure they don't affect on this part ? – Belmin Bedak Feb 07 '17 at 15:13
  • Post your code if possible otherwise there is no way to guess. – dfsq Feb 07 '17 at 15:14
  • That's very odd. I can't replicate on my IE 11. I created a fiddle: https://jsfiddle.net/mspinks/kkqxt8L9/. Can you try that and see if you get the same result as before with your browser? – Matt Spinks Feb 07 '17 at 15:14
  • How is `mString` set? That part of the code is not included in the screencap. May we see that part of the code? – Joseph Marikle Feb 07 '17 at 15:15
  • Possibly (probably?) a duplicate of http://stackoverflow.com/questions/21413757/tolocaledatestring-changes-in-ie11 – Peter B Feb 07 '17 at 15:16
  • mString is created by concatenating datepart strings together: mString = dateParts[2] + "-" + mon.toString() + "-" + dateParts[1] – Shazoo Feb 07 '17 at 15:21
  • You need to share more code. Is `date1` modified? Is `mString` modified? etc. Sometimes explaining the problem leads to the solution – GôTô Feb 07 '17 at 15:25
  • @Shazoo That still kind of just pushes the problem further back. What does `dateParts` contain? How is it set? It would be hugely helpful if you could set up a minimal demo. There's plenty of tools out there to help you do that. Here's a simple demo that kind of tries to replicate the problem, but it works in IE 11: http://jsbin.com/bomihikeqi/edit?html,output – Joseph Marikle Feb 07 '17 at 15:27
  • @JosephMarikle To be honest the way it's built up is a bit convoluted which is why I didn't post it. But surely a string is a string no matter how it's constructed. They're both the same value in the code above so I thought they should behave the same – Shazoo Feb 07 '17 at 15:38
  • @Shazoo Us asking for more code is less about doubting the nature of how the string is being handled and more about wondering if the string has been changed along the way. Try a `console.log(mString)` immediately before the `new Date()`, if that isn't how it works now. If that spits out the same `2017-02-07` value, there's something deeper that might even be a bug with IE at play. It's unlikely but possible. – Joseph Marikle Feb 07 '17 at 15:43
  • Still we don't know how/when variables are modified or where is your breakpoint – GôTô Feb 07 '17 at 15:43
  • Don't post images, post code. See [*How to create a minimal, complete and verifiable example*](http://stackoverflow.com/help/mcve). If you can't post code that replicates the issue, or even describe it accurately, others can't help. – RobG Feb 07 '17 at 22:47
  • BTW, best to ask permission before you use [*someone else's image*](http://www.e-cultura.sapo.pt/artigo/21452) as your avatar, unless you really are that person. – RobG Feb 08 '17 at 00:54

2 Answers2

0

That appears to work, I have tested the below in both IE11 and Chrome, do both alerts work for you?

var mString = "2017-02-07";
var d1 = new Date(mString);
var d2 = new Date("2017-02-07")

alert(d1);
alert(d2);
Ben Temple-Heald
  • 708
  • 1
  • 6
  • 16
  • That isn't what the OP is doing. Using the Date constructor to parse strings isn't recommended. ISO 8601 format date only strings are treated as UTC, so for hosts set to a time zone west of Greenwich the above will create a date for 6 February, not 7. – RobG Feb 07 '17 at 22:49
0

Use this:

var pattern = /(\d{4})-(\d{2})-(\d{2})/;
var date = new Date(mString.replace(pattern,'$1-$2-$3'));
Besat
  • 1,428
  • 13
  • 27
  • lol. Nice idea. I feel like there's some other, simple reason why the stated behaviour is happening though. This might fix OP's problem; but until he/she responds to the comments above, it's too hard to tell; but nice thinking outside the box. However, shouldn't that third grouping be a `\d{2}`? – Joseph Marikle Feb 07 '17 at 15:23
  • Sorry, it still say invalid date – Shazoo Feb 07 '17 at 15:24
  • Yes.. the third group should be 2! :) tnx – Besat Feb 07 '17 at 15:26
  • No, don't do that. Using the Date constructor to parse strings isn't recommended and most will deliver incorrect results for the pattern suggested (if they parse it at all). – RobG Feb 07 '17 at 22:50