关于一段判断时间JS代码,求解!
关于一段判断时间JS代码,求解!JS代码:functiontimeElapse(date){varcurrent=Date();varseconds=(Date.pars...
关于一段判断时间JS代码,求解!
JS代码:
function timeElapse(date){
var current = Date();
var seconds = (Date.parse(current) - Date.parse(date)) / 1000;
var days = Math.floor(seconds / (3600 * 24));
seconds = seconds % (3600 * 24);
var hours = Math.floor(seconds / 3600);
if (hours < 10) {
hours = "0" + hours;
}
seconds = seconds % 3600;
var minutes = Math.floor(seconds / 60);
if (minutes < 10) {
minutes = "0" + minutes;
}
seconds = seconds % 60;
if (seconds < 10) {
seconds = "0" + seconds;
}
var result = "第 <span class=\"digit\">" + days + "</span> 天 <span class=\"digit\">" + hours + "</span> 小时 <span class=\"digit\">" + minutes + "</span> 分钟 <span class=\"digit\">" + seconds + "</span> 秒";
$("#clock").html(result);
这JS计算出来的时间不正确呢~ 求大神帮忙改改!
HTML 部分代码:
var textAnimate = eval(Jscex.compile("async", function () {
var together = new Date();
together.setFullYear(2013, 07, -5);
together.setHours(16);
together.setMinutes(53);
together.setSeconds(0);
together.setMilliseconds(2); 展开
JS代码:
function timeElapse(date){
var current = Date();
var seconds = (Date.parse(current) - Date.parse(date)) / 1000;
var days = Math.floor(seconds / (3600 * 24));
seconds = seconds % (3600 * 24);
var hours = Math.floor(seconds / 3600);
if (hours < 10) {
hours = "0" + hours;
}
seconds = seconds % 3600;
var minutes = Math.floor(seconds / 60);
if (minutes < 10) {
minutes = "0" + minutes;
}
seconds = seconds % 60;
if (seconds < 10) {
seconds = "0" + seconds;
}
var result = "第 <span class=\"digit\">" + days + "</span> 天 <span class=\"digit\">" + hours + "</span> 小时 <span class=\"digit\">" + minutes + "</span> 分钟 <span class=\"digit\">" + seconds + "</span> 秒";
$("#clock").html(result);
这JS计算出来的时间不正确呢~ 求大神帮忙改改!
HTML 部分代码:
var textAnimate = eval(Jscex.compile("async", function () {
var together = new Date();
together.setFullYear(2013, 07, -5);
together.setHours(16);
together.setMinutes(53);
together.setSeconds(0);
together.setMilliseconds(2); 展开
1个回答
推荐于2016-03-23
展开全部
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Ball</title>
<style type="text/css">
</style>
<script type="text/javascript" src="jquery-1.8.0.min.js"></script>
<script type="text/javascript">
function timeElapse (date)
{
var reg = /^[1-9]$/;
var current = new Date ();
var d = new Date (date.replace (/[^\s\d]/g, '/'));
var disp = Math.abs (current.getTime () - d.getTime ());
var days = Math.floor (disp / 1000 / 60 / 60 / 24);
var hours = Math.floor (disp / 1000 / 60 / 60 % 24);
hours = reg.test(hours) ? "0" + hours : hours;
var minutes = Math.floor (disp / 1000 / 60 % 60);
minutes = reg.test(minutes) ? "0" + minutes : minutes;
var seconds = Math.floor (disp / 1000 % 60);
seconds = reg.test(seconds) ? "0" + seconds : seconds;
var result = "与当前时间相差 <span class=\"digit\">" + days + "</span> 天 <span class=\"digit\">" + hours
+ "</span> 小时 <span class=\"digit\">" + minutes + "</span> 分钟 <span class=\"digit\">" + seconds
+ "</span> 秒";
$ ("#clock").html (result);
TIMEOUT = setTimeout (function ()
{
timeElapse (date);
}, 1000);
}
var TIMEOUT = null;
$ (function ()
{
var txt = $ (":text");
$ (":button").click (function ()
{
clearTimeout (TIMEOUT);
timeElapse (txt.val ());
})
})
</script>
</head>
<body>
<input type="text" />
<button>test</button>
<div id="clock"></div>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询