Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » Debugging Javascript Application Using Date Method
Closed Thread
Old 03-26-2007, 04:22 PM   #1 (permalink)
 
Ultra Techie

Join Date: Mar 2004

Posts: 573

Cunjo is on a distinguished road

Send a message via ICQ to Cunjo Send a message via Yahoo to Cunjo
Default Debugging Javascript Application Using Date Method

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....
__________________
\"I\'ll do it cheap, I\'ll do it fast, or I\'ll do it well. You can only choose two.\"

Last edited by Cunjo; 03-26-2007 at 04:32 PM. Reason: update
Cunjo is offline  
Old 04-17-2007, 11:07 PM   #2 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,410

office politics will become famous soon enough

Default Re: Debugging Javascript Application Using Date Method

i choose well and cheap, lol
office politics is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On