![]() |
|
|
|||||||
![]() |
|
|
Thread Tools | Rate Thread |
|
|
PM User | #1 |
|
New to the CF scene Join Date: Nov 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
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); Code:
date1 = (new Date(8, 10, 31, 23, 55, 25)).getTime(); date2 = (new Date(8, 11, 1, 23, 59, 0)).getTime(); Thanks |
|
|
|
|
|
PM User | #2 |
|
Master Coder ![]() ![]() Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 9,818
Thanks: 0
Thanked 48 Times in 47 Posts
![]() |
Returns positive for me => 215000
It shouldn't be a problem as getTime() returns the number of milliseconds since midnight of 01/01/1970
__________________
Glenn Got JavaScript problems? JavaScript FAQ | Using Square Bracket Notation | Search CF Need scripts? Editable Type-ahead Combo | Mobile Phone Keypad script | OO Number Spinner | Drag 'n Drop Tower of Hanoi | DHTML SameGame | Popup DocViewer | OO Dropdown Date Picker | Paste Splitter |
|
|
|
|
|
PM User | #3 |
|
Master Coder ![]() Join Date: Jun 2002
Location: London, England
Posts: 7,670
Thanks: 94
Thanked 917 Times in 898 Posts
![]() ![]() |
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>
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 |
|
|
|
| Users who have thanked Philip M for this post: | oesxyl (11-01-2008) |
|
|
PM User | #4 |
|
New to the CF scene Join Date: Nov 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
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);
}
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 |
|
|
|
|
|
PM User | #6 |
|
New to the CF scene Join Date: Nov 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
![]() |
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(); |
|
|
|
|
|
PM User | #7 |
|
Master Coder ![]() Join Date: Jun 2002
Location: London, England
Posts: 7,670
Thanks: 94
Thanked 917 Times in 898 Posts
![]() ![]() |
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) |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Rate This Thread | |
|
|