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.
Regardless, that should not affect the difference, since both start and end times are in the same timezone.
Check my running example-code here:
http://jsfiddle.net/kaze72/Rm3f3/
$(document).ready(function() {
var tid1 = (new Date).getTime();
$("#tid").click(function() {
var nu = (new Date).getTime();
var diff = new Date(nu - tid1);
console.log(diff.getUTCHours() + ":" +
diff.getUTCMinutes() + ":" +
diff.getUTCSeconds());
console.log(diff.toLocaleTimeString());
});
})
You must understand what
Dateobject represent and how it stores dates. Basically eachDateis 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:
What you really need is:
…and then simply extract seconds, minutes, etc.:
Working fiddle.