JS获取当前加指定N个月日期

版权声明: https://blog.csdn.net/qq_31334119/article/details/80825958
  //日期转换
   //---------
function transformationDate(date,month){
     var strDate;
     var oldDate= strToDate(date);
     var newDate = strToDate(date);
     newDate.setMonth(newDate.getMonth() + month);
     var yy1 = newDate.getFullYear();
     var mm1 = newDate.getMonth()+1;//因为getMonth()返回值是 0(一月) 到 11(十二月) 之间的一个整数。所以要给其加1
     var dd1 = newDate.getDate();
     if (mm1 < 10 ) {
         mm1 = '0'+ mm1;
     }
     if (dd1 < 10) {
       dd1 = '0' + dd1;
     }
    //预计结束日期=开始日期+期限
    if(oldDate.getDate()==newDate.getDate()){  //月末,getDaysInMonth()获取该月的最后一天
    var strDate= yy1+"-"+mm1+"-"+dd1;
      }else{
    var strDate= yy1+"-"+newDate.getMonth()+"-"+new Date(yy1,newDate.getMonth(),0).getDate(); 
      }    
    console.log(strDate);
}
  function strToDate(str)
  {
    var val=Date.parse(str);
    var newDate=new Date(val);
    return newDate;
  }
  //----------

猜你喜欢

转载自blog.csdn.net/qq_31334119/article/details/80825958