5
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?

2 Answers 2

8

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);
5
  • Hmm.. well see my edits to my question. I may be taking the wrong route
    – Jonah
    Apr 9, 2010 at 2:19
  • 1
    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
    Apr 9, 2010 at 2:25
  • @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
    Apr 9, 2010 at 2:29
  • @Jonah getHours() indeed returns the absolute time, i.e. 4pm, of the timestamp Dec 31st 1969, 16:00:00 (localized).
    – deceze
    Apr 9, 2010 at 2:30
  • 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
    Apr 9, 2010 at 2:31
0

Using getUTCHours() works as expected.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getUTCHours

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.