Javascript Date toLocaleTimeString()

Introduction

Javascript Date toLocaleTimeString() displays the date's hours, minutes, and seconds in an implementation-specific format.

The output of this method varies from browser to browser.

dateObj.toLocaleTimeString([locales[, options]])

toLocaleTimeString() without arguments depends on the implementation, the default locale, and the default time zone.

let date = new Date(Date.UTC(2020, 11, 12, 3, 0, 0));
console.log(date.toLocaleTimeString());

Using locales

This example shows some of the variations in localized time formats.

var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));
// formats below assume the local time zone of the locale;
// America/Los_Angeles for the US
// US English uses 12-hour time with AM/PM
console.log(date.toLocaleTimeString('en-US'));
// British English uses 24-hour time without AM/PM
console.log(date.toLocaleTimeString('en-GB'));
// Korean uses 12-hour time with AM/PM
console.log(date.toLocaleTimeString('ko-KR'));
// Arabic in most Arabic speaking countries uses real Arabic digits
console.log(date.toLocaleTimeString('ar-EG'));
// when requesting a language that may not be supported, such as

Using options

The results provided by toLocaleTimeString() can be customized using the options argument:

var date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0));

// use UTC// ww  w  .  ja  va 2  s  .  c o m
var options = { timeZone: 'UTC', timeZoneName: 'short' };
console.log(date.toLocaleTimeString('en-US', options));
// US with 24-hour time
console.log(date.toLocaleTimeString('en-US', { hour12: false }));
// show only hours and minutes, use options with the default locale - use an empty array
console.log(date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }));



PreviousNext

Related