I have found a script on the net (I know its bad) that I have managed to modify according to what I need, it initially used a input box which I changed to innerHTML to meet my needs.
Anyways, the problem is, that I dont know if it is displaying the time in london everywhere (which I dont think it is) or if its taking the time from your country, basically what I need to is display the time in London.
here is the code
<body onload="startclock()">
<script language="javascript">
<!--
function startclock()
{
var thetime=new Date();
var nhours=thetime.getHours();
var nmins=thetime.getMinutes();
var nsecn=thetime.getSeconds();
var AorP=" ";
if (nhours>=12)
AorP="pm";
else
AorP="am";
if (nhours>=13)
nhours-=12;
if (nhours==0)
nhours=12;
if (nsecn<10)
nsecn="0"+nsecn;
if (nmins<10)
nmins="0"+nmins;
document.getElementById('clock').innerHTML="Time in London: "+nhours+":"+nmins+":"+nsecn+" "+AorP;
setTimeout('startclock()',1000);
}
//-->
</script>
<div id="clock">Time in London: Loading...</div>
</body>
Initiate the time through a server site language such as PHP: as a cookie, through url, a txt file, and even maybe through a META tag (just a thought) etc…
I don’t know if the META solution could work but it could be the nicest solution. Set a META such as <META LONDON=“18:30”> and retrieve it from javascript.
yup, that’s not a problem at all can get the time from the server using PHP that’s simple, the problem is how to convert it and get it to start ticking from that time I get from the server?
Anyways, the problem is, that I dont know if it is displaying the time in london everywhere (which I dont think it is) or if its taking the time from your country, basically what I need to is display the time in London.
That’s why they invented GMT time. This line:
var thetime=new Date();
gets the time set on the user’s computer. On windows(probably something similar for other OS’es), by right clicking the clock, you can set the date and time on your computer for any country and any time zone within a country. So, you could see for yourself what your script produces for different time zones.
<script type="text/javascript">
window.onload=startclock;
function startclock()
{
var thetime=new Date();
var nhours=thetime.get[color="red"]UTC[/color]Hours();
var nmins=thetime.get[color="red"]UTC[/color]Minutes();
var nsecn=thetime.get[color="red"]UTC[/color]Seconds();
var AorP=" ";
if (nhours>=12) AorP="pm";
else AorP="am";
if (nhours>=13) nhours-=12;
if (nhours==0) nhours=12;
if (nsecn<10) nsecn="0"+nsecn;
if (nmins<10) nmins="0"+nmins;
document.getElementById('clock').innerHTML="Time in London: "+nhours+":"+nmins+":"+nsecn+" "+AorP;
setTimeout('startclock()',1000);
}
</script>
[color="red"]</head>[/color]
Note: this script would go right before the </head> tag.
UTC time was previously known as GMT time(Greenwich Mean Time). 0:00 UTC time is midnight in Greenwich. I imagine you are on the same time as Greenwich?
The functions: getUTCHours, etc. automatically convert the time obtained from the user’s computer to UTC time. If your time zone was different than the UTC time, then you could add the difference to the UTC time to display your local time.
Hi there…Just found this thread and was wondering if there is a way to use this code to get the time in london (already done) and the time in newyork to display at the same time indifferent divs?
does anyone know why the ticker part is not refreshing (setTimeout) ??
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
window.onload=init;
function init()
{
clock("NY",-4,"nyc");
clock("LON",0,"london");
}
function clock(prefix, offset, elementid)
{
// Copyright 1999, 2000 by Ray Stott
// OK to use if this copyright is included
// Script available at http://www.crays.com/jsc
var TimezoneOffset = offset // adjust for time zone
var localTime = new Date()
var ms = localTime.getTime()
+ (localTime.getTimezoneOffset() * 60000)
+ TimezoneOffset * 3600000
var time = new Date(ms)
var date = time.getDate()
var month = time.getMonth()+1
var hour = time.getHours()
var minute = time.getMinutes()
var second = time.getSeconds()
var curTime = date +"." + month +" " + ((hour > 12) ? hour - 12 : hour)
if(hour==0) curTime = "12"
curTime += ((minute < 10) ? ":0" : ":") + minute
curTime += ((second < 10) ? ":0" : ":") + second
curTime += (hour >= 12) ? " PM" : " AM"
document.getElementById(elementid).innerHTML="<b>"+prefix+"</b>: "+curTime;
//-->
setTimeout('clock(prefix, offset,elementid)',1000);
}
</script>
</head>
<body>
<div id='nyc'></div>
<div id='london'></div>
</body>
</html>