How to create countdown timer in JavaScript

1 Answer

0 votes
const countDownTime = new Date().getTime() + 24 * 60 * 6;

let val = setInterval(function() {
      const now = new Date().getTime();
      const timeLeft = countDownTime - now;

      const days = Math.floor(timeLeft / (1000 * 60 * 60 *24));
      const hours = Math.floor( (timeLeft / (1000 * 60 * 60)) % 24);
      const minutes = Math.floor((timeLeft / 1000 / 60) % 60);
      const seconds = Math.floor((timeLeft / 1000) % 60);
			
      console.log(days + "d " + hours + "h " + minutes + "m " + seconds + "s");
      
      if (timeLeft < 0) {
          clearInterval(val);
      }
    	
   }, 1000);
   
   
   
   
   
/*
run:
   
"0d 0h 0m 7s"
"0d 0h 0m 6s"
"0d 0h 0m 5s"
"0d 0h 0m 4s"
"0d 0h 0m 3s"
"0d 0h 0m 2s"
"0d 0h 0m 1s"
"0d 0h 0m 0s"
"-1d -1h -1m -1s"
   
*/

 



answered Jan 24, 2022 by avibootz

Related questions

1 answer 103 views
1 answer 98 views
1 answer 107 views
1 answer 99 views
1 answer 96 views
1 answer 117 views
...