JavaScript web page to achieve a calculation of how much time left in the year of the countdown program

• 2021-07-15 03:36:48
• OfStack

See this program on the Internet and find a mistake. Correct it 1 times here:

``````
function counter() {
var date = new Date();
var year = date.getFullYear();
var date2 = new Date(year, 11, 30, 23, 59, 59);
/* Convert to seconds */
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 + " Years left " + day + " Days " + hour + " Hour " + minute + " Points " + second + " Seconds ";
console.log(str);
}
window.setInterval("counter()", 1000); ``````

I wrote this myself

``````
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+" Years left "+iDay+" Days "+iHour+" Hour "+iMin+" Points "+iSec+" Seconds ");
}
setInterval(function(){ upDate() },1000);``````

The number of seconds of these two methods has a source

Related articles: