javascripttimedatetime-formattime-format

.toLocaleTimeString() syntax not displaying AM/PM in safari browser


I wanted to change the format of a time to HH:mm AM/PM by using javascript. Then I used .toLocaleTimeString() syntax to format it. But it's not working as expected in safari browser when working correctly on google chrome. Can anyone suggest a solution?

I tried as bellow,

time = dateStr.toLocaleTimeString().replace(/(.*)\D\d+/, '$1');

expected output was => 11:15 AM but output in safari browser was => 11:15


Solution

  • I was founded a solution for this case,

    time = dateStr.toLocaleTimeString('en-US').replace(/(.*)\D\d+/, '$1');
    

    I added 'en-US' to the syntax. Now it's working correctly in both safari and chrome browsers.