react-use-timediff
v1.0.2
Published
React hook to calculate diference between two dates
Downloads
4
Readme
react-use-timediff
React hook for calculate difference between two dates
Install
Using npm:
npm install --save react-use-timediff
using yarn:
yarn add react-use-timediff
Usage
import React, { Component } from "react";
import { useTimeDiff } from "react-use-timediff";
const Example = () => {
const date = (new Date(2020, 11, 31,23,59,59));
const time = useTimeDiff(date, {
live: true
});
return (
<main>
<h1>Remaing time to the end of the year: </h1>
<section>
<span>Years: {time.years}</span>
<span>Months: {time.months}</span>
<span>Days: {time.days}</span>
<span>Hours: {time.hours}</span>
<span>Minutes: {time.minutes}</span>
<span>Seconds: {time.seconds}</span>
</section>
</main>;
)
};
API
useTimeDiff(
eventDate: Date | number,
options?: {
startDate?: Date | number,
live?: boolean,
interval?: number
}
): Object
Required:
eventDate
: The final date, a Date object or milliseconds, to make the calculation.
Optional:
startDate
: The initial date to make the calculations, a Date object or milliseconds. Iflive
is true, this will not be used. Default: Date.now();live
: Whether it should update the time left, periodically, in the provided interval. Default: false;interval
: The periodic update interval of the remaing time, in milliseconds. This will only be used iflive
is set to true. Default: 1000;
License
MIT © @adilsonmandlate
This hook is created using create-react-hook.