Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE's toLocaleString has strange characters in results

I've run into a super strange thing that apparently is IE-specific in toLocaleString on dates.

In the IE console window:

new Date("2014-08-28T20:51:09.9190106Z").toLocaleString(); "‎8‎/‎28‎/‎2014‎ ‎1‎:‎51‎:‎09‎ ‎PM" 

Now, type out that string manually as a string and compare it to what the method returned:

"8/28/2014 1:51:09 PM" === new Date("2014-08-28T20:51:09.9190106Z").toLocaleString(); false 

Does anyone have any idea why this is occurring in IE? This doesn't occur in Chrome.

Update: more examples:

new Date("8/28/2014 1:51:09 PM") [date] Thu Aug 28 2014 13:51:09 GMT-0700 (Pacific Daylight Time)[date] Thu Aug 28 2014 13:51:09 GMT-0700 (Pacific Daylight Time)  new Date(new Date("2014-08-28T20:51:09.9190106Z").toLocaleString()) [date] Invalid Date[date] Invalid Date 
like image 675
John Gardner Avatar asked Aug 29 '14 19:08

John Gardner


People also ask

What does the toLocaleString () method do in JS?

The toLocaleString() method returns a string with a language-sensitive representation of this date. In implementations with Intl. DateTimeFormat API support, this method simply calls Intl. DateTimeFormat .

How does toLocaleString determine locale?

The toLocaleString method relies on the underlying operating system in formatting dates. It converts the date to a string using the formatting convention of the operating system where the script is running.


2 Answers

First, a bit of background: IE11 implemented the ECMA-402 ECMAScript Internationalization API that redefined Date.prototype.toLocaleString (as well as toLocaleDateString and toLocaleTimeString) as calls to format on Intl.DateTimeFormat. As such, d.toLocaleString() is equivalent to

Intl.DateTimeFormat(undefined, {   year: 'numeric',   month: 'numeric',   day: 'numeric',   hour: 'numeric',   minute: 'numeric',   second: 'numeric' }).format(d) 

You might think that this is pretty explicit but browsers are allowed a large amount of leeway with what formats they support and what characters compose the format. This is by design - with all the locales and languages around the planet, specifying this would be quite burdensome and very difficult to keep up-to-date. For this reason you cannot expect to be able to compare the results of toLocaleString across browsers or even expect the same browser to continue giving the same result from release to release. As the underlying locale data changes (perhaps because local custom has changed, or more data is available, or better formats are added), so too will the format that is returned from this API.

The takeaway from this is that you should try not to rely on comparing the output of the toLocaleString APIs with some static value in your application. Further, given a date d, Date.parse(d.toLocaleString()) may work sometimes but not others depending on locale, so it's best to avoid this as well.

With that said, en-US is relatively stable and for the most part browsers do (for now) agree on what that basic format is. However, IE inserts bidirectional control characters around the date. This is by design so the output text will flow properly when concatenated with other text. This is especially important when mixing LTR and RTL content such as concatenating a formatted RTL date with LTR text.

like image 173
Brian Terlson Avatar answered Oct 02 '22 13:10

Brian Terlson


Use

Str.replace(/[^ -~]/g,'') 

This will remove unwanted special characters.

like image 32
Sudip Barman Avatar answered Oct 02 '22 15:10

Sudip Barman