Last updated: Mar 6, 2024
Reading timeยท3 min
To use the toLocaleTimeString()
method without displaying seconds, set the
hour
and minute
parameters in the method's options
object.
const date = new Date(); // โ Using visitor's default locale console.log( date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', }), ); // ๐๏ธ 05:36 PM // โ Using US locale console.log( date.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', }), ); // ๐๏ธ 05:36 PM // ๐๏ธ 07/28/2023, 05:36 PM console.log( date.toLocaleTimeString([], { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', }), );
The toLocaleTimeString() method returns a string with language-sensitive representation displaying the time for the given date.
The toLocaleTimeString
method takes the following 2 parameters:
locales
- a string with a BCP 47 language tag or an array of such strings.
You can use any of the available locales, e.g. es-MX
for Mexico or en-CA
for Canada. If you need more information about this parameter, check out the
MDN docs.options
- an object where we can adjust the formatting for the time. Read
more about the options
object in the
MDN docs.locales
parameter to an empty array to use the visitor's default locale.const date = new Date(); // โ Using visitor's default locale console.log( date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', }), );
In the second example, we set the locale
to en-US
to display the time
according to the US locale, regardless of the visitor's preferences.
const date = new Date(); // โ Using US locale console.log( date.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', }), ); // ๐๏ธ 05:41 PM
We set the hour
and minute
properties on the options
object to 2-digit
,
so the time is always displayed using 2 digits for the hours and minutes, even
if their values are 1 digit only (less than 10
).
The other possible value for the hour
and minute
properties is numeric
(e.g. 3
), which would display the hours and minutes using a single digit if
their values are less than 10
.
seconds
are not displayed because we didn't set the seconds
property in the options
object.The seconds
property can also be set to numeric
or 2-digit
.
You can also display the date, by setting the year
, month
and day
properties in the options
object.
const date = new Date(); // ๐๏ธ 7/28/2023, 05:39 PM console.log( date.toLocaleTimeString([], { year: 'numeric', month: 'numeric', day: 'numeric', hour: '2-digit', minute: '2-digit', }), );
The month
and day
properties can also be set to 2-digit
to format them as
2 digits, even if the month or day values are less than 10
.
const date = new Date(); // ๐๏ธ 01/27/2022, 12:10 PM console.log( date.toLocaleTimeString([], { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', }), );
If you need to get the value in a different locale, change the first parameter
to the specific locale, e.g. en-US
or de-DE
.
You can learn more about the related topics by checking out the following tutorials: