Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: How many Days in a Month

1. ## How many Days in a Month

based on felgall’s idea:
PHP Code:
``` function daysInAMonth(month, year) {   return (32 - new Date(year, month-1, 32).getDate()); }  ```

• why not just
Code:
```function DaysInMonth(yr,mth) {
return new Date(yr,mth,0).getDate();
}```
?

• interesting, never thought to use underflow.

• Originally Posted by Dormilich
At least that post of mine got you thinking about it even if you didn't quite get to the simplest solution.

• Just for the record:-

Code:
```<script type = "text/javascript">

Date.prototype.daysinMonth=function(){
var d = new Date(this.getFullYear(), this.getMonth()+1, 0);
return d.getDate();
}

var d = new Date();
alert("Number of days in this month:- " + d.daysinMonth());

var Mth = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var i = 0;
var yr = 2012;
var Arr = [];
var dt = new Date(yr, 0, 1);
while(i < 12) {
Arr[i]= Mth[i]+' has '+dt.daysinMonth()+' days';
dt.setMonth(++i);
}

alert('Days in each Month of '+yr+'\n\t'+Arr.join('\n\t'))

</script>```

• Originally Posted by felgall
At least that post of mine got you thinking about it even if you didn't quite get to the simplest solution.
it’s still considerably shorter than anything I had seen until then. but nevertheless, your leap year solution got me thinking how to tackle this problem in the not-so-obvious way.

@Philipp_M: I also considered extending Date, but left it to the reader (i.e. you) as it should pose no problem to rewrite it.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•