Solution
To get hours and minutes from date in React, you can use the getHours()
and getMinutes()
methods it will return hours and minutes from date.
To format a date you can read my below article, here I have explained multiple ways to format a date.
https://reactjssnippet.com/posts/how-to-display-date-in-react-js/
Snippet
In this snippet, we will see a short example to get hours and minutes from date.
const date = new Date();
const hours = date.getHours();
const minutes = date.getMinutes();
Example
In this example, we will write a react js code to get hours and minutes from date and in the React UI.
Let’s list down what we are going to do below example.
- Create new date object.
- Get hours from date object.
- Get minutes from date object.
- Show hours and minutes in react ui.
import React from "react";
function App() {
const date = new Date();
const hours = date.getHours();
const minutes = date.getMinutes();
return (
<div>
<p>
The current time is {hours}:{minutes}
</p>
</div>
);
}
export default App;
Output