Javascript Time in London

Hello all :slight_smile:

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>
  
  

Thanks in advacnce for any help :slight_smile:

Hi,

As javascript is a browser-side scripting language, it will get the time from the users clock, and so it will not be London time.

Regards,

Rob.

Any other suggestions as to a solution?

If you have access to a server-side scripting language, such as PHP, it might be possible, although I can’t think exactly how it would be done.

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.

niooi

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?

…thanks for the help so far :slight_smile:

from this line:: var nhours=thetime.getHours();

change accordinbly thetime.getHours(); to what ever you are sending the time (cookie, or whatever)

If you set the time through PHP as a META Tag such as:
<META LONDON=“HOUR” Content=“18”> //ONLY THE HOUR

and then in javascript you change the above javasscript line to:
var nhours= whatever can take the value

regex, document.meta.xxx…

I’m sure it is possible maybe somebody can help u out further.

Niooi

niooi, thanks very much for the input! im on my way out at the mo, but I will give it a shot as soon as I get back in, thanks very much again!!

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. :slight_smile: 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?

Any one?

nvm…done it

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 +"&nbsp;&nbsp;&nbsp;" + ((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>



This code is working in Firefox and Konqueror.


<!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">
// http://www.sitepoint.com/forums/showthread.php?t=206847

window.onload=init;

function init() 
{
clock("NY",-4,"nyc");
clock("LON",0,"london");

setTimeout("init()",1000);
}

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 
  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 +"&nbsp;&nbsp;&nbsp;" + ((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"
return document.getElementById(elementid).innerHTML="<b>"+prefix+"</b>: "+curTime;

} 

</script>
</head>

<body>
<div id='nyc'></div>
<div id='london'></div>
</body>
</html>
  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=UTF-8"
 http-equiv="content-type">
  <title></title>

<script type="text/javascript">
// http://www.sitepoint.com/forums/showthread.php?t=206847
// http://www.sitepoint.com/forums/showthread.php?t=224325&highlight=getUTCDate

Date.prototype.yerelsaat=function(n){
this.setUTCHours(this.getUTCHours()+n);
return this.toUTCString().substring(0,25); 
}

function saatGoster(){

var londra=document.getElementById('londra');
var istanbul=document.getElementById('istanbul');
var nevyork=document.getElementById('nevyork');
londra.innerHTML= new Date().yerelsaat(0);
istanbul.innerHTML= new Date().yerelsaat(+2);
nevyork.innerHTML= new Date().yerelsaat(-5);

setTimeout("saatGoster()",1000);

}

window.onload=saatGoster;
</script>

</head>
<body>
<div>Londra: <span id="londra"></span></div>
<div>&#304;stanbul: <span id="istanbul"></span></div>
<div>Nevyork: <span id="nevyork"></span></div>
</body>
</html>

  
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html; charset=UTF-8"
 http-equiv="content-type">
  <title></title>

<script type="text/javascript">
// http://www.sitepoint.com/forums/showthread.php?t=206847
// http://www.sitepoint.com/forums/showthread.php?t=224325&highlight=getUTCDate



Date.prototype.yerel_saat=function(n){
var t= this.getTime() + (this.getTimezoneOffset() * 60000) + n *3600000;
var d= new Date(t);
return d.toLocaleString().substring(0,25);
}

function saatGoster(){

var londra=document.getElementById('londra');
var istanbul=document.getElementById('istanbul');
var nevyork=document.getElementById('nevyork');
londra.innerHTML= new Date().yerel_saat(0);
istanbul.innerHTML= new Date().yerel_saat(+2);
nevyork.innerHTML= new Date().yerel_saat(-5);

setTimeout("saatGoster()",1000);

}

window.onload=saatGoster;
</script>

</head>
<body>
<div>Londra: <span id="londra"></span></div>
<div>&#304;stanbul: <span id="istanbul"></span></div>
<div>Nevyork: <span id="nevyork"></span></div>
</body>
</html>