Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list how many days for a month for a specific year?

Tags:

javascript

How to list how many days for a month for a specific year in JavaScript?

As we know 30 days have September, April, June, and November. All the rest have 31, Except February, Which has 28 days clear, And 29 in each leap year.

I would need take count of leap year. Do you know any native way to fond out.. or maybe a library.. could you suggest one?

like image 609
GibboK Avatar asked Jul 24 '13 13:07

GibboK


People also ask

How do I calculate the number of days in a month and year in Excel?

Calculate elapsed year, month and days Select a blank cell which will place the calculated result, enter this formula =DATEDIF(A2,B2,"Y") & " Years, " & DATEDIF(A2,B2,"YM") & " Months, " & DATEDIF(A2,B2,"MD") & " Days", press Enter key to get the result.


2 Answers

This will work too assuming Jan=1, Feb=2 ... Dec=12

function daysInMonth(month,year) 
{
   return new Date(year, month, 0).getDate();
}

FIDDLE

like image 39
Khawer Zeshan Avatar answered Sep 21 '22 01:09

Khawer Zeshan


try this

function daysInMonth(m, y)

{
  m=m-1; //month is zero based...
  return 32 - new Date(y, m, 32).getDate();
}

usage :

>> daysInMonth(2,2000) //29

like image 195
Royi Namir Avatar answered Sep 20 '22 01:09

Royi Namir