Return 两个日期之间的月数作为字符串数组

Return the number of months between two dates as an array of strings

//So I have and array of months
 const months = [
     'January',
     'February',
     'March',
     'April',
     'May',
     'June',
     'July',
     'August',
     'September',
     'October',
     'November',
     'December'];

// If the input is startDate: 1-1-2019 & endDate: 3-15-21
// It should return an array like so:

const monthsBetweenDates = [January,February,March,April,May,June,July,August, September, October, November,December, January,February,March,April,May,June,July,August, September, October, November, December, January, Feb, March];

// The returned/new array should have all months between the start and end date above in Order, duplicated needed in order as well.

非常感谢您的帮助和时间。如果您还有其他问题,请告诉我。

function stringToDate(_date,_format,_delimiter)
{
            var formatLowerCase=_format.toLowerCase();
            var formatItems=formatLowerCase.split(_delimiter);
            var dateItems=_date.split(_delimiter);
            var monthIndex=formatItems.indexOf("mm");
            var dayIndex=formatItems.indexOf("dd");
            var yearIndex=formatItems.indexOf("yyyy");
            var month=parseInt(dateItems[monthIndex]);
            month-=1;
            var formatedDate = new Date(dateItems[yearIndex],month,dateItems[dayIndex]);
            return formatedDate;
}

var startDate = stringToDate('01/01/2019','mm/dd/yyyy','/')
var endDate   = stringToDate('08/15/2021','mm/dd/yyyy','/')

var startYear  = startDate.getFullYear();
var startMonth = startDate.getMonth();
var endYear    = endDate.getFullYear();
var endMonth   = endDate.getMonth();

months=['JANUARY','FEBRUARY','MARCH','APRIL','MAY','JUNE','JULY','AUGUST','SEPTEMBER','OCTOBER','NOVEMBER','DECEMBER'];
myArray = [];

  for(let i=startYear;i<= endYear;i++){
     for(let j=0; j<12;j++){
       if (i == startYear && j >= startMonth)myArray.push(months[j]);
       if (i >  startYear && i <     endYear)myArray.push(months[j]);
       if (i == endYear   && j <=   endMonth)myArray.push(months[j]);
     }
  }
       
       console.log(myArray);

普通算术解


考虑到目标,我认为用基础数学解决任务比处理 Date 类型更有意义。

您只需要:

  • destructure 开始日期和结束日期的日期字符串 (m-d-yyyy) 到 start/end 月和年 (mStart, yStart, mEnd, yEnd):
const [mStart, , yStart] = start.split('-'),
      [mEnd, , yEnd] = end.split('-') 
  • 计算期间的总长度(以月为单位,包括开始和结束)为
12*(yEnd-yStart)+(mEnd-mStart)+1}
  • return一个相应长度的array,其中每个项目将从源month数组中取出,旋转移动一个位置:
months[(mStart-1+i)%months.length]

您可以在下面找到完整的演示:

const startDate = '1-1-2019',
      endDate = '3-15-2021',
     
      monthsBetweenDates = (start,end) => {
        const months = ['January','February','March','April','May','June','July','August','September','October','November','December'],
              [mStart, , yStart] = start.split('-'),
              [mEnd, , yEnd] = end.split('-')             
        return Array.from(
          {length: 12*(yEnd-yStart)+(mEnd-mStart)+1},
          (_,i) => months[(mStart-1+i)%months.length]
        )
      }
      
console.log(monthsBetweenDates(startDate,endDate))
.as-console-wrapper{min-height:100%;}