1.0.2 • Published 4 years ago

react-use-timediff v1.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

react-use-timediff

React hook for calculate difference between two dates

NPM JavaScript Style Guide

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. If live 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 if live is set to true. Default: 1000;

License

MIT © @adilsonmandlate


This hook is created using create-react-hook.

1.0.2

4 years ago

1.0.1

4 years ago

0.1.0

4 years ago

1.0.0

4 years ago