倒计时(自留)
2018-02-24 本文已影响0人
Cherry9507
<p id="pobj"><p>
<script>
function time(){
//目标时间
var targetDate=new Date("2019/1/1")
//获取当前时间
var now = new Date()
//目标时间和当前时间相差的毫秒
var millon =targetDate.getTime() - now.getTime()
var seconds =millon/1000
//一天的毫秒数=24*60*60*1000
var days = parseInt(seconds/(24*60*60))
//剩余的小时=(millon - days *24*60*60*1000)/(60*60*1000)
var hours =parseInt(seconds%(24*60*60)/3600)
var minutes = parseInt(seconds%3600/60)
var ss =parseInt(seconds%60)
var pobj =document.getElementById("pobj")
pobj.innerHTML="离2019年元旦还剩"+days+"天"+hours+"小时"+minutes+"分钟"+ss+"秒"
}
setInterval("time()",1000)
<script>