scriptygoddess

25 Aug, 2003

Simple Javascript clock

Posted by: Jennifer In: Scripts

A neat, and easy, way to put a digital clock right into the code of your website/blog. Note that instead of a div, you can also use a span, as long as you use the same id.

Put this in your webpage:

<div id="digitalclock"></div>

Then, you can put this directly below that, or you can put it elsewhere if you wish. Up to you:

<script language="javascript">
function calctime()
{
var currenttime = new Date();
var hours = currenttime.getHours();
var minutes = currenttime.getMinutes();
var seconds = currenttime.getSeconds();
var timesuffix = "AM";
if (hours > 11)
{
timesuffix = "PM";
hours = hours – 12;
}
if (hours == 0)
{
hours = 12;
}
if (hours < 10)
{
hours = "0" + hours;
}
if (minutes < 10)
{
minutes = "0" + minutes;
}
if (seconds < 10)
{
seconds = "0" + seconds;
}
var clocklocation = document.getElementById('digitalclock');
clocklocation.innerHTML = hours + ":" + minutes + ":" + seconds + " " + timesuffix;
setTimeout("calctime()", 1000);
}
calctime();
</script>

Simple modifications to this would give you lower-case AM and PM, and can even give a 24-hour clock.

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

3 Responses to "Simple Javascript clock"

1 | Deltus

August 26th, 2003 at 9:09 am

Avatar

Does the clock still keep proper time? Were you sure to make the line

setTimeout("calctime()", 1000);

set to 1000, and not a smaller number? Otherwise, what's the URL so I can look at your code. I'd be glad to help.

2 | lingosphere daily

October 12th, 2003 at 2:11 pm

Avatar

Digital Clock…
Thanks to Deltus at scratchingtheitch.com's digital clock script, which I added over near the calendar. [via scriptygoddess]…

3 | wcoco

August 26th, 2003 at 2:20 am

Avatar

when running this with mozilla browser (v1.3), the time string starts to blink after 3 seconds and thereafter. no problem from IE. any idea?

Featured Sponsors


  • Curt: If anyone comes across this with similar issues I was able to sort out the pagination issues painlessly with easyCommentsPaginate from http://www.mush
  • Christopher: Yeah, it is indeed hard to do. And something remains elusive about why the pagination never worked. I tried everything I could find. Regardless, I
  • Jennifer: Hi Christopher, always hard to bug test stuff like that remotely. Sorry those didn't help. Glad you found a solution though :)

About


Advertisements