Last updated: Mar 3, 2024
Reading timeยท3 min
To get the hours and minutes from a date:
getHours()
method to get a number between 0
and 23
that
represents the hour for the given date.getMinutes()
method to get a number between 0
and 59
that
represents the minutes in the given date.// โ get hours and minutes from the current Date const now = new Date(); const hoursAndMinutes = now.getHours() + ':' + now.getMinutes(); console.log(hoursAndMinutes); // ๐๏ธ 19:52
The
getHours()
method returns a number between 0
and 23
that represents the hour for the
given date according to local time.
const now = new Date(); console.log(now.getHours()); // ๐๏ธ 19 console.log(now.getMinutes()); // ๐๏ธ 53
The Date.getMinutes() method returns a
number between 0
and 59
that represents the minutes in the given date.
The output from the functions will return a single digit if the hours and
minutes are less than 10
.
You can pad the values with a leading zero if necessary.
// โ Get hours and minutes from a specified Date function padTo2Digits(num) { return String(num).padStart(2, '0'); } const date = new Date('December 14, 2026 08:09:00'); const hoursAndMinutes = padTo2Digits(date.getHours()) + ':' + padTo2Digits(date.getMinutes()); console.log(hoursAndMinutes); // ๐๏ธ 08:09
We used the String.padStart method to add a leading zero to the hours and minutes if necessary.
If you have to get the hours and minutes from a date often, define a reusable function.
function getHoursAndMinutes(date = new Date()) { return ( padTo2Digits(date.getHours()) + ':' + padTo2Digits(date.getMinutes()) ); } function padTo2Digits(num) { return String(num).padStart(2, '0'); } // โ Get hours and minutes from the current date const current = getHoursAndMinutes(); console.log(current); // ๐๏ธ 13:41 // โ Get hours and minutes from the specified date const other = getHoursAndMinutes( new Date('2023-04-27T08:30:10.820'), ); console.log(other); // ๐๏ธ 08:30
The getHoursAndMinutes
function takes a Date
object and returns the hours
and minutes from the date.
Date
object isn't supplied.We also padded the hours and minutes with a leading zero to always format them as 2 digits.
You can use the toLocaleTimeString() method to get a locale-specific representation of the hours and minutes.
const now = new Date(); // ๐๏ธ With PM / AM const hoursAndMinutes = now.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', }); console.log(hoursAndMinutes); // ๐๏ธ 07:55 PM
We passed en-US
for the locale and got the hours and minutes formatted as
HH:MM PM/AM
.
You can also set the locale to default
to use the user's browser
configuration.
const now = new Date(); const hoursAndMinutes = now.toLocaleTimeString('default', { hour: '2-digit', minute: '2-digit', }); console.log(hoursAndMinutes); // ๐๏ธ 01:35 PM
The second argument we passed to the method is an options object. We set both
the hour
and minute
properties to 2-digit
.
10
, a leading 0
automatically gets added to make the output consistent.Similarly, you could use a different locale to get a different output format.
const now = new Date(); const enUS = now.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit', }); console.log(enUS); // ๐๏ธ 01:47 PM const enGB = now.toLocaleTimeString('en-GB', { hour: '2-digit', minute: '2-digit', }); console.log(enGB); // ๐๏ธ 13:47 const deDE = now.toLocaleTimeString('de-DE', { hour: '2-digit', minute: '2-digit', }); console.log(deDE); // ๐๏ธ 13:47
The output of the toLocaleTimeString
method is always going to display 2
digits for the hours and minutes, so we don't have to add a leading zero if
their values are less than 10
.
The other possible value for the hour
and minute
properties is numeric
.
The numeric
value doesn't add a leading zero if the hours and minutes are less
than 10
.
const now = new Date(); const hoursAndMinutes = now.toLocaleTimeString('default', { hour: 'numeric', minute: 'numeric', }); console.log(hoursAndMinutes); // ๐๏ธ 1:37 PM
We set the hour
and minute
properties to numeric
, so they didn't get
padded with a leading zero.
You can learn more about the related topics by checking out the following tutorials: