您的位置:首页 > Web前端 > JavaScript

JS 获取某个时间距离现在时间有多少时分秒

2015-09-10 21:38 639 查看
好记忆不如按烂笔头... ...

以下这段代码是获取某个时间距离当前时间的天时分秒的数据。

function showTime(){

var timeStr = $("#classTime").html(); //获得的时间字符串.

if(timeStr==null || timeStr==''){

return;

}

var end_str = (timeStr).replace(/-/g,"/");

var class_time = new Date(end_str); //将时间字符串转换为时间.

var now_time = new Date();

var totalSecs=(class_time-now_time)/1000; //获得两个时间的总毫秒数. 靠前的就调换再减。

var days=Math.floor(totalSecs/3600/24);

var hours=Math.floor((totalSecs-days*24*3600)/3600);

var mins=Math.floor((totalSecs-days*24*3600-hours*3600)/60);

var secs=Math.floor((totalSecs-days*24*3600-hours*3600-mins*60));

if (days != 0 ) {

$("#tellTime").html("还有"+days+"天"+hours+"小时"+mins+"分钟"+secs+"秒");

}else if (hours == 0 && mins == 0) {

$("#tellTime").html("还有"+secs+"秒");

}else if (hours == 0 && mins != 0) {

$("#tellTime").html("还有"+mins+"分钟"+secs+"秒");

}else if (hours != 0) {

$("#tellTime").html("还有"+hours+"小时"+mins+"分钟"+secs+"秒");

}

}

var clock;

window.onload=function(){

clock=self.setInterval("showTime()", 500);

}

通过以上步骤就可以实现js 时间提醒的功能。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: