var getTime = function() {
var _ = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09'], //补零
d = new Date(),
h = d.getHours(),
m = d.getMinutes(),
s = d.getSeconds();
setTimeout(function(){
console.log(getTime());
}, 1000);
return [_[h] || h, _[m] || m, _[s] || s].join(":");
}
setTimeout(function(){
console.log(getTime());
}, 1000);
=============================
function startit(h=0,m=0,s=0){
var _ = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09'];
s++;
if(s>=60){
s = 0;
m++;
}
if(m>=60){
m = 0;
h++;
}
console.log([_[h] || h, _[m] || m, _[s] || s].join(":"));
setTimeout("startit("+h+","+m+","+s+")", 1000);
}
startit(1,1,28);
===============
var start=new Date("2002-01-10 09:33:10");
var end=new Date("2002-01-10 10:00:00");
function formatTime(seconds) {
var min = Math.floor(seconds / 60),
second = seconds % 60,
hour, newMin, time;
if (min > 60) {
hour = Math.floor(min / 60);
newMin = min % 60;
}
if (second < 10) { second = '0' + second;}
if (min < 10) { min = '0' + min;}
return time = hour? (hour + ':' + newMin + ':' + second) : (min + ':' + second);
}
console.log(formatTime((end-start)/1000));