时间戳

618 阅读1分钟

时间戳

时间戳是从1970年1月1日0:0:0开始,到现在某一时间的的总毫秒数

1.1时间戳的获取方式

第一种获取方式
 var date=new Date()
console.log(date.valueOf())
第二种获取方式
 console.log(date.getTime())
第三种获取方式
  console.log(+new Date())//这个+相当于valueOf
第四种获取方式
console.log(Date.now())

1.2倒计时案例

 function getDownTime(time){
            var nowtime=+new Date();
            var willtime=+new Date(time)
            var downtime=(willtime-nowtime)/1000 //毫秒除以1000得到秒
            var day=parseInt(downtime/60/60/24);
            var hour=parseInt(downtime/60/60%24);//与24小时取余得到剩余小时数
            var minutes=parseInt(downtime/60%60);
            var seconds=parseInt(downtime%60);
            return day+"天"+hour+"小时"+minutes+"分钟"+seconds+"秒"
            
        }
        console.log(getDownTime("2020-3-20 12:00:00"))