js calculate the time difference

计算时差

<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>计算时差</title>
    <script src="jquery.js"></script>
</head>

<body>
    <div id="elapseClock"></div>
    <script>

        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> days <span class=\"digit\">" + hours + "</span> hours <span class=\"digit\">" + minutes + "</span> minutes <span class=\"digit\">" + seconds + "</span> seconds";
            $("#elapseClock").html(result);
        }

        var together = new Date();
        together.setFullYear(2013, 2, 28);
        together.setHours(20);
        together.setMinutes(0);
        together.setSeconds(0);
        together.setMilliseconds(0);

        timeElapse(together);

        //设置定时器,页面时间定时更新
        setInterval(function () {
		timeElapse(together);
	}, 500);
    </script>
</body>

</html>

Guess you like

Origin blog.csdn.net/danai2009/article/details/92390067