1

I'm trying to get three different dynamic timezone clocks on my site. i've got the following js code which i found on this site (saved as myClocks.js and included on the header of my html site):

var clock1 = new Date();
var clock2 = new Date();
var clock3 = new Date();
clock2.setHours(clock2.getHours() + 3);
clock3.setHours(clock3.getHours() - 5);
clock1.getUTCHours();
clock1.getUTCMinutes();
clock1.getUTCSeconds();
clock2.getUTCHours();
clock2.getUTCMinutes();
clock2.getUTCSeconds();
clock3.getUTCHours();
clock3.getUTCMinutes();
clock3.getUTCSeconds();

How do I code the "display" to show it anywhere I want on my HTML page? For example as an id called clocks, to look like the following:

New York: 02:12:02 Paris: 17:01:24 Moscow: 22:23:42

Many thanks in advance.

3
  • you should learn DOM manipulation. for a start, you can just do document.write(clock1); Commented Jul 20, 2013 at 10:04
  • Bear in mind that UTC doesn't take daylight saving time changes into account Commented Jul 20, 2013 at 10:14
  • I'm overwhelmed with the fast responses and how helpful everyone is arround here. However what i am really looking for is for the world clocks to be dynamic and not static. Is there any way to do this? Commented Jul 20, 2013 at 10:24

3 Answers 3

1
<html><head></head><body>
<script language="javascript">
ourDate = new Date();
document.write("The time and date at your computer's location is: "
+ ourDate.toLocaleString() 
+ ".<br/>");
document.write("The time zone offset between local time and GMT is " 
+ ourDate.getTimezoneOffset() 
+ " minutes.<br/>");
document.write("The time and date (GMT) is: " 
+ ourDate.toGMTString() 
+ ".<br/>");
</script>
</body></html>
Sign up to request clarification or add additional context in comments.

Comments

0

innerHTML is what you need. Try something like:

window.onload = function(){ // It is important to wait till DOM is ready!
   var clocks_str = clock3.getUTCHours()+" "+ clock3.getUTCMinutes()+" "+clock3.getUTCSeconds();
   document.getElementById("clocks").innerHTML = clocks_str ;
}

And if you want it dynamic , use setInterval method , like this:

 var clocks_interval;
 var clocks_box;

 window.onload = startClocks;

 function startClocks(){
    clocks_box = document.getElementById("clocks");
    clocks_interval = setInterval(updateClocks , 1000); // 1000 means 1 second
 }

 function updateClocks (){
    var clocks_str = clock3.getUTCHours()+" "+ clock3.getUTCMinutes()+" "+clock3.getUTCSeconds();
    clocks_box.innerHTML = clocks_str ;
 }

1 Comment

thanks for the help. is there any way to avoid making the clocks static? (no page refresh required to see time changing)
0

You can create a div or other HTML and use "innerHTML".

document.getElementById("clocks").innerHTML = clock1.getUTCHours();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.