views:

70

answers:

2
interval = new Date(0);
return interval.getHours();

The above returns 16. I expect it to return 0. Any pointers? getMinutes() and getSeconds() return zero as expected. Thanks!

I am trying to make a timer:

function Timer(onUpdate) {
    this.initialTime = 0;
    this.timeStart = null;

    this.onUpdate = onUpdate

    this.getTotalTime = function() {
        timeEnd = new Date();
        diff = timeEnd.getTime() - this.timeStart.getTime();

        return diff + this.initialTime;
    };

    this.formatTime = function() {
        interval = new Date(this.getTotalTime());

        return this.zeroPad(interval.getHours(), 2) + ":" +  this.zeroPad(interval.getMinutes(),2) + ":" + this.zeroPad(interval.getSeconds(),2);
    };

    this.start = function() {
        this.timeStart = new Date();
        this.onUpdate(this.formatTime());
        var timerInstance = this;
        setTimeout(function() { timerInstance.updateTime(); }, 1000);
    };

    this.updateTime = function() {
        this.onUpdate(this.formatTime());
        var timerInstance = this;
        setTimeout(function() { timerInstance.updateTime(); }, 1000);
    };

    this.zeroPad = function(num,count) {
        var numZeropad = num + '';
        while(numZeropad.length < count) {
            numZeropad = "0" + numZeropad;
        }
        return numZeropad;
    }
}

It all works fine except for the 16 hour difference. Any ideas?

+3  A: 

If you initialize the Date with 0, it will be set to the beginning of the epoch, Jan 1st 1970 00:00:00 GMT. The hours you get is the localized time offset.

To make a timer, you'd rather start with the current timestamp and calculate the difference to it later on. Remember that timestamps are absolute points in time, not relative.

var start = new Date();

// Time is ticking, ticking, ticking...

var end = new Date();

alert(end - start);

Or, more concrete:

var start = new Date();

setTimeout(function () {
    var end = new Date();
    alert(end - start);
}, 2000);
deceze
Hmm.. well see my edits to my question. I may be taking the wrong route
Jonah
this is what I did. But the difference comes out to be zero, and `new Date(0)` reports 16 hours.. I think the problem is that getHours() returns the hour number in the day, not the unix timestamp converted into hours (does that make sense?)
Jonah
@Jonah Exactly. In the example above, the difference will most likely be 0, since it's executing immediately. The `time is ticking...` means you need to do something in between and `end` will be created sometime later. Type this example in the console of your browser and you'll see the difference.
deceze
@Jonah `getHours()` indeed returns the **absolute time**, i.e. 4pm, of the timestamp `Dec 31st 1969, 16:00:00` (localized).
deceze
OK so what i needed was a way to convert the milliseconds to hours, minutes and seconds. Working now.. (getHours() does not work, use a different way) Thanks!
Jonah
A: 

Date represents a point on our calendar, not a time span. Trying to use it for the latter will lead to errors when you fail to account for our screwy calendar...

That said, you should be able to use it for short time spans without problem; you just need to remember that the number you're passing to the constructor is interpreted as the number of milliseconds since the epoch and that the epoch is UTC!

return this.zeroPad(interval.getUTCHours(), 2) 
  + ":" +  this.zeroPad(interval.getUTCMinutes(),2)
  + ":" + this.zeroPad(interval.getUTCSeconds(),2);

...keeping in mind that this will fall apart as soon as you exceed 24 hours. At that point, you'll have to go back to the tried and true method of modulo 24*60*60*1000, divide by 60*60*1000, etc.

Shog9