bmalehorn

react-use-date

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

The current time is: 2021-08-04T18:25:27.130Z

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: 2021-08-04T18:25:27.131Z

Options by Example

optionresult
useDate()18:25:27.131
useDate({ interval: 'second' })18:25:27.131
useDate({ interval: 'minute' })18:25:27.132
useDate({ interval: 'hour' })18:25:27.132
useDate({ interval: 1000 })18:25:27.132

Get It!

  • npm install react-use-date
  • GitHub