Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft Edge javascript toLocaleTimeString incorrect spacing

I'm having trouble understanding what is going on with some JavaScript that seems to behave differently in Edge. Specifically boiled it down to:

var testi = new Date().toLocaleTimeString();
var len2 = testi.length;
alert(len2);

My length in Edge is 17, in Chrome and IE it's 10 There seems to be some phantom spaces in the string, it also screwed up my attempting to substring it.

https://jsfiddle.net/m1m8h7ym/

FYI my Time Zone is US Central.

like image 955
Aaron Avatar asked Aug 19 '15 14:08

Aaron


1 Answers

Looks like Microsoft is slipping in the invisible left-to-right mark. This also occurs in IE11 in Edge mode. I figured this out by looping through each character in the string and passing it to encodeURIComponent()

var output = document.getElementById("output");
var testi = new Date().toLocaleTimeString();
var row;
for (var i = 0, len = testi.length; i < len; i++) {
    row = document.createElement("pre");
    row.innerHTML = encodeURIComponent(testi[i]);
    output.appendChild(row);
}
<div id="output"></div>

You could strip it out by removing it by its unicode, which can be captured in regex by the expression \u200E.

var output = document.getElementById("output");
var testi = new Date().toLocaleTimeString().replace(/\u200E/g,"");
var row;
for (var i = 0, len = testi.length; i < len; i++) {
    row = document.createElement("pre");
    row.innerHTML = encodeURIComponent(testi[i]);
    output.appendChild(row);
}
<div id="output"></div>
like image 112
Thriggle Avatar answered Sep 25 '22 05:09

Thriggle