Without further ado, go straight to the code.

<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta  name="viewport" content="width=device-width, Initial scale=1.0"> <title>Document</title> </head> <body> <div> <span class="hour">1</span> <span class="minute">2</span> <span class="second">3</span> </div> <script> var hour = document.querySelector('.hour'); Var minute = document.querySelector('.minute'); Var second = document.querySelector('.second'); Var inputTime = +new Date('2021-4-10 23:00:00'); CountDown () returns the total number of milliseconds entered by the user. // Start timer setInterval(countDown,1000); function countDown() { var nowTime = +new Date(); Var times = (inputtime-nowtime) / 1000; Var d = parseInt(times / 60/60/24); var d = parseInt(times / 60/60/24); // d = d < 10? '0' + d : d; var h = parseInt(times / 60 / 60 % 24); // h = h < 10? '0' + h : h; hour.innerHTML = h; Var m = parseInt(times / 60%60); // the current second m = m < 10? '0' + m : m; minute.innerHTML = m; Var s =parseInt(times%60); // The current second s = s < 10? '0' + s : s; second.innerHTML = s; / / / / return to the rest of the number of seconds to box d + + h + 'day' 'as' + m +' points' + + 's' s; } </script> </body> </html>Copy the code

This encapsulates a time conversion function:

 function countDown() {
            var nowTime = +new Date(a);// Return the number of milliseconds in the current time
            var times = (inputTime - nowTime) / 1000; //times is the total amount of time left
            var d = parseInt(times / 60 / 60 / 24); / / day
            d = d < 10 ? '0' + d : d;
            var h = parseInt(times / 60 / 60 % 24); / /
            h = h < 10 ? '0' + h : h;
            var m = parseInt(times / 60 % 60); / / the second
            m = m < 10 ? '0' + m : m;
            var s =parseInt(times%60);/ / the second
            s = s < 10 ? '0' + s : s;
         	return d + 'day' + h + 'when' + m + 'points' + s + '秒';
        }
Copy the code