Search code examples
javascriptinternet-explorertimeparseint

ParseInt of toLocaleTimeString() in Internet Explorer


I have problem with parse string after toLocaleTimeString()

var b = new Date();
var c = b.toLocaleTimeString();
var d = c.split(":");
var e = parseInt(d[0]);

After this: Internet Explorer: e is NaN, d is "15" Chrome: e is 15, d is "15"


Solution

  • In IE you need to use charAt() instead of d[0] after converting to string.

    var b = new Date();
    var c = b.toLocaleTimeString();
    var d = c.split(":");
    
    // For IE Support...
    var D = d.toString();
    var e = parseInt(D.charAt(0));
    if ( isNaN(e) === true ){
        var e = parseInt(D.charAt(1));
    }
    
    alert(d);
    alert(e);