473,498 Members | 1,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

clock in javascript

is there a way to display a ticking clock in a web page using
javascript? but not in a textbox, rather as text that i can change the
style, font etc.

cenk tarhan

Dec 30 '06 #1
12 4061
VK

ce************@gmail.com wrote:
is there a way to display a ticking clock in a web page using
javascript? but not in a textbox, rather as text that i can change the
style, font etc.
<http://www.dynamicdrive.com/dynamicindex6/clock2.htm>

see <http://www.dynamicdrive.com/dynamicindex6/for more

Dec 30 '06 #2
thanks a lot VK, for the qucikest reply ever!

Dec 30 '06 #3
ce************@gmail.com wrote:
is there a way to display a ticking clock in a web page using
javascript? but not in a textbox, rather as text that i can change the
style, font etc.

cenk tarhan
<html>
<head>
<title>Clock</title>
</head>
<body>
<body>
<div id='clock' class='somecssstyle'></div>

<script langage='javascript :-P' type='text/javascript'>
var months=new Array("January", "February", "Mararch", "April",
"May", "June", "Julu", "August", "September", "October", "November",
"December");
var calDays=new Array("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday");
var clockID = document.getElementById('clock');

function pad(i) {
//simple function to just add a zero in front of numbers less
than zero (so we get 12:05 instead of 12:5)
if (i < 10) {
i = "0"+i;
}
return(i);
}

function doClock() {
setTimeout( "doClock()", 1000 );
t = new Date();
m = t.getMonth();
d = t.getDay();
dt = t.getDate();
y = t.getFullYear();
h = t.getHours();
if (h < 12) {
ap="AM";
} else {
ap="PM";
h=h-12;
}
mn= pad(t.getMinutes());
s = pad(t.getSeconds());
if (h==0) {
h = 12
}
clockID.innerHTML=calDays[d]+", "+months[m]+" "+dt+"
"+y+"<BR>"+h+":"+mn+":"+s+" "+ap;
}

doClock()
</script>
</body>

</html>

Happy new year -- or else!

--
http://www.hunlock.com -- Musings in Javascript, CSS.
$FA
Dec 30 '06 #4
pcx99 wrote:
ce************@gmail.com wrote:
>is there a way to display a ticking clock in a web page using
javascript? but not in a textbox, rather as text that i can change the
style, font etc.
[...]
<div id='clock' class='somecssstyle'></div>

<script langage='javascript :-P' type='text/javascript'>
Was there ever a script element attribute called 'langage'? ;-)

var months=new Array("January", "February", "Mararch", "April",
"May", "June", "Julu", "August", "September", "October", "November",
"December");
I think you need to use a spell checker.

var calDays=new Array("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday");
var clockID = document.getElementById('clock');

function pad(i) {
//simple function to just add a zero in front of numbers less than
zero (so we get 12:05 instead of 12:5)
if (i < 10) {
i = "0"+i;
}
return(i);
}
A more concise function for this case is:

function pad(i){
return (i<10)? '0'+i : ''+i;
}

function doClock() {
setTimeout( "doClock()", 1000 );
Simply setting a timeout of 1000 ms does not guarantee that the function
will run at exactly 1 second intervals, otherwise you could just use
setInterval.

t = new Date();
m = t.getMonth();
d = t.getDay();
dt = t.getDate();
y = t.getFullYear();
h = t.getHours();
if (h < 12) {
ap="AM";
} else {
ap="PM";
h=h-12;
}
mn= pad(t.getMinutes());
s = pad(t.getSeconds());
if (h==0) {
h = 12
I never understood the concept of representing times between midnight
and 1:00 am as 12:xx am. Why not 00:xx am? There is far less chance of
confusion. A large percentage of the world uses a 24hr clock, so maybe
that's a better idea.

I don't know why anyone wants to put an clock in a web page unless it's
to display the time somewhere other that where the visitor is located.
Anyhow, here's another effort. I find changing the value of a text node
causes less flicker and is faster in some browsers than changing the
innerHTML.

<script type="text/javascript">

function Clock(id) {
if (typeof id == 'string') id = document.getElementById(id);
this.el = id;
}

Clock.prototype.month = ['Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'];

Clock.prototype.day = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];

Clock.prototype.addZ = function(n){ return (n<10)? '0'+n: ''+n; };

Clock.prototype.tick = function(){
var clock = this;
var t = new Date();
this.el.firstChild.data = clock.day[t.getDay()] + ', '
+ clock.addZ(t.getDate()) + ' '
+ clock.month[t.getMonth()] + ' '
+ t.getFullYear() + ', '
+ clock.addZ(t.getHours()) + ':'
+ clock.addZ(t.getMinutes()) + ':'
+ clock.addZ(t.getSeconds());

setTimeout(function(){clock.tick();}, (1.05 - t.getMilliseconds()));
}

</script>

<span id="clock">&nbsp;</span>
<script type="text/javascript">
var digClock = new Clock('clock'); digClock.tick();
</script>
--
Rob
Dec 31 '06 #5
Lee
RobG wrote:
I never understood the concept of representing times between midnight
and 1:00 am as 12:xx am.
That's how it's been represented on most analog clocks for centuries.
Dec 31 '06 #6
Lee wrote:
RobG wrote:
I never understood the concept of representing times between midnight
and 1:00 am as 12:xx am.

That's how it's been represented on most analog clocks for centuries.
Which doesn't explain why it should be used with digital clocks based
on 24 hr time, even those that wish to use 12 hour intervals with am
and pm. 00:15 (am) is unambiguous, 12:15 am can easily be
misunderstood. 12:00 am and 12:00 pm make no sense and create
confusion.

Only a small proportion of the world's population uses 12 hour notation
with am/pm, 24 hr notation is preferable on the web.

Happy new year :-)
--
Rob

Dec 31 '06 #7
In comp.lang.javascript message <11*********************@a3g2000cwd.goog
legroups.com>, Sat, 30 Dec 2006 14:59:19, ce************@gmail.com
posted:
>is there a way to display a ticking clock in a web page using
javascript? but not in a textbox, rather as text that i can change the
style, font etc.
<URL:http://www.merlyn.demon.co.uk/js-date2.htm>
<URL:http://www.merlyn.demon.co.uk/js-anclk.htm>

It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Dec 31 '06 #8
In comp.lang.javascript message <459763f0$0$2605$5a62ac22@per-
qv1-newsreader-01.iinet.net.au>, Sun, 31 Dec 2006 17:17:01, RobG
<rg***@iinet.net.auposted:
>pcx99 wrote:
>A more concise function for this case is:

function pad(i){
return (i<10)? '0'+i : ''+i;
}
or

function pad(i) { return (i<10 ? '0' : '') + i }
setTimeout(function(){clock.tick();}, (1.05 - t.getMilliseconds()));
}
I think you mean 1050 not 1.05 ; I wonder how that method compares in
efficiency with (1050 - t%1000), and how one could tell.

For anywhere using ISO 8601 time format, something like
new Date().toString().match(/[\d:]{8}/)
or new Date().toString().match(/([\d:]{8})/)[1]
or new Date().toString().match(/\s(\d+:[\d:]+)\s/)[1]
will give hh:mm:ss.

Since the OP wanted a clock, I see no need to provide a calendar
function.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/- w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/- see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Jan 1 '07 #9
Lee
Fred wrote:
Lee wrote:
>RobG wrote:
>>I never understood the concept of representing times between midnight
and 1:00 am as 12:xx am.
That's how it's been represented on most analog clocks for centuries.

Which doesn't explain why it should be used with digital clocks based
on 24 hr time, even those that wish to use 12 hour intervals with am
and pm. 00:15 (am) is unambiguous, 12:15 am can easily be
misunderstood.
"12:15 am" cannot easily be misunderstood by anyone who understands
what am and pm mean. "12:00 am" seems to be a problem for people who
have trouble with concept formation.

I personally perfer 24-hour time notation, but if you're going to
use am and pm, the midnight hour is correctly represented as "12".
Jan 1 '07 #10
Dr J R Stockton wrote:
In comp.lang.javascript message <459763f0$0$2605$5a62ac22@per-
qv1-newsreader-01.iinet.net.au>, Sun, 31 Dec 2006 17:17:01, RobG
<rg***@iinet.net.auposted:
[...]
setTimeout(function(){clock.tick();}, (1.05 - t.getMilliseconds()));
}

I think you mean 1050 not 1.05 ;
Yes - 1.05 makes the clock update rather too often :-x

I wonder how that method compares in
efficiency with (1050 - t%1000), and how one could tell.
For a single call? Impossible using javascript I think, it probably
requires acccurate measurement of nanoseconds. :-) Running a loop of
10,000 calls shows getMilliseconds is 20% faster in Safari but takes 3
times longer than the remainder operator in Firefox on OS X.

For anywhere using ISO 8601 time format, something like
new Date().toString().match(/[\d:]{8}/)
or new Date().toString().match(/([\d:]{8})/)[1]
or new Date().toString().match(/\s(\d+:[\d:]+)\s/)[1]
will give hh:mm:ss.

Since the OP wanted a clock, I see no need to provide a calendar
function.
I was thinking the same thing - if a 24hr clock suits then:

<script type="text/javascript">
function runClock(el) {
if (typeof el == 'string') el = document.getElementById(el);
var t = new Date();
el.firstChild.data = t.toString().match(/([\d:]{8})/)[1];
setTimeout( function(){runClock(el);}, 1050 - t%1000 );
}
</script>

<div id="digiClock">&nbsp;</div>
<script type="text/javascript">runClock('digiClock');</script>
--
Rob

Jan 2 '07 #11
Lee wrote:
Fred wrote:
>Lee wrote:
>>RobG wrote:

I never understood the concept of representing times between midnight
and 1:00 am as 12:xx am.
That's how it's been represented on most analog clocks for centuries.

Which doesn't explain why it should be used with digital clocks based
on 24 hr time, even those that wish to use 12 hour intervals with am
and pm. 00:15 (am) is unambiguous, 12:15 am can easily be
misunderstood.

"12:15 am" cannot easily be misunderstood by anyone who understands
what am and pm mean. "12:00 am" seems to be a problem for people who
have trouble with concept formation.
To be absolutely strict, it is "12:00 noon" and "12:00 midnight".
Neither one of the two is either AM or PM. However, a great many
mechanical and electronic time displays have problems with this, and so
"12:00 a.m." has come to be used to mean "12:00 midnight" and "12:00
p.m." to mean "12:00 noon".

--
John W. Kennedy
"The blind rulers of Logres
Nourished the land on a fallacy of rational virtue."
-- Charles Williams. "Taliessin through Logres: Prelude"
Jan 2 '07 #12
John W. Kennedy wrote on 02 jan 2007 in comp.lang.javascript:
To be absolutely strict, it is "12:00 noon" and "12:00 midnight".
Neither one of the two is either AM or PM. However, a great many
mechanical and electronic time displays have problems with this, and so
"12:00 a.m." has come to be used to mean "12:00 midnight" and "12:00
p.m." to mean "12:00 noon".
.... which does not stop us in the rest of the world being amazed over.

It is completely illogical, but nicely explained, john.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jan 2 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2227
by: Gino Elloso - Philippines | last post by:
Hello, I made a webpage ( @ Geocities Free Webpages ) that contains a mousetrail clock script. I simply copied the script ( including all html tags ) exactly as it was from a source webpage (...
8
2986
by: Prometheus Research | last post by:
http://newyork.craigslist.org/eng/34043771.html We need a JavaScript component which will auto-submit a form after a set period has elapsed. The component must display a counter that dynamically...
9
3443
by: ME | last post by:
can someone tell me how to make this first one work??, located here: http://javascript.about.com/library/scripts/blshowdatetime.htm#examplesource This second one, well, dreamweaver eats the code,...
1
1594
by: s.shahzaib.ali | last post by:
I want to make analog clock skin of windows media player so how can i roteate the layer upto 360 degrees with the help of javascript please tell me iam very anxious about it. iam the first and only...
0
1097
by: Kamyk | last post by:
Hello all! I have problem with code. Firstly I have created the VBScript code which count me the time and days which left to the end of working week. I work from 8:00 am till 4 pm. I would like...
7
1855
by: ian ward | last post by:
Hello, I want to show the result of some JavaScript on an HTML page. I've had a look at some threads and it seems the conclusion is the following - javascript entities are usable on the...
7
3120
by: Daz | last post by:
Hi everyone. I am trying to find out how I can create a real time clock, which knows when to set itself backwards or forwards 1 hour. The clock will work for various timezones. Some of which...
7
4985
Frinavale
by: Frinavale | last post by:
This whole thing started when I wanted to display list items in a circle...it just looked way too much like a clock. So for fun I started to create a JavaScript Analog clock. I was wondering how...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7167
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4915
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.