获取2个日期之间的月份名称

我要和两个约会。 我想获得这两个日期之间的所有月份名称。

以下是我的代码

 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'); 

我怎样才能使它工作?

我的更好: 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')); 

你将不得不做一个比切片更手动的方法。 这是一个起点,您可以确定如何处理注释中提到的案例。

 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"] 

如果从理解的角度帮助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"] 
 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'))来测试它。

懒惰的回答:

 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'));