输入tm=11 输出11:00 输入1输出 01:00
时间处理时分,没有秒
function makeTime(tm) {
if(!tm){return ''}
//let regTm=/^(?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]:[0-5][0-9]$/;
let regTm=/^(?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]$/;
let reg=/^[0-9]*$/;
let isNum=reg.test(tm);
//console.log(tm)
let isTm=regTm.test(tm);
if(isTm){return tm}
if(!isNum&&!isTm){
return '';
}
//console.log(tm.length);
//console.log(tm);
if(tm.length==1){
tm='0'+tm+'00';
tm=mkTime(tm);
if(regTm.test(tm)){return tm;}else{return '';}
}else if(tm.length==2){
let tmArr=tm.split('');
let tm01=tm+'00'; tm01=mkTime(tm01);
let tm02='0'+tm+'0'; tm02=mkTime(tm02);
let tm03='00'+tm; tm03=mkTime(tm03);
if(regTm.test(tm01)){return tm01;}
else if(regTm.test(tm02)){return tm02;}
else if(regTm.test(tm03)){return tm03;}
else{return '';}
}else if(tm.length==3){
let tm01=tm+'0';
let tm02='0'+tm;
tm01=mkTime(tm01);
tm02=mkTime(tm02);
if(regTm.test(tm01)){return tm01;}
else if(regTm.test(tm02)){return tm02;}
else{return '';}
}else if(tm.length==4){
tm=mkTime(tm);
if(regTm.test(tm)){return tm;}else{return '';}
}else{
tm=mkTime(tm);
if(regTm.test(tm)){return tm;}else{return '';}
}
function mkTime(time) {
//console.log(time);
let h=time.substring(0,2);
let m=time.substring(2,4);
let str=h+':'+m;
//console.log(str);
return str;
}
}
}