JavaScript Date toLocaleTimeString()
Syntax & Examples
Date.toLocaleTimeString() method
The toLocaleTimeString() method returns a string with a locality-sensitive representation of the time portion of this date, based on system settings.
Syntax of Date.toLocaleTimeString()
There are 3 variations for the syntax of Date.toLocaleTimeString() method. They are:
toLocaleTimeString()
This method returns a string representing the time portion of this date according to the default locale and options.
Returns value of type String
.
toLocaleTimeString(locales)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
locales | optional | A string with a BCP 47 language tag, or an array of such strings, to specify the locale or locales. |
This method returns a string representing the time portion of this date according to the specified locale.
Returns value of type String
.
toLocaleTimeString(locales, options)
Parameters
Parameter | Optional/Required | Description |
---|---|---|
locales | optional | A string with a BCP 47 language tag, or an array of such strings, to specify the locale or locales. |
options | optional | An object with configuration options for time formatting, such as hour, minute, second, timeZoneName, etc. |
This method returns a string representing the time portion of this date according to the specified locale and formatting options.
Returns value of type String
.
✐ Examples
1 Using toLocaleTimeString() method with no arguments
In JavaScript, we can use the toLocaleTimeString() method to get the time portion of a date according to the default locale.
For example,
- Create a new Date object with the current date and time.
- Use the toLocaleTimeString() method with no arguments.
- Log the resulting string to the console using console.log().
JavaScript Program
const date = new Date();
const localeTimeString = date.toLocaleTimeString();
console.log(localeTimeString);
Output
Expected output: A string representing the time portion of the current date in the default locale format.
2 Using toLocaleTimeString() method with a locale argument
In JavaScript, we can use the toLocaleTimeString() method to get the time portion of a date in a specified locale.
For example,
- Create a new Date object with the current date and time.
- Use the toLocaleTimeString() method with the locale argument 'fr-FR' (French - France).
- Log the resulting string to the console using console.log().
JavaScript Program
const date = new Date();
const localeTimeString = date.toLocaleTimeString('fr-FR');
console.log(localeTimeString);
Output
Expected output: A string representing the time portion of the current date in 'fr-FR' locale format (e.g., '15:45:30').
3 Using toLocaleTimeString() method with locale and options arguments
In JavaScript, we can use the toLocaleTimeString() method to get the time portion of a date in a specified locale with specific formatting options.
For example,
- Create a new Date object with the current date and time.
- Use the toLocaleTimeString() method with the locale argument 'en-GB' (English - United Kingdom) and options for hour, minute, second, and timeZoneName.
- Log the resulting string to the console using console.log().
JavaScript Program
const date = new Date();
const options = { hour: '2-digit', minute: '2-digit', second: '2-digit', timeZoneName: 'short' };
const localeTimeString = date.toLocaleTimeString('en-GB', options);
console.log(localeTimeString);
Output
Expected output: A string representing the time portion of the current date in 'en-GB' locale format with specified options (e.g., '15:45:30 GMT+1').
Summary
In this JavaScript tutorial, we learned about toLocaleTimeString() method of Date: the syntax and few working examples with output and detailed explanation for each example.