let timeStr = '2019/7/3 16:5:6'
let timeStr1 = '2019-07-03'
let ceshi = '2019/7/3'
let ceshi1 = '2019/7/3 16'
let ceshi2 = '2019/7/7 16:05'
let ceshi3 = '2019-07-03 16:05:07'
let ceshi4 = '2019-7-3 16:5:7'
function resetTime(timeStr,format){
if(timeStr){
let year,month,date,mi,se,hour,minutes,seconds
if(timeStr.indexOf(format)!=-1){
let time = timeStr.split(format)
year = time[0]
month = time[1].length>1 ? time[1] : ('0'+time[1])
if(time[2]){
date = time[2].split(' ')[0]
}
if(date){
date = date.length>1 ? date : ('0'+date)
}
mi = timeStr.split(' ')
if(mi[1]){
if(mi[1].indexOf(':')!=-1){
se = mi[1].split(':')
if(se.length>2){
hour = (se[0].length>1 ? se[0] : ('0'+se[0]))
hour = hour ? hour : '00'
minutes = se[1].length>1 ? se[1] : ('0'+se[1])
minutes = minutes ? minutes : '00'
seconds = se[2].length>1 ? se[2] : ('0'+se[2])
seconds = seconds ? seconds : '00'
}else if(se.length == 2){
hour = (se[0].length>1 ? se[0] : ('0'+se[0]))
hour = hour ? hour : '00'
minutes = se[1].length>1 ? se[1] : ('0'+se[1])
minutes = minutes ? minutes : '00'
seconds = "00"
}
}else{
hour = '00'
minutes='00'
seconds = '00'
}
}else{
hour = '00'
minutes='00'
seconds = '00'
}
}
return year+'-'+month+'-'+date+' '+hour+':'+minutes+':'+seconds
}
}
console.log('时间',resetTime(timeStr1,'-'))
console.log('ceshi',resetTime(ceshi,'/'))
console.log('timeStr',resetTime(timeStr,'/'))
console.log('ceshi2',resetTime(ceshi2,'/'))
console.log('ceshi3',resetTime(ceshi3,'-'))
console.log('ceshi4',resetTime(ceshi4,'-'))
console.log('ceshi1',resetTime(ceshi1,'/'))
网友评论