Vue中的时间转换,把毫秒换算成正常时间
生活随笔
收集整理的這篇文章主要介紹了
Vue中的时间转换,把毫秒换算成正常时间
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、53200毫秒
只到秒,沒(méi)有分,沒(méi)有進(jìn)位,只要簡(jiǎn)單的除以1000就可以了
{{Math.round(time0/1000)}}二、3022222毫秒
進(jìn)位到分鐘,需要除以60,然后把小數(shù)點(diǎn)前后部分的數(shù)值分開(kāi)來(lái)處理
{{Math.floor(Math.round(time/1000)/60)}}:{{Math.round(time/1000)%60}}三、32300432毫秒
進(jìn)位到了小時(shí),需要把除以60得到的整數(shù)部分,再除以60,把得到的數(shù)的小數(shù)和整數(shù)部分分開(kāi)繼續(xù)處理
{{Math.floor(Math.floor(Math.round(time2/1000)/60)/24) }}:{{Math.floor(Math.round(time2/1000)/60)%24 }}:{{Math.round(time2/1000)%60}}四、4242323234毫秒
分步除以60、60、24,一共要處理四個(gè)數(shù)
{{Math.floor(Math.floor(Math.floor(Math.round(time3/1000)/60)/60)/24)}}天{{Math.floor(Math.floor(Math.round(time3/1000)/60)/60)%24}}時(shí){{Math.floor(Math.round(time3/1000)/60)%60}}分{{Math.round(time3/1000)%60}}秒顯示結(jié)果:
五、53200毫秒
把第一步的數(shù)和第四步的代碼結(jié)合起來(lái)看看,出來(lái)的是什么結(jié)果
這樣顯示明顯不合適,0的時(shí)候就可以不用顯示
加個(gè)v-show來(lái)做下判斷,代碼就改進(jìn)成下面這樣
給div都加上display: inline-block;改成行內(nèi)塊,讓它們能顯示在同一行
<div><div v-show="time0/1000>86400" style="display: inline-block;">{{Math.floor(Math.floor(Math.floor(Math.round(time0/1000)/60)/60)/24)}}天</div><div v-show="time0/1000>3600" style="display: inline-block;">{{Math.floor(Math.floor(Math.round(time0/1000)/60)/60)%24}}時(shí)</div> <div v-show="time0/1000>60" style="display: inline-block;">{{Math.floor(Math.round(time0/1000)/60)%60}}分</div> {{Math.round(time0/1000)%60}}秒</div>顯示就正常了
再加個(gè)三目運(yùn)算把個(gè)位數(shù)的前面都拼接上0
然后代碼一下就變得無(wú)比的長(zhǎng)
<div><div v-show="time0/1000>86400" style="display: inline-block;">{{Math.floor(Math.floor(Math.floor(Math.round(time0/1000)/60)/60)/24)<10?('0'+Math.floor(Math.floor(Math.floor(Math.round(time0/1000)/60)/60)/24)):Math.floor(Math.floor(Math.floor(Math.round(time0/1000)/60)/60)/24)}}天</div><div v-show="time0/1000>3600" style="display: inline-block;">{{Math.floor(Math.floor(Math.round(time0/1000)/60)/60)%24<10?('0'+Math.floor(Math.floor(Math.round(time0/1000)/60)/60)%24):Math.floor(Math.floor(Math.round(time0/1000)/60)/60)%24}}時(shí)</div> <div v-show="time0/1000>60" style="display: inline-block;">{{Math.floor(Math.round(time0/1000)/60)%60<10?('0'+Math.floor(Math.round(time0/1000)/60)%60):Math.floor(Math.round(time0/1000)/60)%60}}分</div> {{Math.round(time0/1000)%60<10?('0'+Math.round(time0/1000)%60):Math.round(time0/1000)%60}}秒</div>顯示效果對(duì)比
重點(diǎn):
%:取余
Math.floor():相當(dāng)于省去小數(shù)后的數(shù)字
Math.round():四舍五入?
總結(jié)
以上是生活随笔為你收集整理的Vue中的时间转换,把毫秒换算成正常时间的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 3D建模软件
- 下一篇: springboot + vue 时区问