JavaScript 代码
<script type="text/javascript">
var time_start = new Date('2018','7','15');//开始时间,注意!!!如果从8月15开始,月份就是7月15
var clock_start = time_start.getTime();
//计算时间差
function get_time_spent() {
var time_now = new Date();
return ((time_now.getTime() - clock_start));
}
function get_times() {
var i_total_secs = Math.round(get_time_spent());
//计算出相差天数
var days = Math.floor(i_total_secs / (24 * 3600 * 1000))
//计算出小时数
var leave1 = i_total_secs % (24 * 3600 * 1000) //计算天数后剩余的毫秒数
var hours = Math.floor(leave1 / (3600 * 1000))
//计算相差分钟数
var leave2 = leave1 % (3600 * 1000) //计算小时数后剩余的毫秒数
var minutes = Math.floor(leave2 / (60 * 1000))
//计算相差秒数
var leave3 = leave2 % (60 * 1000) //计算分钟数后剩余的毫秒数
var seconds = Math.round(leave3 / 1000)
if (seconds.toString(www.mhylpt.com).length == 1) {
seconds = '0' + seconds;
}
console.log(days + '天' + hours + '个小时' + minutes + '分钟' + seconds + '秒');
document.getElementById('timer').innerHTML="<div>" + days + " <span>天</span></div>" +
"<div>" + hours + " <span>小时</span></div>" +
"<div>" + minutes + " <span>分钟<www.huayi157.com /span></div>" +
"<div>" + seconds + " <span>秒<www.myzx1.com /span></div>"
window.setTimeout('show_secs()', 1000); //每隔1s刷新一次input里的值
}
</script>
HTML代码
<body οnlοad=" window.setTimeout('get_times()',1);">
<div id="timer">
<div>296 <span>天</span></div>
<div>23 <span>小时</span></div>
<div>32 <span>分钟</span></div>
<div>01 <span>秒</span></div>
</div>
</body>
2、倒计时
JavaScript代码
<script type="text/javascript">
//获取截至时间
const endTime = new Date("2019/10/21 00:00:00").getTime();
let timer = setInterval(function() {
const today = new Date().getTime();
// 获取时间差
const timer = endTime - today;
// 获取天数、月数、分钟数、秒数
let days = Math.floor(timer / (1000 * 60 * 60 * 24));
let hours = Math.floor((timer % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
let minutes = Math.floor((timer % (1000 * 60 * 60)) / (1000 * 60));
let seconds = Math.floor(www.enzuo178.com(timer % (1000 * 60)) / 1000);
if(days <=0){
document.getElementById(www.tiaotiaoylzc.com'days').innerHTML=0;
document.getElementById('hours').innerHTML=0;
document.getElementById(www.dfzx157.com'minutes'www.dfgjpt.com).innerHTML=0;
document.getElementById('seconds'www.yongshi123.cn).innerHTML=0;
}else{
document.getElementById('days').innerHTML=days;
document.getElementById('hours').innerHTML=hours;
document.getElementById('minutes').innerHTML=minutes;
document.getElementById('seconds'www.yongshiyule178.com).innerHTML=seconds;
}
}, 1000);
</script>
HTML代码
<div>
<span id="days"></span>天
<span id="hours"></span>小时
<span id="minutes"></span>分
<span id="seconds"></span>秒
</div>
Javascript实现倒计时和根据某时间开始计算时间
最新推荐文章于 2023-05-31 18:30:08 发布