Countdown that is renewed at midnight using pure javascript

1
  

How do I show the missing hours and minutes by 00:00 (midnight)?

Example:

  

I have a sales site where I want to put a countdown that tells the remaining time to midnight, which when midnight comes, appears 23h 59m 59s.

It's going to be some kind of propaganda, like, "Run! There's only XXh XX m XXs left"

If the person has for example at 20:31, it will show 3h 29m 00s.

And so sussecientemente ....

I saw this example, but I did not have the ability to make it work: link

I just want something simple, just a text "XXh XXm XXs". No CSS.

    
asked by anonymous 11.08.2016 / 11:26

1 answer

3

You can do this:

function calculateHMSleft()
{
	var now = new Date();
	var hoursleft = 23-now.getHours();
	var minutesleft = 59-now.getMinutes();
	var secondsleft = 59-now.getSeconds();
	if(minutesleft<10) minutesleft = "0"+minutesleft;
	if(secondsleft<10) secondsleft = "0"+secondsleft;
	document.getElementById('count').innerHTML = hoursleft+":"+minutesleft+":"+secondsleft;
}
calculateHMSleft();
setInterval(calculateHMSleft, 1000);
<div id="count"></div>

Reply adapted from here

    
11.08.2016 / 11:32