Js implements a countdown processing problem

implement a function:
the function is calculated in hours, such as the expiration time of the event is"20, 30, 30, 50 hours, and the current time examination is used to calculate the number of hours: minutes: seconds" from the arrival time to the target.

my preliminary code is:

setTime() {
    const currentTime = new Date();
    const arr = this.props.data.aimTime.split(":");  //:"20:30:45"
  
    const aimTime = 3600000 * (parseInt(arr[0])) + 60000 * (parseInt(arr[1])) + 1000 * (parseInt(arr[2]));   //
    const cuTime = (3600000 * currentTime.getHours()) + (60000 * currentTime.getMinutes()) + (1000 * currentTime.getSeconds());  //
    const remainTime = aimTime - cuTime;  //
    const hours = parseInt(arr[0]) - currentTime.getHours();  //
   
    const min = parseInt(arr[1]) > 0 ? (60 - currentTime.getMinutes() + parseInt(arr[1]))
      : 60 - currentTime.getMinutes();  //

    const second = parseInt(arr[2]) > 0 ? (60 - currentTime.getSeconds() + parseInt(arr[2]))
      : 60 - currentTime.getSeconds();   //

    
    remainTime > 0 &&
      this.setState({
        hours: hours > 9 ? hours : "0" + hours,
        min: min > 9 ? min : "0" + min,
        second: second > 9 ? second : "0" + second,
      });
  }

if the number of minutes and seconds is incorrect, how to correct it, or is there a better way to achieve this function

Mar.03,2021

function formatTime(t){
    t=t.toString();
    return t[1] ? t : '0'+t
}
function countDown(t){
    var maxTime=(new Date(t).getTime())-Date.now();
    if(maxTime>0){
        var h=parseInt(maxTime/(60*60*1000));
        var m=parseInt(maxTime/(60*1000)%60);
        var s=parseInt(maxTime/(1000)%60);
        var time=[h,m,s].map((el)=>formatTime(el)).join(':');
        console.log(time)
        setTimeout(countDown,1000,t);
    }else{
        console.log('timeout')
    }
}
countDown('2018-04-17 18:00:00');

there are many tools for time processing, such as moment.js , Datejs , date-fns , etc.

there is a relatively simple method

  http://www.jb51.net/article/9.

Menu