我无法理解某些 JavaScript 在 Edge 中的行为似乎有所不同。 具体归结为:
var testi = new Date().toLocaleTimeString();
var len2 = testi.length;
alert(len2);
我在 Edge 中的长度是 17,在 Chrome 和 IE 中是 10 字符串中似乎有一些幻影空格,它也搞砸了我试图对其进行子字符串化的尝试。
https://jsfiddle.net/m1m8h7ym/
仅供引用,我的时区是美国中部。
最佳答案
看起来微软正在滑入 the invisible left-to-right mark .这也发生在 IE11 的边缘模式中。我通过遍历字符串中的每个字符并将其传递给 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>
您可以通过其 unicode 将其删除,从而将其删除,该 unicode 可以通过表达式 \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>
关于javascript - Microsoft Edge javascript toLocaleTimeString 不正确的间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32099053/