时间:2020-12-28 11:05:59 | 栏目: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);
这两个方法的秒数有出处