JRS: In article <11**********************@m73g2000cwd.googlegroups .com>,
dated Mon, 25 Sep 2006 00:30:53 remote, seen in
news:comp.lang.javascript, Gregc. <gr*********@bigpond.composted :
>
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.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.