
Mailing List Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [tlug] [OT] Subtracting two times in JavaScript
- Date: Sat, 20 Mar 2010 23:08:17 +0900
- From: Lewske Wada <ryu@example.com>
- Subject: Re: [tlug] [OT] Subtracting two times in JavaScript
- References: <4BA4D0BD.6030802@example.com>
- User-agent: Mozilla-Thunderbird 2.0.0.22 (X11/20090707)
Dave M G wrote:
> 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).
> when I expect the different to be about an hour (1:00), it comes out as 9:59.
>
> 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.
>
>
Whenever you make difference of two different times,
you need to get everything in UTC as it tries to use the time
in default time zone (JST-9) if omitted, I think.
So you will probably want to :
endTime = new Date();
endTime.setHours(endTime.getHours() + 1);
currTime = new Date();
diff = new Date(endTime.getTime() - currTime.getTime());
var hours = diff.getUTCHours();
var minutes = diff.getUTCMinutes();
if (minutes.length == 1)
{
minutes = "0" + minutes;
}
remainTime = hours + ":" + minutes;
return remainTime;
Ryu
Home |
Main Index |
Thread Index