如何在javascript中获得2,3,4个月的日子

how to get the days of 2,3,4 months in javascript

本文关键字:4个 日子 javascript      更新时间:2023-09-26

我想获得超过1个月的天数,如果将2,3,4作为月数传递给函数,我想获得从今天开始的总天数

function get_days_in_month(months){
   days_in_month = new Date();
   var y = days_in_month.getFullYear();
   var m = days_in_month.getMonth();
   if(months==1){
      return new Date(y,m+1,0).getDate();
   }
  if(months==2){
    // need to write code or some other logic
  } 
}

提前感谢您的帮助

使用for循环:

var total = 0;
for (i = 1; i <= months; i++) {
    total += new Date(y, m + i, 0).getDate();
}
return total;
<<p> JSFiddle演示/strong>。