Click to See Complete Forum and Search --> : clock - setting the time zone?
helsinkihammer
10-27-2003, 04:49 AM
I know very little about javascript, but I've found a nice piece of code for putting a written clock on a webpage. In the code is one line that says if the time isn't set, then use system default. I want to set the time to GMT +2 hours. Any ideas?
The idea is to always give the Helsinki time on the webpage. Here is the code:
function number(x) {
if (x==1) return "one"; if (x==2) return "two"; if (x==3) return "three";
if (x==4) return "four"; if (x==5) return "five"; if (x==6) return "six";
if (x==7) return "seven"; if (x==8) return "eight"; if (x==9) return "nine";
if (x==10) return "ten"; if (x==11) return "eleven"; if (x==12) return "twelve";
return x; //default
}
function ishtime(h,m) {
h = number(h)
if (m<=3 || m>57) return h+" o'clock";
if (m<=7) return "five past "+h;
if (m<=12) return "ten past "+h;
if (m<=17) return "quarter past "+h;
if (m<=23) return "twenty past "+h;
if (m<=28) return "twenty-five past "+h;
if (m<=33) return "half past "+h;
if (m<=38) return "twenty-five to "+h;
if (m<=43) return "twenty to "+h;
if (m<=48) return "quarter to "+h;
if (m<=53) return "ten to "+h;
if (m<=58) return "five to "+h;
return "h:m"; // never reached?
}
function daytime(h) {
if (!h || h>21) return " at night"
if (h<12) return " in the morning";
if (h<=17) return " in the afternoon";
return " in the evening"; // default
}
function ish(h,m) {
if (!h && !m) { // if no time supplied, use the system time
time = new Date()
h = time.getHours()
m = time.getMinutes()
}
z = daytime(h);
h = h % 12 // fix to 12 hour clock
if (m>57 && time.getSeconds()>30) m++; // round seconds
if (m>60) m=0 // round up minutes
if (m>33) h++ // round up hours
if (h>12) h = 1 // the clock turns round..
if (h==0) h = 12
return "It's now about "+ishtime(h,m)+z+"."
}
// End -->
</script>
Thanks in advance - Mark
Charles
10-27-2003, 05:23 AM
<script type="text/javascript">
<!--
Date.ONE_SECOND = 1000;
Date.ONE_MINUTE = Date.ONE_SECOND * 60;
Date.ONE_HOUR = Date.ONE_MINUTE * 60;
Date.prototype.toUTCOffset = function (offset) {return new Date(this.getTime() + this.getTimezoneOffset() * 60000 + offset * Date.ONE_HOUR)}
Date.prototype.toTimeString = function () {
var hour = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'][this.getHours() % 12];
hour = hour ? hour : this.getHours() % 12;
var m = this.getMinutes();
if (m<=3 || m>57) {hour = hour + ' o\'clock'}
else if (m<=7) {hour = 'five past '+ hour}
else if (m<=12) {hour = 'ten past '+ hour}
else if (m<=17) {hour = 'quarter past '+ hour}
else if (m<=23) {hour = 'twenty past '+ hour}
else if (m<=28) {hour = 'twenty-five past '+ hour}
else if (m<=33) {hour = 'half past '+ hour}
else if (m<=38) {hour = 'twenty-five to '+ hour}
else if (m<=43) {hour = 'twenty to '+ hour}
else if (m<=48) {hour = 'quarter to '+ hour}
else if (m<=53) {hour = 'ten to '+ hour}
else if (m<=58) {hour = 'five to '+ hour}
else {hour = this.toTimeString()};
if (this.getHours() < 12) {hour += ' in the morning'}
else if (this.getHours() <= 17) {hour += ' in the afternoon'}
else hour += ' at night';
return hour;
}
document.write('<p>-2 GMT, it\'s about <span id="time">', new Date().toUTCOffset(-2).toTimeString(), '</span></p>');
if (document.getElementById) setInterval("document.getElementById('time').firstChild.data = new Date().toUTCOffset(-2).toTimeString()", 0.2 * Date.ONE_SECOND);
// -->
</script>
helsinkihammer
10-27-2003, 05:53 AM
Thanks for replying so quickly Charles.
I tried running the code, but it's come up with errors. Put the code in the head and the body, neither worked, I tried the write.document in the body, the rest in the head, but also neither worked.
I tried it on a blank html page, so as no conflicts, but no joy.
Am I missing something obvious?
This is the original code in it's entirety, including the write.document.
In the original, the write.document is going into the body, is this not the same with the modified code for adding a gmt +2?
<html>
<head>
<title>JavaFILE</title>
<script LANGUAGE="JavaScript">
function number(x) {
if (x==1) return "one"; if (x==2) return "two"; if (x==3) return "three";
if (x==4) return "four"; if (x==5) return "five"; if (x==6) return "six";
if (x==7) return "seven"; if (x==8) return "eight"; if (x==9) return "nine";
if (x==10) return "ten"; if (x==11) return "eleven"; if (x==12) return "twelve";
return x; //default
}
function ishtime(h,m) {
h = number(h)
if (m<=3 || m>57) return h+" o'clock";
if (m<=7) return "five past "+h;
if (m<=12) return "ten past "+h;
if (m<=17) return "quarter past "+h;
if (m<=23) return "twenty past "+h;
if (m<=28) return "twenty-five past "+h;
if (m<=33) return "half past "+h;
if (m<=38) return "twenty-five to "+h;
if (m<=43) return "twenty to "+h;
if (m<=48) return "quarter to "+h;
if (m<=53) return "ten to "+h;
if (m<=58) return "five to "+h;
return "h:m"; // never reached?
}
function daytime(h) {
if (!h || h>21) return " at night"
if (h<12) return " in the morning";
if (h<=17) return " in the afternoon";
return " in the evening"; // default
}
function ish(h,m) {
if (!h && !m) { // if no time supplied, use the system time
time = new Date()
h = time.getHours()
m = time.getMinutes()
}
z = daytime(h);
h = h % 12 // fix to 12 hour clock
if (m>57 && time.getSeconds()>30) m++; // round seconds
if (m>60) m=0 // round up minutes
if (m>33) h++ // round up hours
if (h>12) h = 1 // the clock turns round..
if (h==0) h = 12
return "It's now about "+ishtime(h,m)+z+"."
}
// End -->
</script>
<base target="leftframe">
</head>
<body BGCOLOR="#ffffff" link="#CC0033" vlink="#333399" alink="#FF0000" <!--content start-->
<table WIDTH="96%" BORDER="0" CELLSPACING="5" CELLPADDING="5">
<tr>
<td WIDTH="100%"><p align="center">What is the time in Helsinki? <font FACE="ARIEL,HELVETICA" SIZE="-1"></p>
<p align="center"><script language="JavaScript">
<!--HIDE FROM OLD BROWSERS--
document.writeln(ish().fontcolor("black"),"<P>")
<!--DONE-->
</script> </font></td>
</tr>
</table>
<!--content stop-->
</body>
</html>
Again, many thanks for your quick reply Charles
Charles
10-27-2003, 06:02 AM
Give this one a try then.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<script type="text/javascript">
<!--
Date.ONE_SECOND = 1000;
Date.ONE_MINUTE = Date.ONE_SECOND * 60;
Date.ONE_HOUR = Date.ONE_MINUTE * 60;
Date.prototype.toUTCOffset = function (offset) {return new Date(this.getTime() + this.getTimezoneOffset() * 60000 + offset * Date.ONE_HOUR)}
Date.prototype.toTimeString = function () {
var h = this.getHours();
var m = this.getMinutes();
var s = this.getSeconds();
if (s > 30) {m++; m = m % 60}
if (m > 30) {h++; h = h% 60}
var st = ['', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve'][h % 12];
st = st ? st : h % 12;
if (m == 60) {return st + ' o\'clock'}
return [['', 'five past', 'ten past', 'quarter past', 'twenty past', 'twenty-five past', 'half past', 'twenty-five of', 'twenty of', 'ten of', 'five of'][Math.round(m / 5)], st].join(' ');
}
if (document.getElementById) onload = function () {setInterval("document.getElementById('time').firstChild.data = new Date().toTimeString()", 0.2 * Date.ONE_SECOND)}
// -->
</script>
<h1>Example</h1>
<script type="text/javascript">
<!--
document.write('<p>-2 GMT, it\'s about <span id="time">', new Date().toTimeString(), '</span></p>');
// -->
</script>
helsinkihammer
10-28-2003, 08:55 AM
Once again, thanx to Charles for putting the time in to try to solve this one.
Unfortunately, the new code didn't seem to work either. However, I've found some code that does allow you to put your home country time on your site, irrespective of the system time of the person surfing your page.
It's not as nifty in that it doesn't tell the time with words, but it's nice all the same, and you can see it working at:
http://angelfire.com/crazy/prusskij
The code is located here:
http://javascript.internet.com/clocks/date-time-long.html
The only thing to change is time zone and place
var zone = 6;
I had to play with the number before I found -2 coincided with Finnish time GMT (UTC) + 2