这篇文章主要介绍JavaScript网页中如何实现一个计算当年还剩多少时间的倒数计时程序,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
实例如下:
function counter() {
var date = new Date();
var year = date.getFullYear();
var date2 = new Date(year, 11, 30, 23, 59, 59);
var time = (date2 - date) / 1000;
var day = Math.floor(time / (24 * 60 * 60))
var hour = Math.floor(time % (24 * 60 * 60) / (60 * 60))
var minute = Math.floor(time % (24 * 60 * 60) % (60 * 60) / 60);
var second = Math.floor(time % (24 * 60 * 60) % (60 * 60) % 60);
var str = year + "年还剩" + day + "天" + hour + "时" + minute + "分" + second + "秒";
console.log(str);
}
window.setInterval("counter()", 1000);
这是我自己写的
var oDateNow = new Date();
var oYear = oDateNow.getFullYear();
var oDateEnd = new Date();
oDateEnd.setFullYear(oYear+1);
oDateEnd.setMonth(0);
oDateEnd.setDate(0);
oDateEnd.setHours(0);
oDateEnd.setMinutes(0);
oDateEnd.setSeconds(0);
function upDate(){
var oDateNow = new Date();
var iTime = oDateEnd.getTime()-oDateNow.getTime();
var iRemain = iTime/1000;
var iDay=parseInt(iRemain/86400);
iRemain%=86400;
var iHour=parseInt(iRemain/3600);
iRemain%=3600;
var iMin=parseInt(iRemain/60);
iRemain%=60;
var iSec=parseInt(iRemain);
document.write(oYear+"年还剩"+iDay+"天"+iHour+"时"+iMin+"分"+iSec+"秒");
}
setInterval(function(){ upDate() },1000);
这两个方法的秒数有出处
以上是“JavaScript网页中如何实现一个计算当年还剩多少时间的倒数计时程序”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网行业资讯频道!