Time Zones

G

Gregc.

Hi

Would anyone have an example of working out international time zones,
using an array.

Greg
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sat, 23 Sep 2006 20:25:12 remote, seen in
news:comp.lang.javascript said:
Thanks but already looked on your website.

Then you should know :-

a) Not to quote signatures.

b) Enough to ask an answerable question. Note that, as of about 18:20
GMT today, no-one shows any sign of understanding which meaning your
question might have been intended to have. You've not even indicated
your location, so we don't know whether US zones are to be included.
 
R

RobG

Gregc. said:
Dr said:
A remarkably ambiguous question. Read
<URL:http://www.merlyn.demon.co.uk/js-dates.htm> ff. (incl date5)
and <URL:http://www.merlyn.demon.co.uk/misctime.htm> then reformulate.
[...]
Thanks but already looked on your website.

As John said, you need to supply a bit more information in your
question - most people won't waste time guessing possible answers to
something so vague, or to ask questions trying to find out what it is
that you want. So...

What do you want to do - send out a date and time that will be
displayed in a users' local timezone? Let the user input dates and
times and convert them to your server's local time? Convert solar time
to astronomical time?

The general strategy is to convert stuff to UTC and then turn it into
whatever local time as appropriate - usually just for display. But
that may not suit or be required in your case. More information is
required.
 
G

Gregc.

RobG said:
As John said, you need to supply a bit more information in your
question - most people won't waste time guessing possible answers to
something so vague, or to ask questions trying to find out what it is
that you want. So...

What do you want to do - send out a date and time that will be
displayed in a users' local timezone? Let the user input dates and
times and convert them to your server's local time? Convert solar time
to astronomical time?

The general strategy is to convert stuff to UTC and then turn it into
whatever local time as appropriate - usually just for display. But
that may not suit or be required in your case. More information is
required.


--


Okay, well this is what I am trying to do:

HTML>
<HEAD>
<SCRIPT LANGUAGE=javascript type="text/javascript">

var selectedCity;
var timeDiff;
var arrTimeZones = new Array();
arrTimeZones [0] = "Paris;+;1;;"
arrTimeZones [1] = "London;+;0;;"
arrTimeZones [2] = "Rome;+;1;;"
arrTimeZones [3] = "New York City;-;5;;"
arrTimeZones [4] = "Toronto;-;5;;"
arrTimeZones [5] = "Sydney;+;10;;"
arrTimeZones [6] = "Moscow;+;3;;"
arrTimeZones [7] = "Tokyo;+;9;;"
arrTimeZones [8] = "Beijing;+;8;;"
arrTimeZones [9] = "San Francisco;-;8;;"
arrTimeZones [10] = "Cairo;+;2;;"
arrTimeZones [11] = "Bangkok;+;7;;"
arrTimeZones [12] = "Lima;-;5;;"


function window_onload() {
updateTimeZone();
window.setInterval("updateTimeZone()", 1000);
}
function updateTimeZone()
{
var lstCity = document.form1.lstCity;
timeDiff = lstCity.options[lstCity.selectedIndex].value;
selectedCity = lstCity.options[lstCity.selectedIndex].text;
updateTime();




}
function getTimeString(dateObject)
{
var timeString;
var hours = dateObject.getHours();
if (hours < 10)
hours = "0" + hours;
var minutes = dateObject.getMinutes();
if (minutes < 10)
minutes = "0" + minutes;

var seconds = dateObject.getSeconds()
if (seconds < 10)
seconds = "0" + seconds;
timeString = hours + ":" + minutes + ":" + seconds;
return timeString;
}
function updateTime()
{
var nowTime = new Date();
var resultsFrame = window.top.resultsFrame.document;
resultsFrame.open()
resultsFrame.write("Local Time is " + getTimeString(nowTime) +
"<BR>");
nowTime.setMinutes(nowTime.getMinutes() +
nowTime.getTimezoneOffset() +
parseInt(timeDiff));
resultsFrame.write(selectedCity + " time is " +
getTimeString(nowTime));
resultsFrame.close();
}

</SCRIPT>
</HEAD>
<BODY LANGUAGE=JavaScript onload="window_onload()">
<FORM NAME=form1>
<SELECT SIZE=5 NAME=lstCity LANGUAGE=JavaScript
onchange="updateTimeZone(this);">

<option>Paris</option>
<option>London</option>
<option>Rome</option>
<option>New York</option>
<option>Toronto</option>
<option>Sydney</option>
<option>Moscow</option>
<option>Tokyo</option>
<option>Beijing</option>
<option>San Francisco</option>
<option>Cairo</option>
<option>Bangkok</option>
<option>Lima</option>

</select>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>

But I am having trouble trying to get it to work. Would anyone have an
idea?

Greg
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Mon, 25 Sep 2006 00:30:53 remote, seen in
news:comp.lang.javascript said:
Okay, well this is what I am trying to do:

HTML>
<HEAD>
<SCRIPT LANGUAGE=javascript type="text/javascript">
^^^^^^^^^^^^^^^^^^^ deprecated, superfluous.
var selectedCity;
var timeDiff;
var arrTimeZones = new Array();
arrTimeZones [0] = "Paris;+;1;;"
arrTimeZones [1] = "London;+;0;;"
arrTimeZones [2] = "Rome;+;1;;"
arrTimeZones [3] = "New York City;-;5;;"
arrTimeZones [4] = "Toronto;-;5;;"
arrTimeZones [5] = "Sydney;+;10;;"
arrTimeZones [6] = "Moscow;+;3;;"
arrTimeZones [7] = "Tokyo;+;9;;"
arrTimeZones [8] = "Beijing;+;8;;"
arrTimeZones [9] = "San Francisco;-;8;;"
arrTimeZones [10] = "Cairo;+;2;;"
arrTimeZones [11] = "Bangkok;+;7;;"
arrTimeZones [12] = "Lima;-;5;;"

var arrTimeZones = ["Paris;+;1;;", ..., "Lima;-;5;;"]
is simpler.
function window_onload() {
updateTimeZone();
window.setInterval("updateTimeZone()", 1000);
}

Be aware that setInterval(..., 1000) will not fire synchronously with
the seconds of the displaying computer.
function updateTimeZone()
{
var lstCity = document.form1.lstCity;
timeDiff = lstCity.options[lstCity.selectedIndex].value;
selectedCity = lstCity.options[lstCity.selectedIndex].text;
updateTime();




}
function getTimeString(dateObject)
{
var timeString;
var hours = dateObject.getHours();
if (hours < 10)
hours = "0" + hours;
var minutes = dateObject.getMinutes();
if (minutes < 10)
minutes = "0" + minutes;

var seconds = dateObject.getSeconds()
if (seconds < 10)
seconds = "0" + seconds;
timeString = hours + ":" + minutes + ":" + seconds;
return timeString;
}

Don't repeat code; use a (one-line) Leading Zero function.
function updateTime()
{
var nowTime = new Date();
var resultsFrame = window.top.resultsFrame.document;
resultsFrame.open()
resultsFrame.write("Local Time is " + getTimeString(nowTime) +
"<BR>");
nowTime.setMinutes(nowTime.getMinutes() +
nowTime.getTimezoneOffset() +
parseInt(timeDiff));

There should be no need to use parseInt; and used thus it will probably
give incorrect results for differences of 08 & 09. Use unary +.

alert(timeDiff) shows me an empty string.
resultsFrame.write(selectedCity + " time is " +
getTimeString(nowTime));
resultsFrame.close();
}

</SCRIPT>
</HEAD>
<BODY LANGUAGE=JavaScript onload="window_onload()">
<FORM NAME=form1>
<SELECT SIZE=5 NAME=lstCity LANGUAGE=JavaScript
onchange="updateTimeZone(this);">

<option>Paris</option>

<option>London</option>
<option>Rome</option>
<option>New
York</option>

<option>Toronto</option>

<option>Sydney</option>

<option>Moscow</option>
<option>Tokyo</option>

<option>Beijing</option>
<option>San
Francisco</option>
<option>Cairo</option>

<option>Bangkok</option>
<option>Lima</option>

</select>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>

But I am having trouble trying to get it to work. Would anyone have an
idea?

Explain what trouble. For me, it fails since window.top.resultsFrame.
document is not an Object; don't presume that everyone sees the same.
Maybe you need to understand the use of "open". You could instead write
to a TextArea, for simplicity.

ISTM that timeDiff is an empty string when in updateTime.

You need to learn how to debug code. Write in smaller pieces, testing
as you go. Function alert() will show the value of a variable.

I see no allowance for the Summer Time rules of each location.

If this is coursework, consider a different career.

If it represents a practical need, then js-date5.htm#Demo serves for any
location (even LHI <g>) and js-date5.htm#SLGD (was SLHD) serves for a
list of locations. While the latter input is a TextArea, the value is
immediately converted to an Array.

It's a good idea to read the newsgroup and its FAQ.
 
G

Gregc.

Dr said:
JRS: In article <[email protected]>,
dated Mon, 25 Sep 2006 00:30:53 remote, seen in
news:comp.lang.javascript said:
Okay, well this is what I am trying to do:

HTML>
<HEAD>
<SCRIPT LANGUAGE=javascript type="text/javascript">
^^^^^^^^^^^^^^^^^^^ deprecated, superfluous.
var selectedCity;
var timeDiff;
var arrTimeZones = new Array();
arrTimeZones [0] = "Paris;+;1;;"
arrTimeZones [1] = "London;+;0;;"
arrTimeZones [2] = "Rome;+;1;;"
arrTimeZones [3] = "New York City;-;5;;"
arrTimeZones [4] = "Toronto;-;5;;"
arrTimeZones [5] = "Sydney;+;10;;"
arrTimeZones [6] = "Moscow;+;3;;"
arrTimeZones [7] = "Tokyo;+;9;;"
arrTimeZones [8] = "Beijing;+;8;;"
arrTimeZones [9] = "San Francisco;-;8;;"
arrTimeZones [10] = "Cairo;+;2;;"
arrTimeZones [11] = "Bangkok;+;7;;"
arrTimeZones [12] = "Lima;-;5;;"

var arrTimeZones = ["Paris;+;1;;", ..., "Lima;-;5;;"]
is simpler.
function window_onload() {
updateTimeZone();
window.setInterval("updateTimeZone()", 1000);
}

Be aware that setInterval(..., 1000) will not fire synchronously with
the seconds of the displaying computer.
function updateTimeZone()
{
var lstCity = document.form1.lstCity;
timeDiff = lstCity.options[lstCity.selectedIndex].value;
selectedCity = lstCity.options[lstCity.selectedIndex].text;
updateTime();




}
function getTimeString(dateObject)
{
var timeString;
var hours = dateObject.getHours();
if (hours < 10)
hours = "0" + hours;
var minutes = dateObject.getMinutes();
if (minutes < 10)
minutes = "0" + minutes;

var seconds = dateObject.getSeconds()
if (seconds < 10)
seconds = "0" + seconds;
timeString = hours + ":" + minutes + ":" + seconds;
return timeString;
}

Don't repeat code; use a (one-line) Leading Zero function.
function updateTime()
{
var nowTime = new Date();
var resultsFrame = window.top.resultsFrame.document;
resultsFrame.open()
resultsFrame.write("Local Time is " + getTimeString(nowTime) +
"<BR>");
nowTime.setMinutes(nowTime.getMinutes() +
nowTime.getTimezoneOffset() +
parseInt(timeDiff));

There should be no need to use parseInt; and used thus it will probably
give incorrect results for differences of 08 & 09. Use unary +.

alert(timeDiff) shows me an empty string.
resultsFrame.write(selectedCity + " time is " +
getTimeString(nowTime));
resultsFrame.close();
}

</SCRIPT>
</HEAD>
<BODY LANGUAGE=JavaScript onload="window_onload()">
<FORM NAME=form1>
<SELECT SIZE=5 NAME=lstCity LANGUAGE=JavaScript
onchange="updateTimeZone(this);">

<option>Paris</option>

<option>London</option>
<option>Rome</option>
<option>New
York</option>

<option>Toronto</option>

<option>Sydney</option>

<option>Moscow</option>
<option>Tokyo</option>

<option>Beijing</option>
<option>San
Francisco</option>
<option>Cairo</option>

<option>Bangkok</option>
<option>Lima</option>

</select>
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>

But I am having trouble trying to get it to work. Would anyone have an
idea?

Explain what trouble. For me, it fails since window.top.resultsFrame.
document is not an Object; don't presume that everyone sees the same.
Maybe you need to understand the use of "open". You could instead write
to a TextArea, for simplicity.

ISTM that timeDiff is an empty string when in updateTime.

You need to learn how to debug code. Write in smaller pieces, testing
as you go. Function alert() will show the value of a variable.

I see no allowance for the Summer Time rules of each location.

If this is coursework, consider a different career.

If it represents a practical need, then js-date5.htm#Demo serves for any
location (even LHI <g>) and js-date5.htm#SLGD (was SLHD) serves for a
list of locations. While the latter input is a TextArea, the value is
immediately converted to an Array.

It's a good idea to read the newsgroup and its FAQ.
--
Thanks for your advice. Personally, I'm not fond of JavaScript. Don't
really appreciate the following either "If this is coursework, consider
a different career."
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Mon, 25 Sep 2006 17:38:03 remote, seen in
news:comp.lang.javascript said:
Lines: 161
Dr John Stockton wrote:
Thanks for your advice. Personally, I'm not fond of JavaScript. Don't
really appreciate the following either "If this is coursework, consider
a different career."

You have repeatedly been advised to read the FAQ; if you had done so,
you would have known not to over-quote.

You'll be better off in a career matching your intellectual capability.
 
E

Evertjan.

Dr John Stockton wrote on 26 Sep 2006 in comp.lang.javascript:
You'll be better off in a career matching your intellectual capability.

Wow, John, that is just an assumption.

I could put to you some incompatible assumptions,
applicable to different individuals:

A career below that capability could fuel and make time for intellectual
hobbyism, that could enrich life and be a joy when your working days are
over. [This could possibly apply to the majority on this NG?]

A career over that capability could enlarge that capability,
improving one's self esteem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top