我一直在試圖讓下面的代碼返回一個內部的兩個一等數組..例如:[ 'July 1st', '4th' ]
。但是,到目前爲止,我的代碼返回:[ 'July 1st, 4th' ]
。請注意,我需要它有確切的引號。用引號輸出數組的問題
這是我的代碼:
function friendly(str) {
var final = [];
var months = {01:"January" , 02: "February", 03:"March", 04:"April", 05:"May", 06:"June", 07:"July", 08:"August", 09:"September", 10:"October", 11:"November", 12:"December"}, i, c, date1, date2, getYear1, getMonth1, getDay1, getYear2, getMonth2, getDay2;
var days = {01: "st", 02: "nd", 03: "rd", 04: "th", 05: "th"};
date1 = str[0];
date1.split('-');
date2 = str[1];
date2.split('-');
getYear1 = date1.substring(0, 4);
getMonth1 = date1.substring(5, 7);
getMonth2 = date2.substring(5, 7);
getDay1 = date1.substring(8, 10);
getYear2 = date2.substring(0,4);
getMonth2 = date2.substring(5,7);
getDay2 = date2.substring(8, 10);
for(var key in months){
//console.log(getMonth1.charAt(0) == 0);
if(getMonth1.charAt(0) == 0){
getMonth1 = getMonth1.slice(1);
}
if(getMonth2.charAt(0) == 0){
getMonth2 = getMonth2.slice(1);
}
if(getDay1.charAt(0) == 0){
getDay1 = getDay1.slice(1);
}
if(getDay2.charAt(0) == 0){
getDay2 = getDay2.slice(1);
}
if(days.hasOwnProperty(getDay1)){
getDay1 = getDay1 + days[getDay1];
}
if(days.hasOwnProperty(getDay2)){
getDay2 = getDay2 + days[getDay2];
}
if(months.hasOwnProperty(getMonth1)){
getMonth1 = months[getMonth1];
}
if(months.hasOwnProperty(getMonth2)){
getMonth2 = months[getMonth2];
}
if(getMonth1 == getMonth2 && getYear1 == getYear2 && getDay1 !== getDay2){
return [getMonth1 + ' ' + getDay1 + ', ' + getDay2.split(',')];
//console.log(getMonth1);
}
else if(getMonth1 == getMonth2 && getYear1 == getYear2 && getDay1 == getDay2){
return [getMonth1 + ' ' + getDay1 + ', ' + getYear1];
}
else if(getYear1 !== getYear2 && getMonth1 !== getMonth2 && getDay1 !== getDay2){
return [getMonth1 + ' ' + getDay1 + ', ' + getMonth2 + ' '+ getDay2.split(',')];
}
else if(getYear1 == getYear2 && getMonth1 !== getMonth2 && getDay1 !== getDay2){
return [getMonth1 + ' ' + getDay1 + ', ' + getMonth2 + ' '+ getDay2 + ', ' + getYear1];
}
else if(getYear1 == getYear2 && getMonth1 !== getMonth2 && getDay1 !== getDay2){
return;
}
else if (getYear1 !== getYear2 && getMonth1 == getMonth2 && getDay1 !== getDay2){
return [getDay1 + ', ' + getDay2.split(',')];
}
}
}
friendly(['2015-07-01', '2015-07-04']);
扯淡!感謝注意,哈哈,仍然有我的問題,雖然.. – feners