View Full Version : Clock Help
cateraction
02-01-2003, 02:40 PM
No I need a live clock that is set to a timezone such as central or pacific.
piglet
02-01-2003, 02:58 PM
With Javascript all new Date()s are based on the client's computer clock settings.
You could try this script (http://www.btinternet.com/~kurt.grigg/javascript/worldclock.html) .
cateraction
02-01-2003, 03:13 PM
Are you sure you can't do anything about it?
piglet
02-01-2003, 03:17 PM
You could easily do something about it with server-side scripting - you then can set the time in your javascript clock based on the time on your server as you build up the page to send your visitor. The clock on your server would be fixed to one time-zone so you could easily work out what offset you needed for other timezones. The problem for a javascript clock is that you don't know your visitors computer's clock is right (or what world timezone). Mine certainly isn't right!
cateraction
02-01-2003, 03:19 PM
How would you do that?
Like the code.
whammy
02-01-2003, 04:23 PM
I have a script which will simulate a "server time" by getting the GMT time of the client's machine and subtracting whatever number... but I don't have it handy I'll have to get it to you monday
Quiet Storm
02-01-2003, 10:49 PM
<HEAD>
<script language="JavaScript">
var nav=navigator.userAgent.toLowerCase();
var isopera=false;
if (nav.indexOf('opera')!=-1) isopera=true;
var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
function getthedate(){
var mydate=new Date()
var year=mydate.getFullYear()
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10) daym="0"+daym
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
var tz=Math.round(mydate.getTimezoneOffset()/60)
var Timezone=-3 //Change this number for your time zone (-3 for Santiago, Chile)
tz+=Timezone
var hourss=hours+tz;
hourss>=24?hourss-=24:hourss;
hourss<=0?hourss+=24:hourss;
if (hourss==24) hourss=0
var dn="am"
var dns="am"
if (hours>=12)
{
dn="pm"
hours-=12
}
if (hours==0) hours=12
if (hourss>=12)
{
dns="pm"
hourss-=12
}
if ((daym == "1") || (daym == "21") || (daym == "31")){ DayType = "st" }
else if ((daym == "2") || (daym == "22")){ DayType = "nd" }
else if ((daym == "3") || (daym == "23")){ DayType = "rd" }
else (DayType = "th")
if (hourss==0) hourss=12
if (minutes<=9) minutes="0"+minutes
if (seconds<=9) seconds="0"+seconds
if (mydate.getDay()!=0)
cdate="Santiago Time: "+hourss+":"+minutes+" "+dns
else
cdate="Santiago Time: "+hourss+":"+minutes+" "+dns
if (document.all&&isopera!=true)
{
document.all.clock.style.position='relative'
document.all.clock.innerHTML=cdate
}
else if (document.layers)
{document.clock.visibility='show'
document.clock.document.open();
document.clock.document.write('<center>'+cdate+'</center>');
document.clock.document.close();}
else if (document.getElementById&&isopera!=true)
{
document.getElementById("clock").innerHTML=cdate
}
else document.write('<center>'+cdate+'</center>')
}
if (!document.layers&&!document.all&&!document.getElementById) getthedate()
else if (isopera!=true) setInterval("getthedate()",1000)
else getthedate();
</script>
</HEAD>
<BODY>
<div id="clock"></div>
...
...
...
</BODY>
whammy
02-01-2003, 11:50 PM
Cool!
piglet
02-03-2003, 01:52 PM
And if the visitor's clock is wrong..........?
Roelf
02-03-2003, 02:21 PM
then, in this line:
var mydate=new Date()
insert a serverside date-object, like:
var mydate=new Date(<%Now()%>)
get that out of the function., in every function call, add one second to the dateobject like
mydate.setSeconds(mydate.getSeconds + 1);
then the new display can be created.
this assuming the clients seconds take the same time to elapse as the servers seconds :D
brothercake
02-03-2003, 02:37 PM
Originally posted by Roelf
this assuming the clients seconds take the same time to elapse as the servers seconds :D
But they won't ... a one-second loop will never take exactly one second to process - it depends on what else is in the CPU queue.
If you want a truly accurate clock, you have to query the system time fresh with each iteration - this (http://www.brothercake.com/scripts/stopwatch.php) isn't what you want, but it demonstrates the accuracy principle
Roelf
02-03-2003, 02:48 PM
then, in an init function, establish the difference between the server time and the client time. take that in mind, every time you read the client time. add or subtract the right amount of time, then you have all bases covered
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.