Make a Javascript Clock
Sunday, October 26th, 2008Although there are many ways to make a clock using Javascript, most use Javascript’s date() object and the setInterval() method. In this tutorial we’ll start by building a clock using these simple functions. Then we’ll explore some interesting ways of optimizing our clock, and hopefully learn a whole bunch of Javascript along the way.
Getting the current time
Let’s kick things off by getting the current time with Javascript. To do so, we will need the Javascript date() object. Remember that this gets the client-side time, e.g. the time in the user’s browser, not the actual time on the server. Client-side time is great for situations like ours, since it takes away any time-zone localization problems.
The date() object is simple enough to use, just instantiate it:
var d = new Date();
Then you can call any properties of this object, getMinutes(), getDay(), getDate(), etc. … (more…)