Javascript date get hours

JavaScript Date getHours()

ES1 (JavaScript 1997) is fully supported in all browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes Yes

More Examples

Add zeros and colons to display the time:

const d = new Date();
let h = addZero(d.getHours());
let m = addZero(d.getMinutes());
let s = addZero(d.getSeconds());
let time = h + «:» + m + «:» + s;

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Javascript date get hours

Last updated: Jan 4, 2023
Reading time · 3 min

banner

# Table of Contents

# 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.
Copied!
// ✅ get hours and minutes from the current Date const now = new Date(); const hoursAndMinutes = now.getHours() + ':' + now.getMinutes(); console.log(hoursAndMinutes); // 👉️ 13:28

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

Copied!
const now = new Date(); console.log(now.getHours()); // 👉️ 13 console.log(now.getMinutes()); // 👉️ 28

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.

Copied!
// ✅ 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.

Copied!
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.

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.

Copied!
const now = new Date(); // 👇️ With PM / AM const hoursAndMinutes = now.toLocaleTimeString('en-US', hour: '2-digit', minute: '2-digit', >); console.log(hoursAndMinutes); // 👉️ 01:34 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.

Copied!
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.

Copied!
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 .

Copied!
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.

Источник

How To Get the Hours And Minutes From A Date In JavaScript

Get the Hours and Minutes from a Date in JavaScript

To get the hours and minutes from a date in JavaScript, we have two methods: using the getHours() with getMinutes() method and using the toTimeString() method with replace() method. Check out the details below to get more information.

To get the hours and minutes from a date in JavaScript

Using the getHours() and getMinutes() methods

The best simple way is to use getHours() to get hours from the date string and getMinutes() to get minutes in date. Two methods are already built in javascript, and you can easily call it out to use.

getHours() method

Syntax: Date.getHours()

  • Parameters: NONE
  • Return value: return value is the local time hour (0 – 23).

getMinutes() method

Syntax: Date.getMinutes()

  • Parameters: NONE
  • Return value: return value is the minutes of the date (0 to 59).

Code example:

In this example, we will create a new date string, then call the function getHours() to get hours of the date string, and similar call the getMinutes() to get minutes of the date string.

// Using getHours() and getMinutes methods to get Minutes and Hours of date var Hours = new Date("2022/10/20 10:30:00").getHours() var Minutes = new Date("2022/10/20 10:30:00").getMinutes() console.log("Hour is: ", Hours) console.log("and Minutes is: ", Minutes)
Hour is: 10 and Minutes is: 30

Using the toTimeString() method and replace() method

In this way, we will use two methods to get the hours and minutes of the date, the toTimeString() method returns the time portion of a date object as a string and the replace() method will return a new string where the specified value has been replaced.

toTimeString() method

Syntax: Date.toTimeString()

  • Parameter: NONE
  • Return value: return value is a string, representing the time as a string.

replace() method

Syntax: string.replace(searchValue, newValue)

  • searchValue: The value, or regular expression, to search for.
  • newValue: The new value (to replace with).

Return value: A new string where the specified value(s) has been replaced.

In this example, we will create a currentTime variable and call toTimeString() function to get only the time of the date string. After that, we use regex to get hours and minutes of current time by using replace method with a regular expression.

// get only the time of day by using toTimeString() method currentTime = new Date("2022/10/20 10:30:00").toTimeString() console.log(currentTime) // use regex to get Hours and Minutes with replace() method getHoursAndMinutes = currentTime.replace(/(\d):(\d):.*/, "Hours is: $1 \nand Minutes is: $2"); console.log(getHoursAndMinutes)
10:30:00 GMT+0700 Hours is: 10 and Minutes is: 30

Summary

In this tutorial, we have explained how to get the hours and minutes from a date in JavaScript in 2 ways. We always hope this tutorial is helpful to you. Leave your comment here if you have any questions or comments to improve the article. Thanks for reading!

Maybe you are interested:

My name is Fred Hall. My hobby is studying programming languages, which I would like to share with you. Please do not hesitate to contact me if you are having problems learning the computer languages Java, JavaScript, C, C#, Perl, or Python. I will respond to all of your inquiries.

Name of the university: HUSC
Major: IT
Programming Languages: Java, JavaScript, C , C#, Perl, Python

Источник

Читайте также:  :nth-child()
Оцените статью