看到网上这个程序 发现了处错误这里改正了一下:
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 网页中实现一个计算当年还剩多少时间的倒数计时程序,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章: