實(shí)現(xiàn)一個(gè)功能:以小時(shí)計(jì)算,如活動(dòng)到期時(shí)間為'20:30:50',以當(dāng)前的時(shí)間考試計(jì)算離到達(dá)目標(biāo)是時(shí)間剩余多少‘小時(shí):分:秒’的功能。我的初步代碼是:setTime(){constcurrentTime=newDate();constarr=this.props.data.aimTime.split(':');//輸入字符串格式的時(shí)間格式,如:'20:30:45'constaimTime=3600000*(parseInt(arr[0]))+60000*(parseInt(arr[1]))+1000*(parseInt(arr[2]));//計(jì)算目標(biāo)時(shí)間毫秒數(shù)constcuTime=(3600000*currentTime.getHours())+(60000*currentTime.getMinutes())+(1000*currentTime.getSeconds());//當(dāng)前時(shí)間毫秒數(shù)constremainTime=aimTime-cuTime;//一天內(nèi)離目標(biāo)時(shí)間剩余毫秒數(shù)consthours=parseInt(arr[0])-currentTime.getHours();//剩余小時(shí)constmin=parseInt(arr[1])>0?(60-currentTime.getMinutes()+parseInt(arr[1])):60-currentTime.getMinutes();//剩余分鐘,有問題感覺constsecond=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,});}這樣寫分鐘和秒數(shù)不正確,該如何更正,或者有什么更好的方式實(shí)現(xiàn)這一功能
js實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)處理問題
不負(fù)相思意
2019-05-21 17:17:33