NOTE: This Problem has been resolved - scroll to the bottom to view the solution
I'm trying to get my website to display a GMT clock written in Javascript. The clock works perfectly... except at certain times of day. I'm not sure if there are other times that it has issues, but I know that between 3 and 4 PM localtime (1900 and 2000 GMT) the time in hours and minutes displays improperly as a two-digit integer of neither minutes nor hours. At 2000 GMT, the clock resumes normal operation, and so is very difficult to debug (I'm currently working on a computer without root access to override the system time).
Below is the code, and a link to a page where you can see it in action:
The Javascript
:
Code:
<script type="text/javascript">
<!--
function GMTclock(){
var time = new Date()
var gmtMS = time.getTime()
+ (time.getTimezoneOffset() * 60000)
var gmtTime = new Date(gmtMS)
var hr = gmtTime.getHours()
var min = gmtTime.getMinutes()
var sec = gmtTime.getSeconds()
var day = gmtTime.getDay()
var date = gmtTime.getDate()
var month = gmtTime.getMonth()
var year = gmtTime.getFullYear() + 2428
if(hr < 10){
hr = "0" + hr
}
if(min < 10){
min = "0" + min
}
if(sec < 10){
sec = "0" + sec
}
if(month == 0){
month = "Jan"
}
if(month == 1){
month = "Feb"
}
if(month == 2){
month = "Mar"
}
if(month == 3){
month = "Apr"
}
if(month == 4){
month = "May"
}
if(month == 5){
month = "Jun"
}
if(month == 6){
month = "Jul"
}
if(month == 7){
month = "Aug"
}
if(month == 8){
month = "Sep"
}
if(month == 9){
month = "Oct"
}
if(month == 10){
month = "Nov"
}
if(month == 11){
month = "Dec"
}
if(day == 0){
day = "Sunday"
}
if(day == 1){
day = "Monday"
}
if(day == 2){
day = "Tuesday"
}
if(day == 3){
day = "Wednesday"
}
if(day == 4){
day = "Thursday"
}
if(day == 5){
day = "Friday"
}
if(day == 6){
day = "Saturday"
}
/*
document.gmt.digits.value= day + ", " + date + "-" + month + "-" + year + " " + hr + min + "." + sec + " Zulu"
*/
document.gmt.day.value= day + ", "
document.gmt.date.value= date + "-" + month + "-" + year + " "
document.gmt.time.value= hr + min + "." + sec + " Zulu "
setTimeout("GMTclock()", 1000)
}
//-->
</script>
<link rel="stylesheet" type="text/css" href="styles.css" />
Display HTML:
Code:
<body onload="GMTclock()">
<form name="gmt" style="position:relative;left:100px">
<input type="text" name="day" background="#000000" class="clock" size="12" value="Loading" /><br />
<input type="text" name="date" background="#000000" class="clock" size="12" value="Loading" /><br />
<input type="text" name="time" background="#000000" class="clock" size="12" value="Loading" />
</form>
</body>
The clock prototype:
http://scarvo.net/time.htm
EDIT: I just looked again, and it seems it's gone back to acting oddly again (currently 2024 GMT, but displaying as "44")
EDIT 2: I figured out the problem, but not the reason for the changes in behavior. It was interpreting
hour + min by adding the two values together rather than appending. changing it to
hour + "" + min fixed the problem (I think), but it doesn't really explain why at some times of day it acted differently than others....