Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tlug] [OT] Subtracting two times in JavaScript



TLUG,

I know this isn't a JavaScript list, but the advantage you guys have
over other JavaScript lists I've posted to is that you actually respond.
So I hope this new question won't be contributing too much noise.

All I'm trying to do is take two times, subtract one from the other, and
get the difference back in hours and minutes (formatted like this - 0:00).

I've gone round and round trying to convert times back and forth from
Unix time, and using JavaScripts Date() functions which theoretically
allow you to subtract times directly...

In the following example, "this.timeStamp" is a Unix time stamp, and I'm
confident that it's set right because it's working in an another
function. (Of course, I've been wrong before.)

The times I'm getting back from this code is crazy. Like, when I expect
the different to be about an hour (1:00), it comes out as 9:59. Or
sometimes the format will be off, like 9:5

So I'm sure I've got my conversions off, but I've been over it again and
again, and don't see why this isn't working. Well, I mean, I'm using
code of the internet, so I'm going on the assumption that the pages I
got the code from knew what they were doing.

If anyone can, might they tell me why I'm not getting a correct hours
and minutes difference between these two times.

endTime = new Date(this.timeStamp);
currTime = new Date();
diff = new Date(endTime.getTime() - currTime.getTime());
var hours = diff.getHours();
var minutes = diff.getMinutes();
if (minutes.length == 1)
{
    minutes = "0" + minutes;
}
remainTime = hours + ":" + minutes;
return remainTime;

Any advice would be much appreciated.

-- 
Dave M G


Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links