SOURCE

function reduMonths(startMonth,endMonth){ 
  let startY = startMonth.split("-")[0],
      startM = startMonth.split("-")[1],
      endY = endMonth.split("-")[0],
      endM = endMonth.split("-")[1];
      startMonth = startY + startM
      endMonth = endY + endM
  if(startMonth > endMonth){
    let reduY = startY - endY,
        reduM = startM - endM;
    return reduY*12+reduM + "个月"
  }else if(startMonth < endMonth){
    let reduY = endY - startY,
        reduM = endM - startM;
      
    return reduY*12+reduM + "个月"
  }else{
    return 0 + "个月"
  }
}
 
console.log("月份差:",reduMonths("2020-10","2022-01"));
 
//取月份
let date = "2022-03-21"
console.log("格式化月份:", date.split("-")[0] + "-" +  date.split("-")[1]);
console 命令行工具 X clear

                    
>
console