Why Do I Get +1 Hour When Calculating Time Difference In Javascript?
I trying to create a very simple time difference calculation. Just 'endtime - starttime'. I'm getting +1 hour though. I suspect it has with my timezone to do, since I'm GMT+1. Rega
Solution 1:
You must understand what Date
object represent and how it stores dates. Basically each Date
is a thin wrapper around the number of milliseconds since 1970 (so called epoch time). By subtracting one date from another you don't get a date: you just get the number of milliseconds between the two.
That being said this line doesn't have much sense:
var diff = newDate(nu - tid1);
What you really need is:
var diffMillis = nu - tid1;
...and then simply extract seconds, minutes, etc.:
var seconds = Math.floor(diffMillis / 1000);
var secondsPart = seconds % 60;
var minutes = Math.floor(seconds / 60);
var minutesPart = minutes % 60;
var hoursPart = Math.floor(minutes / 60);
//...console.log(hoursPart + ":" + minutesPart + ":" + secondsPart);
Post a Comment for "Why Do I Get +1 Hour When Calculating Time Difference In Javascript?"