2014-01-29 99 views
2

我必須有兩個日期fromto。我想在這兩個日期之間得到所有的月份名稱。得到兩個日期之間的月份名稱

以下是我的代碼

var monthNames = [ "January", "February", "March", "April", "May", "June", 
     "July", "August", "September", "October", "November", "December" ]; 

function diff(from, to) { 
    var datFrom = new Date('1 ' + from); 
    var datTo = new Date('1 ' + to); 
    var arr = monthNames.slice(datFrom.getMonth(), datTo.getMonth() + 1); 
} 

上面的代碼適用於以下輸入

diff('September 2013', 'December 2013'); 

,但它不適用於本

diff('September 2013', 'February 2014'); 

工作,我怎樣才能使它發揮作用?

+3

您希望它爲'diff('September 2013','February 2015')'做什麼? – lurker

+0

我想要這兩個日期之間的所有月份名稱。 – 2619

+0

你想重複的月份名稱是問題的要點,我相信 –

回答

5

煤礦是更好:http://jsfiddle.net/kS73f/8/

var monthNames = [ "January", "February", "March", "April", "May", "June", 
     "July", "August", "September", "October", "November", "December" ]; 

function diff(from, to) { 
    var arr = []; 
    var datFrom = new Date('1 ' + from); 
    var datTo = new Date('1 ' + to); 
    var fromYear = datFrom.getFullYear(); 
    var toYear = datTo.getFullYear(); 
    var diffYear = (12 * (toYear - fromYear)) + datTo.getMonth(); 

    for (var i = datFrom.getMonth(); i <= diffYear; i++) { 
     arr.push(monthNames[i%12] + " " + Math.floor(fromYear+(i/12))); 
    }   

    return arr; 
} 

console.log(diff('September 2013', 'March 2014')); 
+0

當我調用這個'console.log(diff('September 2013','March 2014'));''然後'March'不包含在其中。 – 2619

+0

更新了jsFiddle,它是一箇舊版本。在for循環中將'<'更改爲'<=' –

+0

是的,現在很好,但有一點,我怎樣才能在這幾個月裏增加一年。例如如果我調用'console.log(diff('September 2013','March 2015'));'然後我需要返回'month year'。我如何修改這個? – 2619

2

你將不得不做一個比切片更手動的方法。這裏有一個起點,您可以確定如何處理評論中提到的案例。

function diff(from, to) { 
    var result = []; 
    var datFrom = new Date('1 ' + from); 
    var datTo = new Date('1 ' + to); 
    if(datFrom < datTo) { 
     var month = datFrom.getMonth(); 
     var toMonth = datTo.getMonth() + 1 + ((datTo.getYear() - datFrom.getYear())*12); //toMonth adjusted for year 
     for(; month < toMonth; month++) { //Slice around the corner... 
     result.push(monthNames[month % 12]); 
     } 
    } 

    return result; 
} 

diff('September 2013', 'February 2014'); //=["September", "October", "November", "December", "January", "February"] 
0
var monthNames = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; 

function diff(from, to) { 
    var datFrom = new Date('1 ' + from); 
    var datTo = new Date('1 ' + to); 
    var arr; 
    if(datFrom > datTo) { 
     return diff(to, from); 
    } 

    var fromYear = datFrom.getFullYear(); 
    var toYear = datTo.getFullYear(); 

    if(fromYear === toYear) { 
     return monthNames.slice(datFrom.getMonth(), datTo.getMonth() + 1); 
    } else { 
     var arr = addYear(monthNames.slice(datFrom.getMonth(), new Date('1 December ' + fromYear)), fromYear); 
     for(var i = 1; i < (toYear - fromYear); i++) { 
     arr = arr.concat(addYear(monthNames, fromYear + i)); 
     } 
     return arr.concat(addYear(monthNames.slice(new Date('1 January ' + fromYear).getMonth(), datTo.getMonth() + 1), toYear)); 
    } 
} 

function addYear(arr, year) { 
    var updatedArr = []; 
    for(var i = 0; i < arr.length; i++) { 
    updatedArr[i] = arr[i] + ' ' + year; 
    } 
    return updatedArr; 
} 

不是試圖console.log(diff('September 2013', 'February 2015'))進行測試。

+0

當我調用這個'console.log(diff('September 2013','March 2014'));'那麼它會記錄'[「September」,「十月「」十一月「」十二月「」一月「」二月「」三月「」四月「」五月「」六月「」七月「」八月「」九月「」十月「 ,「十一月」,「十二月」]' – 2619

+0

@ 2619 Ups,錯字。修好了,現在就試試。 –

+0

如何將年份與月份包含在一起? – 2619

-1

懶回答:

var monthNames = [ "January", "February", "March", "April", "May", "June", 
     "July", "August", "September", "October", "November", "December", 
     "January", "February", "March", "April", "May", "June", 
     "July", "August", "September", "October", "November", "December" ]; 

function diff(from, to) { 
    var mFrom = new Date('1 ' + from).getMonth(); 
    var mTo = new Date('1 ' + to).getMonth(); 
    mTo = mTo < mFrom ? mTo + 12 : mTo; 
    return monthNames.slice(mFrom, mTo + 1); 
} 

alert(diff('September 2013', 'December 2013')); 
alert(diff('September 2013', 'February 2014')); 
0

以下修改原有的功能儘可能少的,如果這能幫助從理解的角度來看,OP。

function diff(from, to) { 
    var datFrom = new Date('1 ' + from); 
    var datTo = new Date('1 ' + to); 
    var arr = monthNames.slice(datFrom.getMonth(), datTo.getMonth() + 1); 

    if (!arr.length) { 
     arr = monthNames.slice(datFrom.getMonth(), 12); 
     arr = arr.concat(monthNames.slice(0, datTo.getMonth() + 1)); 
    } 

    return arr; 
} 

console.log(diff('December 2013', 'February 2014')); //["September", "October", "November", "December", "January", "February"] 
+0

我也想包括所有這些月的一年。我怎樣才能做到這一點? – 2619

相關問題