【发布时间】:2010-04-09 10:44:58
【问题描述】:
interval = new Date(0);
return interval.getHours();
以上返回 16。我希望它返回 0。任何指针? getMinutes() 和 getSeconds() 按预期返回零。谢谢!
我正在尝试做一个计时器:
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;
}
}
除了 16 小时的差异外,一切正常。有什么想法吗?
【问题讨论】:
标签: javascript date