我需要输出以下内容:
凌晨12:00 - 凌晨12:00,上午06:00 - 08:00 AM,上午9:00 - 上午11:00
但是我得到了这个:
凌晨12:00 - 凌晨12:00 06:00 AM - 08:00 AM 09:00 AM - 11:00 AM
var time = '00:00-00:00,06:00-08:00,09:00-11:00';
var slotTimeCount = time.split(',');
var cnts = slotTimeCount.length;
var mySeven = 0;
for (var sltCont = 0; sltCont < cnts; sltCont++) {
//console.log(slotTimeCount);
var time = slotTimeCount[sltCont];
var splitTime = time.split('-');
//console.log(splitTime);
var splitTimeFrom = splitTime[0];
var splitTimeTo = splitTime[1];
var clinicFromTime = splitTimeFrom.split(':');
var h_24 = clinicFromTime[0];
var mins = clinicFromTime[1];
var clinicToTime = splitTimeTo.split(':');
var h1_24 = clinicToTime[0];
var mins1 = clinicToTime[1];
var h = h_24 % 12;
if (h === 0) {
h = 12;
}
var h1 = h1_24 % 12;
if (h1 === 0) {
h1 = 12;
}
var cnvrt = (h < 10 ? "0" + h : h) + ":" + mins +
" " + (h_24 < 12 ? 'AM' : 'PM') + ' - ' +
(h1 < 10 ? "0" + h1 : h1) + ":" + mins1 +
" " + (h1_24 < 12 ? 'AM' : 'PM');
console.log(cnvrt);
}
&#13;
答案 0 :(得分:0)
您只需使用Array.prototype.join()方法即可。我在代码中只添加了三行代码。
var time = '00:00-00:00,06:00-08:00,09:00-11:00';
var slotTimeCount = time.split(',');
var cnts = slotTimeCount.length;
var mySeven = 0;
var str = [];
for (var sltCont = 0; sltCont < cnts; sltCont++) {
var time = slotTimeCount[sltCont];
var splitTime = time.split('-');
var splitTimeFrom = splitTime[0];
var splitTimeTo = splitTime[1];
var clinicFromTime = splitTimeFrom.split(':');
var h_24 = clinicFromTime[0];
var mins = clinicFromTime[1];
var clinicToTime = splitTimeTo.split(':');
var h1_24 = clinicToTime[0];
var mins1 = clinicToTime[1];
var h = h_24 % 12;
if (h === 0) {
h = 12;
}
var h1 = h1_24 % 12;
if (h1 === 0) {
h1 = 12;
}
var cnvrt = (h < 10 ? "0" + h : h) + ":" + mins +
" " + (h_24 < 12 ? 'AM' : 'PM') + ' - ' +
(h1 < 10 ? "0" + h1 : h1) + ":" + mins1 +
" " + (h1_24 < 12 ? 'AM' : 'PM');
str.push(cnvrt); // add to array
}
console.log(str.join(', ')); // join the array
答案 1 :(得分:0)
添加一个var final,然后替换逗号
final+=cnvrt.replace(/,/g,' ');
然后在结尾处显示最终
var time='00:00-00:00,06:00-08:00,09:00-11:00';
var slotTimeCount=time.split(',');
var cnts=slotTimeCount.length;
var mySeven =0;
var final="";
for(var sltCont=0; sltCont <cnts; sltCont++)
{//console.log(slotTimeCount);
var time=slotTimeCount[sltCont];
var splitTime = time.split('-');
//console.log(splitTime);
var splitTimeFrom = splitTime[0];
var splitTimeTo = splitTime[1];
var clinicFromTime = splitTimeFrom.split(':');
var h_24 = clinicFromTime[0];
var mins = clinicFromTime[1];
var clinicToTime = splitTimeTo.split(':');
var h1_24 = clinicToTime[0];var mins1 = clinicToTime[1];
var h = h_24 % 12;if (h === 0) h = 12;
var h1 = h1_24 % 12;if (h1 === 0) h1 = 12;
var cnvrt=(h < 10 ? "0" + h : h) + ":" + mins + " " + (h_24 < 12 ? 'AM' : 'PM') + ' - ' + (h1 < 10 ? "0" + h1 : h1) + ":" + mins1 + " " + (h1_24 < 12 ? 'AM' : 'PM');
final+=cnvrt.replace(/,/g,' ')+' ';
}
alert(final)
&#13;