【发布时间】:2015-10-01 18:06:12
【问题描述】:
我有这个应该显示时间的 javascript 代码。有用。不过,我不能增加额外的时间。假设我想增加 1 小时。
<script type="text/javascript">
Date.prototype.addHours = function(h) {
this.setTime(this.getTime() + (h*60*60*1000));
return this;
}
// This function gets the current time and injects it into the DOM
function updateClock() {
// Gets the current time
var now = new Date();
// Get the hours, minutes and seconds from the current time
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
// Format hours, minutes and seconds
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
// Gets the element we want to inject the clock into
var elem = document.getElementById('clock');
// Sets the elements inner HTML value to our clock data
elem.innerHTML = hours + ':' + minutes + ':' + seconds;
}
function start(){
setInterval('updateClock()', 200);
}
</script>
第一个函数计算我要添加的毫秒数,第二个函数是“实时时钟”。如何将第一个函数实现到第二个函数中,从而得到工作结果?
【问题讨论】:
-
你会做
now.addHours(2)。但是有更简单的方法可以为 Date 添加小时数。
标签: javascript time clock