javascript - 如何在javascript中获取2,3,4个月的天数

标签 javascript

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

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 loop :

var total = 0;

for (i = 1; i <= months; i++) {
    total += new Date(y, m + i, 0).getDate();
}

return total;

JSFiddle demo .

关于javascript - 如何在javascript中获取2,3,4个月的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23949249/

相关文章:

JavaScript 语法结构

javascript - 类的属性名称是什么?

javascript - JS DOM 操作 'mouseleave' 在 Safari 浏览器中意外触发

javascript - Nodejs : Good practice to just use the index. js 导出?

javascript - 访问通过社交注册创建的用户文档

javascript - 无法获取按钮的ID

javascript - 无法将按钮的 HTML 传递给 Angular ng-click 函数

javascript - 在 WebBrowser 的 DocumentCompleted 事件中操作 DOM 时,由于 jQuery 的 $(document).ready 导致轻微闪烁

javascript - 如何在 AngularJS 组件中绑定(bind)多个单词属性

Javascript 克隆并从克隆的对象中删除内容