2

This is straight from the console in IE (Microsoft Edge 25.10):

greg = new Date().toLocaleTimeString().split(':')[0];
"12"
greg = new Date().toLocaleTimeString().split(':')[0]=="12";
false

why?

rikkitikkitumbo
  • 954
  • 3
  • 17
  • 38

2 Answers2

4

It's because there are two invisible characters in the string: U-200E (left-to-right marks) at the beginning and end:

new Date().toLocaleTimeString().split(':')[0] == "\u200e12\u200e" // true

toLocaleTimeString is defined here by ECMA-402 if the browser supports ECMA-402; otherwise, it's completely up to the implementation. IE doesn't support ECMA-402 and thus it can put anything in that string it likes. Including left-to-right marks seems a bizarre thing to do, but...

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

Strangely, there appear to be some invisible characters surrounding the numbers in that time string:

enter image description here

This can be circumvented / solved by parsing the split part as integer:

var part = new Date(2016, 00, 01, 10, 55, 30) // Fri Jan 01 2016 10:55:30
    .toLocaleTimeString().split(':')[0];

console.log(part);
console.log(part == "10");
console.log(parseInt(part) == 10);
Cerbrus
  • 70,800
  • 18
  • 132
  • 147