Chrome not giving correct toLocaleTimeString() result

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Mygind
    Contributor
    • Mar 2008
    • 571

    Chrome not giving correct toLocaleTimeString() result

    I am doing some work with displaying the local time.

    I would like to get this format:
    2:18:00 pm

    Using the sample code from w3Schools.com below, I can get the correct results from IE and FireFox. But when it comes to Chrome, I get the 24hr clock version where it is simply displayed this way:
    14:18:00

    http://xn--www-5da33as14n.w3sc hools.com/jsref/jsref_tolocalet imestring.asp


    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
      <title> Getting Local Time </title>
     </head>
    
     <body>
    	<script type="text/javascript">
    
    		var d=new Date();
    		document.write("Original form: ");
    		document.write(d + "<br />");
    		document.write("Formatted form: ");
    		document.write(d.toLocaleTimeString());
    
    	</script>  
     </body>
    </html>
    What setting is there in Chrome or what code do I need to write to get the desired format from Chrome?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    I think it may be a bug: see http://code.google.com/p/chromium/issues/detail?id=3607

    You can parse the string to convert from 24hr to 12hr. Split on : and if the first number (14) is greater than 12, then subtract 12 and then add a "pm" at the end.

    Comment

    Working...