I need to pass a date in the format 10/9/2014
to our API, but I cannot use toLocaleDateString
in IE because it turns it into ?10?/?9?/?2014
because IE loves to encode things differently. Is there another simple way of getting a date into this format that is clean across the API in all browsers?

- 7,744
- 16
- 55
- 113
-
Ok thank you.. I have seen similar SO problems like that and people just say you aren't supposed to use the `toLocale` functions for anything besides displaying things in the UI. But that seems stupid because there are plenty of times when I need to use this format in my API – Matt Hintzke Oct 09 '14 at 21:57
-
I would just write your own formatting function that extends the Date prototype. If it's not even working in IE 11, you can bet 99% of your IE users will see this bug for several years to come since IE isn't on a rigorous update schedule like other browsers. – user2867288 Oct 09 '14 at 22:04
-
according to the spec, toLocaleXXX() can be completely arbitrary according to the whims of the implementation, and JS doesn't even have to know how to parse the locale versions to comply with the spec... – dandavis Oct 09 '14 at 22:24
-
@user2867288: IE11 is evergreen, it won't be around nearly as along as say, IE8. – dandavis Oct 09 '14 at 22:24
1 Answers
I have came across this behavior as well. IE11 (fairly confident it only started happening in IE11) was putting in Unicode character \u200E (Left to right mark) when you use toLocaleDateString
. I would consider this a bug and undesirable behavior , and as such I submitted a ticket on Microsoft Connect (unsure if they consider it as the same). Current the ticket is still active. Please add a repro on it too hopefully Microsoft will get around to dealing with it and making it behave as we expect it to. Ticket can be found here
There is a temporary workaround of the following
datevar.toLocaleDateString().replace(/[\u200E]/g, "")
this should sanitize your LocaleDateString and get it to behave
There is a related SO question here, which indicates that the primary issue is that we are expected the string returned from toLocaleDateString
to be parsable back into a date. Which it is not intended to be. IMO I think it should be, and IE11 seems to be the odd one out here (Other browser don't do it)
-
classic Microsoft... I cannot comment on it because it says "Sign in to comment" even though I am signed in already!!! :SSSSSSS – Matt Hintzke Oct 09 '14 at 22:16