Here’s how to have a realtime display showing how many hours, minutes, and seconds are left in the day:
Time left until midnight: <span id='HMSremaining'></span>
<script>
function calculateHMSleft()
{
//calculate
var now = new Date();
var hoursleft = 23-now.getHours();
var minutesleft = 59-now.getMinutes();
var secondsleft = 59-now.getSeconds();
//format 0 prefixes
if(minutesleft<10) minutesleft = "0"+minutesleft;
if(secondsleft<10) secondsleft = "0"+secondsleft;
//display
$('#HMSremaining').html(hoursleft+":"+minutesleft+":"+secondsleft);
}
calculateHMSleft();
setInterval(calculateHMSleft, 1000);
</script>
If you calculate how many days until an event, then you can append that and have a countdown timer to any holiday or special occasion!
Leave a Reply