/*
功能:两个日期的时间差
参数:
start 开始时间 Date类型的对象
end 结束时间 Date类型的对象
返回值:返回一个对象(Object类型)
*/
<style>
span{
display: inline-block;
width: 40px;
height: 40px;
line-height: 40px;
margin-right: 10px;
background-color: pink;
color: #fff;
text-align: center
}
</style>
<span id="day">0</span>
<span id='hours'>0</span>
<span id='minute'>0</span>
function getDiff(start,end) {
// 1.创建一个开始的时间对象
// var start = new Date();
// 2.创建要一个未来时间对象
// var end = new Date(‘2019/7/10 22:18:18’);
// 3.求两个日前的时间差(差多少毫秒)
var temp = end.getTime() - start.getTime();
var date = parseInt(temp / 1000 / 60 / 60 / 24); //天
if(date>9){
date=date
}else{
date='0'+date
}
var hours = parseInt(temp / 1000 / 60 / 60 % 60); // 小时
if(hours>9){
hours=hours
}else{
hours='0'+hours
}
var minute = parseInt(temp / 1000 / 60 % 60); // 分钟
if(minute>9){
minute=minute
}else{
minute='0'+minute
}
var seconds = parseInt(temp / 1000 % 60); // 秒
if(seconds>9){
seconds=seconds
}else{
seconds='0'+seconds
}
var milliSecond = temp % 1000; // 毫秒
var obj = {
date:date,
hours:hours,
minute:minute,
seconds:seconds,
milliSecond:milliSecond
};
return obj;
}
setInterval(function() {
var s = new Date();
var e = new Date('2019/6/14 13:12:12');
var r = getDiff(s,e);
if(e>=s){
$('#day').text(r.date);
$('#hours').text(r.hours);
$('#minute').text(r.minute);
}
return false;
}, 1000);