I have a date object created from vars saved in a database.
var prevTime = Date(year,month,day,hour,minute);
I want to calculate the difference between this and the current time.
var thisTime = Date();
I am doing this:
prevTime.getTime() - thisTime.getTime();
It gives me a negative number that is very large. I divide by 1000 to get seconds and then divide by 3600 to get hours. I need an elapsed time in hours. I end up with a number that is like -756.00. If the current time is larger than the previous time, why is the number negative? What am I doing wrong?
Thanks,
Scott
I have a date object created from vars saved in a database.
var prevTime = Date(year,month,day,hour,minute);
I want to calculate the difference between this and the current time.
var thisTime = Date();
I am doing this:
prevTime.getTime() - thisTime.getTime();
It gives me a negative number that is very large. I divide by 1000 to get seconds and then divide by 3600 to get hours. I need an elapsed time in hours. I end up with a number that is like -756.00. If the current time is larger than the previous time, why is the number negative? What am I doing wrong?
Thanks,
Scott
Share Improve this question edited Jul 9, 2011 at 18:31 Felix Kling 816k180 gold badges1.1k silver badges1.2k bronze badges asked Jul 9, 2011 at 18:20 llihttocsllihttocs 2,0113 gold badges14 silver badges13 bronze badges 2 |4 Answers
Reset to default 24The current time is larger than the previous time so subtracting a larger number from a smaller number gives you a negative number. Reverse the order if you want the positive difference in times. Is there more to the question than that?
Demonstration here: http://jsfiddle.net/jfriend00/NYSsp/
var prevTime = new Date(2011,1,1,0,0); // Feb 1, 2011
var thisTime = new Date(); // now
var diff = thisTime.getTime() - prevTime.getTime(); // now - Feb 1
alert(diff / (1000*60*60*24)); // positive number of days
First one must be smaller that you get negative. Your first date is in the past.
Here is an example:
var a = new Date(2011,7,5,2,1,1) - new Date(2011,7,5,1,1,1);
alert(a); // 3600000 which is millisecond for 1 hour. Divide by this to get hours.
IF you want Date and Time in Dynamic Run on web Page and also the Login time i.e the clock starts from the start of java function scipt and also gives how many hours,minutes and seconds you are online in the Application or a Particular web Page For this just call the javascript function in the html body load.
<script type="text/javascript">
var second;
var first = new Date;
function date_time() {
var id = 'Label1'
date = new Date;
second = date.getTime();
year = date.getFullYear();
month = date.getMonth();
months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
d = date.getDate();
day = date.getDay();
days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
h = date.getHours();
if (h < 10) {
h = "0" + h;
}
m = date.getMinutes();
if (m < 10) {
m = "0" + m;
//min = min+1;
}
s = date.getSeconds();
if (s < 10) {
s = "0" + s;
//sec = sec+1;
}
var timeDiff = second - first.getTime();
var hours = Math.floor(timeDiff / (1000 * 60 * 60));
timeDiff -= hours * (1000 * 60 * 60);
var mins = Math.floor(timeDiff / (1000 * 60));
timeDiff -= mins * (1000 * 60);
var secs = Math.floor(timeDiff / 1000)
timeDiff -= secs * 1000;
result = 'LoginTime(HH:MM:SS) ' + hours + ':' + mins + ':' + secs + ' ' + days[day] + ' ' + months[month] + ' ' + d + ' ' + year + ' Clock(24hr): ' + h + ':' + m + ':' + s;
document.getElementById(id).innerHTML = result;
setTimeout('date_time("' + id + '");', '1000');
return true;
}
By Santosh Kumar Murarkar
Check this:
var first = new Date();
for(i = 0; i<10000; i++) // Some loop to make a little time pass
{
i = i;
}
var second = new Date();
second.getTime() - first.getTime();
thisTime - prevTime
. – Šime Vidas Commented Jul 9, 2011 at 18:23