/    Sign up×
Community /Pin to ProfileBookmark

how can you determine how many days in a month

I was wondering if I was to have a month, how cany determine how many days it has?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@mrhooMar 07.2007 — You need the year as well, to get all the Febs correctly.

[CODE]Date.prototype.monthDays= function(){
var m= this.getMonth()+1;
switch(m){
case 4: case 6: case 9: case 11: return 30;
case 2:
var y= this.getFullYear();
if (y > 1582){
if(y%400 ==0 || (y%100 !=0 && y%4==0)) return 29;
return 28;
}
else return ((y % 4 == 0) ? 29 : 28 );
default: return 31;
}
}[/CODE]
// call it on any Date object:

new Date(2008,0).monthDays()// remember, months start at 0 for January
Copy linkTweet thisAlerts:
@felgallMar 07.2007 — pass the year and month you want the number of days for into the following function if you want something shorter than the above solution:

<i>
</i>function daysInMonth(year,month) {
var t = new Date(year, month - 1, 32);
return 32 - t.getDate();
}
×

Success!

Help @domiereavron spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...