JS计时器 年月日时分秒几十

计时器功能代码:

<html>
<body>

<script type="text/javascript">

//设置起始时间
var together = new Date();
together.setFullYear(2015,9,17); //时间年月日     注意这个9代表的是10月
together.setHours(0);//小时   
together.setMinutes(00);//分钟
together.setSeconds(0); //秒前一位
together.setMilliseconds(2);//秒第二位

//使用定时器调用这个函数 就可实现
timeElapse(together);

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> 秒"; 


document.write(result)
}

</script>

</body>
</html>

定时器在哪调用自行添加。

发布了97 篇原创文章 · 获赞 41 · 访问量 73万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章