IE 11 error with JavaScript toLocaleDateString () formatting

I am using JavaScript Date toLocaleDateString () function to format my date to look like 8/13/2014

, but for some reason, when I try to send this value through an API call by doing JSON.stringify

and then AJAXing the value, IE decides to change the actual value ?8?/?30?/?2014

.. This. obviously causing errors on the back.

Why is IE doing this and how to fix it?

+3


source to share


2 answers


It looks like this is a bug that was introduced in IE 11. IE 11 uses Unicode characters, so you see U + 200E 'LEFT-TO-RIGHT MARK'

What you can do as a workaround to fix this problem is to replace this char. Like this:



console.log((new Date()).toLocaleDateString().replace(/\u200E/g, ''));

      

+3


source


You should check the answer here: Changes to ToLocaleDateString () in IE11

You shouldn't use a function designed to format anything for a custom display locale specific and expect the output to be handled by the machine. Any of the results from toLocaleString, toLocaleDateString, or toLocaleTimeString are human-readable only. (As Bergie explained in the comments, toString is also for displaying people, but ECMA ยง15.9.4.2 says it should be both ways)



Although the function returns a string, it is human readable only and never machine parseable. I'm not 100% sure what the encoding is for IE, but although it looks like a string, a different encoding is used below it.

For date formatting you can use Moment.js or just write your own formatting function.

0


source







All Articles