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