bmalehorn

react-use-date

Have you ever used new Date() in React code, but noticed it didn't update?

The current time is: 2020-06-26T00:11:57.966Z

Instead, try using useDate():

import useDate from "react-use-date";

const Clock = () => {
  const date = useDate();

  return <div>The current time is: {date.toISOString()}</div>;
};
The current time is: 2020-06-26T00:11:57.967Z

Options by Example

optionresult
useDate()00:11:57.968
useDate({ interval: 'second' })00:11:57.968
useDate({ interval: 'minute' })00:11:57.968
useDate({ interval: 'hour' })00:11:57.968
useDate({ interval: 1000 })00:11:57.969

Get It!

  • npm install react-use-date
  • GitHub