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)

Output

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)

Parameters: 

  • 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.

Example

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{2}):(\d{2}):.*/, 
                                        "Hours is: $1 \nand Minutes is: $2");
console.log(getHoursAndMinutes)

Output

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:

Leave a Reply

Your email address will not be published. Required fields are marked *