Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Old 11-01-2008, 01:39 AM   PM User | #1
r243
New to the CF scene

 
Join Date: Nov 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
r243 is an unknown quantity at this point
Question JS Time difference problem

Hi guys,
I'm developing a browser-based game which requires a javascript countdown.
I have one that works perfectly almost all the time, but I and several players have noticed times when it fails.

I think I have isolated the problem to midnight shifting. I have the following code, which generates the time difference between two dates in seconds:

Code:
date1 = (new Date(8, 10, 31, 20, 55, 0)).getTime();
date2 = (new Date(8, 10, 31, 23, 59, 0)).getTime();

Time_Left = Math.round((date2 - date1) / 1000);
 
alert(date2 + " - " + date1 + " = " + Time_Left);
This works fine. However, if I change date1 and date2 to i.e.
Code:
date1 = (new Date(8, 10, 31, 23, 55, 25)).getTime();
date2 = (new Date(8, 11, 1, 23, 59, 0)).getTime();
Time_Left will be a negative number even though it should be positive. I'm sure it's something really simple, but I can't see what. Anyone spot the problem?

Thanks
r243 is offline   Reply With Quote
Old 11-01-2008, 02:44 AM   PM User | #2
glenngv
Master Coder


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 9,818
Thanks: 0
Thanked 48 Times in 47 Posts
glenngv will become famous soon enough
Returns positive for me => 215000

It shouldn't be a problem as getTime() returns the number of milliseconds since midnight of 01/01/1970
glenngv is offline   Reply With Quote
Old 11-01-2008, 09:40 AM   PM User | #3
Philip M
Master Coder

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 7,670
Thanks: 94
Thanked 917 Times in 898 Posts
Philip M will become famous soon enoughPhilip M will become famous soon enough
The year in the new Date() object should be 2008, not 8 which is interpreted as 1908, giving negative numbers of milliseconds.

But:-

date1 = (new Date(2008, 10, 31, 23, 55, 25)).getTime();
is invalid as month 10 (November) does not have 31 days. Months in Javascript are 0-11. Month 10 Day 31 is treated as Month 11 (December) Day 1 which explains why

date1 = (new Date(8, 10, 31, 20, 55, 0)).getTime();
date2 = (new Date(8, 10, 31, 23, 59, 0)).getTime();

returns a (negative!) value, but not the desired value.


You can check the validity of a date i.e. that the date exists (which in your case is strongly recommended) as follows:-

Code:
<script type = "text/javascript">

function checkValidDate() {
var yr = "2008";
var mm = "10";   //  mm = 0 - 11
var dd = "31"
var nd = new Date();
nd.setFullYear(yr,mm,dd);  // YYYY,MM(0-11),DD
var ndmm = nd.getMonth();
if (ndmm != mm) {
alert (dd + "/" + mm + "/" + yr  + " is an Invalid Date!");
return false; 
}
}

</script>
This checks to see if Javascript has adjusted the month value, and if so the date is not valid.


111,111,111 x 111,111,111 = 12,345,678,987,654,321

Last edited by Philip M; 11-01-2008 at 10:54 AM.. Reason: Add date checking code
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
oesxyl (11-01-2008)
Old 11-01-2008, 09:32 PM   PM User | #4
r243
New to the CF scene

 
Join Date: Nov 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
r243 is an unknown quantity at this point
Wonderful, thank you Philip!
The code example I posted before works now. Unfortunatley, it seems that my full script already compensates for this, and the year. (i didn't write the script myself, only modified it for my needs)

The dates are dynamically generated in asp. If target month is wrong then the time difference always would be wrong, right? And it works almost all the time.
This is my script:

Code:
function countdown_clock(year, month, day, hour, minute, second, format, divid)
	 {
		 html_code = '<div id="' + divid + '"></div>';
		 document.write(html_code);

		 Today = new Date();
		 Todays_Year = Today.getYear() - 2000;
		 Todays_Month = Today.getMonth() + 1;

		 <%
		 time_of_day = time
		 time_of_day = TimeValue(time_of_day)

		 today = date
		 today = DateValue(today)

		 current_second = second(time_of_day)
		 current_minute = minute(time_of_day)
		 current_hour = hour(time_of_day)
		 current_day = day(today)
		 current_month = month(today)    
		 current_year = year(today)                                             
		 %>

		 //Computes the time difference between the client computer and the server.
		 Server_Date = (new Date(<%= current_year - 2000 %>, <%= current_month %>, <%= current_day %>,
								 <%= current_hour %>, <%= current_minute %>, <%= current_second %>)).getTime();
		 Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(),
								 Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();

		 countdown(year, month, day, hour, minute, second, (Todays_Date - Server_Date), format, divid);                
	 }

function countdown(year, month, day, hour, minute, second, time_difference, format, divid)
	 {
		 Today = new Date();
		 Todays_Year = Today.getYear() - 2000;
		 Todays_Month = Today.getMonth() + 1;

		 //Convert today's date and the target date into miliseconds.

		 Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(), Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();
		 Target_Date = (new Date(year, month, day, hour, minute, second)).getTime();

		 //Find their difference, and convert that into seconds.
		 //Taking into account the time differential between the client computer and the server.
		   Time_Left = Math.round((Target_Date - Todays_Date + time_difference) / 1000);

		 if(Time_Left < 0)
			Time_Left = 0;

		//Output: HH:MM:SS
		hours = Math.floor(Time_Left / (60 * 60));
		Time_Left %= (60 * 60);
		minutes = Math.floor(Time_Left / 60);
		Time_Left %= 60;
		seconds = Time_Left;
		days=0;
		//No single numbers
		if(hours < 10) hours ='0' + hours;
		if(minutes < 10) minutes ='0' + minutes;
		if(seconds < 10) seconds ='0' + seconds;

		document.getElementById(divid).innerHTML = '';
		document.getElementById(divid).innerHTML += hours + ':';
		document.getElementById(divid).innerHTML += minutes + ':';
		document.getElementById(divid).innerHTML += seconds;

		 //Recursive call, keeps the clock ticking.
		 setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' + second + ',' + time_difference + ', ' + format + ', ' + divid + ');', 1000);
	 }
The only dates I know for sure didn't work were:

Todays_Date: 08, 10, 31, 23, 55, 0
Target_Date: 08, 11, 1, 0, 5, 0

Somehow this returned a negative number. I would be most grateful if you could pinpoint the error.

thank you again,
henrik
r243 is offline   Reply With Quote
Old 11-01-2008, 10:05 PM   PM User | #5
Philip M
Master Coder

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 7,670
Thanks: 94
Thanked 917 Times in 898 Posts
Philip M will become famous soon enoughPhilip M will become famous soon enough
Please read post #3. Was there something in that which is not clear?

One more time:-

Todays_Date: 08, 10, 31, 23, 55, 0
There is no 31st November. OK?
Philip M is offline   Reply With Quote
Old 11-01-2008, 11:22 PM   PM User | #6
r243
New to the CF scene

 
Join Date: Nov 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
r243 is an unknown quantity at this point
Sorry, I did not mean that literally. At the time I noticed the error, it was about 23:55 the 31'st of october. And Todays_Date was generated, at that time, with this script:
Code:
Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(), Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();
r243 is offline   Reply With Quote
Old 11-02-2008, 09:26 AM   PM User | #7
Philip M
Master Coder

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 7,670
Thanks: 94
Thanked 917 Times in 898 Posts
Philip M will become famous soon enoughPhilip M will become famous soon enough
If target month is wrong then the time difference always would be wrong, right?
No, wrong. Any month with less than 31 days will give an incorrect result if the 31st is selected (or of course >28 in February).

And it works almost all the time.
That means that it does not work. Period. You cannot be "almost a virgin".

Try changing Todays_Month to Todays_Month-1 (to change months 1-12 to 0-11)
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 06:35 PM.

Home - Contact Us - Archives - Link to CF - Resources - Top 

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.