First look at the complete code:
]
HTML part of the code:
You can enter the countdown time in the text box. If the current time is less than the deadline, it will return normally. Otherwise, it will return the time from the deadline to now
The code is as follows:
javascript part of the code :
The code is as follows:
function countDown(endDate) {
var now = new Date();
var deadtime = document.getElementById(endDate);
var deadline = new Date(deadtime.value);
//The difference between local time and Greenwich Mean Time (GMT) Minute difference
var timeDiff = now.getTimezoneOffset();
//There is some confusion here. If the minute difference is converted into milliseconds, it should be timeDiff*60*1000, but the data returned in this way is incorrect!
var leave = Math.abs(deadline.getTime() - now.getTime() timeDiff*60);
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var countDay = Math.floor(leave/day);
//var countHour = Math.floor((leave - day*countDay)/hour); Two calculation ideas
var countHour = Math.floor(leave/hour - countDay*24);
var countMinute = Math.floor(leave/minute) - countDay*24*60 - countHour*60;
var countSecond = Math. floor(leave/1000) - countDay*24*60*60 - countHour*60*60 - countMinute*60;
var outStr = "";
if(deadline < now) {
outStr = "distance" deadtime.value "has" countDay "days" countHour "hour" countMinute "minutes" countSecond "seconds";
} else {
outStr = "distance" deadtime.value "still has "countDay" days" countHour "hours" countMinute "minutes" countSecond "seconds";
}
var showTime = document.getElementById("showTime");
showTime.innerHTML = outStr;
}