Straigt的Javascript时间格式/更新
1:创建月变换器FUNC 2:创建时间FUNC 3:创建更新FUNC 4:创建输出FUNC
// month converter from index/0-11 values
function covertMonth(num){
let months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
// look into index with num 0-11
let computedRes = months[num];
return computedRes;
}
// time func
function Time(){
// important to get new instant of the Date referrance
let date = new Date();
this.time = date.toLocaleTimeString();
this.year = date.getUTCFullYear();
this.day = date.getUTCDate();
this.month = date.getUTCMonth();
this.currentTime = date.toLocaleTimeString() + ' ' + covertMonth(this.month) + ' ' + this.day + ' ' + this.year;
return this.currentTime;
}
function timeOutPut(){
let where = document.getElementById('some-id');
where.textContent = Time(); // 1:21:39 AM Dec 17 2017
}
// run every 5secs
setInterval(timeOutPut, 5000);
伊沃 - 好悲痛的人,我相信你可以提供你的解决方案而不会粗鲁。 没有人没有错误:“那么” - >“比”?来吧。 – d2burke 2012-02-08 14:47:37
您应该使用前导零填充'now.getMinutes()'调用。 – 2018-01-27 23:05:26