2

When I try to use new Date("Dec/30/2016") it returns Fri Dec 30 2016 00:00:00 GMT+0800 (PHT) but when I do this on IE11, it instead returns Fri Dec 30 -2016 00:00:00 GMT+0800 (PHT)

Is there a way to patch this up like a polyfill/shiv to make it work as expected?

https://jsfiddle.net/hx038a03/5/ enter image description here

reikyoushin
  • 1,993
  • 2
  • 24
  • 40
  • momentjs is probably your best bet. – Matt Jun 22 '17 at 00:05
  • As you can see, the problem is on the Date object, even native javascript would fail on this scenario unless I can find a way to patch things up. – reikyoushin Jun 22 '17 at 00:08
  • 1
    Don't parse date strings with the built-in parser, always use a bespoke function or library with a **decent** parser. – RobG Jun 22 '17 at 00:36
  • But even using momentjs returns the wrong answer.. why was this a duplicate? What I was looking for was a shiv.. – reikyoushin Jun 22 '17 at 00:58
  • 3
    hrmmm... @RobG I have to object to the duplicate. In this case, the input *stays the same* whereas the duplicate target is clearly about *different inputs representing the same date*. Also please read the above comment by reikyoushin. – Félix Adriyel Gagnon-Grenier Jun 22 '17 at 00:58
  • 1
    @FélixGagnon-Grenier—I disagree. It's all about the OP depending on the built-in parser when that is a very bad idea. The [*duplicate I marked*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) explains why it's a bad idea, why different browsers may return different results (which is exactly the OP's problem) and how to address it. The bottom line is the OP should not use the built-in parser for this. – RobG Jun 22 '17 at 05:50
  • @reikyoushin—moment.js will correctly parse that format **if** you tell the parser the format to parse: `moment('Dec/30/2016','MMM/DD/YYYY')`. Perhaps you had the [*parse tokens*](https://momentjs.com/docs/#/parsing/string-format/) wrong (moment uses all upper case for the date parts and lower for time, except for 24hr which is HH). If that's your solution, include a moment.js tag, then post it as an answer and accept it. – RobG Jun 22 '17 at 06:01
  • @RobG You can see here https://jsfiddle.net/hx038a03/5/ that it does not return the expected Date when passing the format.. Try opening that one up in IE11 – reikyoushin Jun 22 '17 at 07:31
  • @reikyoushin—I tried it in IE 11 with moment.js and it worked fine, but I used the correct parse tokens. There are other parsers, try [*fecha.js*](https://github.com/taylorhakes/fecha). – RobG Jun 22 '17 at 08:05
  • @RobG hmm. yeah, that makes sense. If this question stays without answer, your initial decision will indeed become very sensible. Thanks for the explanation. – Félix Adriyel Gagnon-Grenier Jun 22 '17 at 15:56
  • @RobG When you said it worked fine, have you checked that one in IE11? Cause in mine it returns -2016 – reikyoushin Jun 23 '17 at 05:54
  • @r— "*I tried it in IE 11 with moment.js*" means I tried `moment('Dec/30/2016','MMM/DD/YYYY')` in IE 11. ;-) – RobG Jun 23 '17 at 08:18
  • @RobG but in mine it shows `Fri Dec 01 2017 00:00:00 GMT-0800 (Pacific Standard Time)` See https://jsfiddle.net/hx038a03/5/ – reikyoushin Jun 28 '17 at 00:42
  • @reikyoushin— **you're still using the wrong [*parse tokens*](https://momentjs.com/docs/#/parsing/string-format/)**, use MM/DD/YYYY (note the upper case). If you do not supply the parse format, moment.js will first try ISO 8601, then RFC 2882, then fall back to just `new Date(string)` which is why you see the same result if you don't give it any parse format. [*Read the documentation*](https://momentjs.com/docs/#/parsing/string/). If you give it erroneous parse tokens, it will give you erroneous results: GI/GO. – RobG Jun 28 '17 at 01:37
  • @RobG But my string has "Dec" and not "12" thus the "MMM" and not the "MM" – reikyoushin Jul 03 '17 at 02:15
  • 1
    But you're using "dd/yyyy" instead of "DD/YYYY". – RobG Jul 03 '17 at 04:08
  • Ahh yes, its the case of the letters that im missing. Thanks! – reikyoushin Jun 01 '20 at 05:47

0 Answers0