Sohail Yasmin
Sohail Yasmin

Reputation: 518

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

I want to get the days of more then 1 month if pass 2 , 3 ,4 as number of month to the function, I want to get total number of days from today

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
  } 
}

Thanks for help in advance

Upvotes: 0

Views: 68

Answers (1)

James Donnelly
James Donnelly

Reputation: 128771

Use a for loop:

var total = 0;

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

return total;

JSFiddle demo.

Upvotes: 2

Related Questions