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 时间提醒的功能。
以下这段代码是获取某个时间距离当前时间的天时分秒的数据。
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 时间提醒的功能。
相关文章推荐
- javascript生成器函数function generator初探和理解
- js实现文字拖动
- Jackson如何使JSON输出变得优雅?
- 【js基础】js排序方法——快排+堆排+插排+选择排
- js禁用按键和鼠标单击
- JSON解析----个人学习笔记
- js 调试
- Javascript学习笔记
- Javascript学习笔记
- js动态时钟
- js倒计时
- javascript中闭包
- js 正则
- JS日期加减
- JavaScript基础回顾
- JSP技术
- js正则表达式
- json字符串的拼接,并转换为json对象
- js老生常谈之this,constructor ,prototype
- 原产地政策,jsonp跨域