{"size": 1185, "ext": "js", "max_stars_repo_path": "src/index.js", "max_stars_repo_name": "worldstats/react-countdown", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/index.js", "max_issues_repo_name": "worldstats/react-countdown", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/index.js", "max_forks_repo_name": "worldstats/react-countdown", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.2142857143, "max_line_length": 73, "alphanum_fraction": 0.5679324895}
import React, { useEffect, useState } from 'react'

function msToTime(duration) {
    let seconds = Math.floor((duration / 1000) % 60),
        minutes = Math.floor((duration / (1000 * 60)) % 60),
        hours = Math.floor((duration / (1000 * 60 * 60)) % 24),
        days = Math.floor(duration / (24 * 60 * 60 * 1000));

    days = String(days).padStart(2, '0')
    hours = String(hours).padStart(2, '0');
    minutes = String(minutes).padStart(2, '0');
    seconds = String(seconds).padStart(2, '0');

    return [days, hours, minutes, seconds];
}



// 2021-07-21T12:24:31Z
export const useCountDown = ({ targetDate }) => {
    let currentDate = Date.now(),
        tgtDate = Date.parse(targetDate),
        targetTimeInMs = tgtDate - currentDate
    const [target, setTarget] = useState(targetTimeInMs)
    useEffect(() => {
        const interval = setInterval(function(){
            setTarget(tar => tar - 1000)
        }, 1000)
        return (() => {
            clearInterval(interval)
        })
    },[])
    if (targetTimeInMs && !isNaN(targetTimeInMs) && targetTimeInMs > 0) {
        return msToTime(target)
    }
    return [
        '00', '00', '00', '00'
    ];

}

