1

Could someone please explain why IE has a bug when trying to use String functions on a Date function that uses the "Locale"? I think it has something to do with encoding of the characters.

Check this out jsFiddle in both IE and Chrome and you will see that in Chrome we get 4 (the correct index) and in IE we get 8. Does this have to do with ascii vs. unicode? If so, should this be a bug in IE?

var date = new Date();

var str = date.toLocaleTimeString();
jQuery('#a').text(str);

jQuery('#b').text(str.lastIndexOf(":"));

str = date.toTimeString();
jQuery('#c').text(str);
jQuery('#d').text(str.lastIndexOf(":"));

Screen shot of IE 11 jsFiddle output

Screen shot of IE 11 jsFiddle output

Matt Hintzke
  • 7,744
  • 16
  • 55
  • 113

2 Answers2

1

Here's a modified fiddle. For whatever reason, the IE 11 string has a bunch of extra Unicode "left-to-right mark" characters embedded in it.

The code I added was

var s = '';
for (var i = 0; i < str.length; ++i)
    s  += str.charCodeAt(i) + ' ';
$('#c').text(s);

and an accompanying <h4> like the others.

Also see this other Stackoverflow question on the topic.

Community
  • 1
  • 1
Pointy
  • 405,095
  • 59
  • 585
  • 614
  • So would this technically be a bug in IE 11? It just seems very annoying to have to do this encoding conversion to all these strings just because IE is incompetent. – Matt Hintzke Aug 14 '14 at 16:32
  • @MattHintzke well it's certainly weird, but I'm not sure you could call it a "bug". There are no firm rules on what the return value from that API should be, and there's nothing technically wrong with including those characters. (It seems dumb though.) – Pointy Aug 14 '14 at 16:33
  • @MattHintzke note that if you need access to the individual "pieces" of the timestamp, the Date API provides that. – Pointy Aug 14 '14 at 16:34
1

By definition Date.toLocaleTimeString() produces output according to user's preferences/defaults.

So the result (position of : ) can vary on different platforms, locales and even time of the day.

This is how Korean time looks like for example:

"오후 12:00:00"

Apparently in your case Chrome and IE have different opinion on default locale formatting.

If you have any code that relies on position of : in toLocaleTimeString() it must be refactored to something more reliable.

In some locales/settings toLocaleTimeString() may not contain : at all.

c-smile
  • 26,734
  • 7
  • 59
  • 86