Hello:
I'm a newbie to javascript and am trying to get a file to correctly display the time for different time zones. Unfortunately, the time is off by one hour. I have attached 2 files. Thank you.
The 1st file is:
The 2nd file is:
I'm a newbie to javascript and am trying to get a file to correctly display the time for different time zones. Unfortunately, the time is off by one hour. I have attached 2 files. Thank you.
The 1st file is:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>Time Zones</title>
<script type="text/javascript" src="JavaScript_Ch11_script04_Page267.js"></script>
</head>
<body bgcolor="#FFFFFF">
<!-- Filename: C:\BLUE\JavaScript_Ch11_script04_Page266.html -->
<h3>Our office hours are 9:00 am to 5:00 pm, Monday through Friday, at each of our locations. It is now</h3>
<ul>
<li><span class="tz-8"></span> in San Francisco</li>
<li><span class="tz-5"></span> in New York</li>
<li><span class="tz-0"></span> in London</li>
<li><span class="tz+7"></span> in Hong Kong</li>
</ul>
</body>
</html>
The 2nd file is:
Code:
window.onload = initDate;
function initDate() {
var allTags = document.getElementsByTagName("*");
for (var i=0; i<allTags.length; i++) {
if (allTags[i].className.indexOf("tz")==0) {
showTheTime(allTags[i],allTags[i].className.substring(2));
}
}
}
// Filename is C:\BLUE\JavaScript_Ch11_script04_Page267.js
function showTheTime(currElem,tzOffset) {
var dayName = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var thatTZ = new Date();
var dateStr = thatTZ.toUTCString();
dateStr = dateStr.substr(0,dateStr.length - 3);
thatTZ.setTime(Date.parse(dateStr));
thatTZ.setHours(thatTZ.getHours() + parseInt(tzOffset));
currElem.innerHTML = showTheHours(thatTZ.getHours()) + showZeroFilled(thatTZ.getMinutes()) + showAmPm(thatTZ.getHours()) + dayName[thatTZ.getDay()];
function showTheHours(theHour) {
if (theHour == 0) {
return 12;
}
if (theHour < 13) {
return theHour;
}
return theHour-12;
}
function showZeroFilled(inValue) {
if (inValue > 9) {
return ":" + inValue;
}
return ":0" + inValue;
}
function showAmPm(thatTime) {
if (thatTime < 12) {
return " AM ";
}
return " PM ";
}
}