Counting down to 11pm using Javascript.

Once again I found myself pondering another use for Javascript to help make my work more easier and to address what is perhaps my biggest concern during my shift. How much longer before I can go home?

I have seen tons of sample scripts that count down to a specific date, which is nice but I wanted to create a script that counts down to a specific time instead. In my case I wanted the script to count down to 11pm, which marks the end of my shift. I also wanted to be able to run this same script each day without having to change anything in the code, as I noticed in the date countdown scripts I saw.

So. Off I went to find the script. After some searching and sweating I came across this discussion forum in which a fellow Javascript novice needed some help creating a countdown script similar to what I was looking for. The moderator responded by posting a countdown script, one that I tried out and liked well enough for me to experiment with to get it to do my bidding.

Here’s my variation on the forum moderator’s script which counts down to 11pm:

<title>End of Shift Countdown</title>
<script type=”text/javascript”>
// From:
// Modified for:
var definedTime = new Date(’23:00′); // military time to countdown TO
function ShowTimes() {
var now = new Date();
var hrs = 22-now.getHours();
if (hrs < 0) { hrs += 24; }
var mins = 60-now.getMinutes();
var secs = 60-now.getSeconds();
var str = ”;
str += ‘<center><font size=”7″><br>’+hrs+’ hours, ‘+mins+’ minutes to go</font></center>’;
document.getElementById(‘countdownToMidnight’).innerHTML = str;

var _cntDown;
function StopTimes() { clearInterval(_cntDown); }
<body onload=”_cntDown=setInterval(‘ShowTimes()’,1000)”>
<div id=”countdownToMidnight”></div>

The biggest change I made to the script was modifying the “var hrs” line to subtract 22 from the getHours() variable. I cleaned up the output and did away with displaying the seconds as all I really wanted to do was just count down the hours and minutes to 11pm.

I still consider myself a total novice when it comes to Javascript but it’s nice to know the web is full of sample scripts to help me along.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s