jsは2つの時間の間に月を違います.
話を多くしないで、直接コードをかけます.
/**
*
*/
function getMonths(start, end){
var result = [];
var starts = start.split('-');
var ends = end.split('-');
var staYear = starts[0]*1;
var staMon = starts[1]*1 < 10? starts[1]:starts[1];
var endYear = ends[0]*1;
var endMon = ends[1]*1 < 10? ends[1]:ends[1];;
result.push(staYear+'-'+staMon);
while (staYear <= endYear) {
if (staYear === endYear) {
while (staMon < endMon) {
staMon++;
if(staMon < 10){
result.push(staYear+'-0'+staMon);
}else{
result.push(staYear+'-'+staMon);
}
}
staYear++;
} else {
staMon++;
if (staMon > 12) {
staMon = 1;
staYear++;
}
if(staMon < 10){
result.push(staYear+'-0'+staMon);
}else{
result.push(staYear+'-'+staMon);
}
}
}
return result;
}
/**
*
* @param date
* @param num
*/
function getfMonths(date, num){
var dates = date.split('-');
var year = dates[0]*1;
var month = dates[1]*1;
var i = 1;
var result = [];
result.push(dates[0]+'-'+dates[1]);
while(i < num){
month++;
if(month < 10){
result.push(year+'-0'+month);
}else{
if(month < 13){
result.push(year+'-'+month);
}else{
month = 1;
year++;
result.push(year+'-0'+month);
}
}
i++;
}
return result;
}
/**
*
* @param date
* @param num
*/
function getlMonths(date, num){
var dates = date.split('-');
var year_ = dates[0]*1;
var month_ = dates[1]*1;
var i = num;
var result = [];
while(i > 0){
i--;
var month = month_;
var year = year_;
if(i == 0){
result.push(dates[0]+'-'+dates[1]);
}else{
if(month_ < num){
month += 12;
year--;
month -= i;
if(month < 10){
result.push(year+'-0'+month);
}else{
if(month == 13){
month = 1;
result.push(year+'-0'+month);
}else{
result.push(year+'-'+month);
}
}
}else{
month -= i;
if(month < 10){
result.push(year+'-0'+month);
}else{
result.push(year+'-'+month);
}
}
}
}
return result;
}
よろしくお願いします.