Get the Hours and Minutes from a Date in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 3, 2024
3 min

banner

# Table of Contents

  1. Get the Hours and Minutes from a Date
  2. Get the Hours and Minutes using toLocaleTimeString

# Get the Hours and Minutes from a Date in JavaScript

To get the hours and minutes from a date:

  1. Use the getHours() method to get a number between 0 and 23 that represents the hour for the given date.
  2. Use the getMinutes() method to get a number between 0 and 59 that represents the minutes in the given date.
index.js
// โœ… get hours and minutes from the current Date const now = new Date(); const hoursAndMinutes = now.getHours() + ':' + now.getMinutes(); console.log(hoursAndMinutes); // ๐Ÿ‘‰๏ธ 19:52

get hours and minutes from date

The getHours() method returns a number between 0 and 23 that represents the hour for the given date according to local time.

index.js
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.

index.js
// โœ… 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

pad the values with leading zero if necessary

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.

index.js
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.

The function returns the hours and minutes from the current date if a Date object isn't supplied.

We also padded the hours and minutes with a leading zero to always format them as 2 digits.

# Get the Hours and Minutes from a Date using toLocaleTimeString

You can use the toLocaleTimeString() method to get a locale-specific representation of the hours and minutes.

index.js
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

get hours and minutes from date using tolocaletimestring

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.

index.js
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.

This means that even if the hours and minutes are less than 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.

index.js
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.

index.js
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev