Date.prototype.numberOfDays=function(){return new Date(this.getFullYear(),this.getMonth()+1,0).getDate()}
For the beginners in the crowd, I always like to include an example of the use.
I also took the liberty of renaming it to be more descriptive of what it does.
Code:
<script type="text/javascript">
Date.prototype.DaysInMonth=function() {
return new Date(this.getFullYear(),this.getMonth()+1,0).getDate();
}
var today = new Date();
var Months = ['Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'];
var tmp;
var str = '';
for (var i=0; i<12; i++) {
tmp = new Date(2011,i,1);
str += Months[i]+' : '+tmp.DaysInMonth()+'\n';
}
alert(today.DaysInMonth()+'\n\n'+str);
</script>
Date.prototype.DaysInMonthOfYear=function(yr,mth){return 32 - new Date(yr, mth, 32).getDate();}
obj = new Date();
alert( obj.HelloAreWePayingAttention( 2011 , 1 ) );
Returns the result 28
Last edited by JunkMale; 11-23-2011 at 02:15 AM.
We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
Code:
Youre = {
STILL_not_getting_it:function(){
alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
},
MissingThePoint:function(msg){
alert("You're missing the point. " + msg);
}
}
Youre.STILL_not_getting_it();
We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
Code:
Youre = {
STILL_not_getting_it:function(){
alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
},
MissingThePoint:function(msg){
alert("You're missing the point. " + msg);
}
}
Youre.STILL_not_getting_it();
I'm not sure of the problem you see with the year.
Below is a test to show that the year is not stuck (note leap year of 2012 results)
and that the default condition is for the current year using your original code.
Code:
<html>
<head>
<script type="text/javascript">
Date.prototype.DaysInMonthOfYear=function() {
return new Date(this.getFullYear(),this.getMonth()+1,0).getDate();
}
// demonstration of use
var Months = ['Jan','Feb','Mar','Apr','May','Jun',
'Jul','Aug','Sep','Oct','Nov','Dec'];
function showYear(year) {
var str = '';
for (var i=0; i<12; i++) {
str += Months[i]+' : '+(new Date(year,i,1)).DaysInMonthOfYear()+'<br>';
}
return str;
}
window.onload = function() {
var str = new Date().toGMTString().split(' ')[2];
str = 'This month: '+str+' has '+(new Date()).DaysInMonthOfYear()+' days<p>';
document.getElementById('test').innerHTML += str;
document.getElementById('test').innerHTML += '<h3>2010</h3>'+showYear('2010');
document.getElementById('test').innerHTML += '<h3>2011</h3>'+showYear('2011');
document.getElementById('test').innerHTML += '<h3>2012</h3>'+showYear('2012');
document.getElementById('test').innerHTML += '<h3>2013</h3>'+showYear('2013');
}
</script>
</head>
<body>
<div id="test"></div>
</body>
</html>
You misunderstand me, you can only obtain the current days information from the function you showing. I am showing an alternative method which allows the user the flexibility to return the right number of days for any given year and month.
We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
Code:
Youre = {
STILL_not_getting_it:function(){
alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
},
MissingThePoint:function(msg){
alert("You're missing the point. " + msg);
}
}
Youre.STILL_not_getting_it();
You misunderstand me, you can only obtain the current days information from the function you showing. I am showing an alternative method which allows the user the flexibility to return the right number of days for any given year and month.
Well, that is obvious!
The example I posted does show for any given year and month.
What is the error there when I'm using your code?
Provide an example of what you are talking about please.
You're definitely confusing the issue as for the functions... if I wanted your function to tell me the number of days in February 2032 I would have to wait until 1st February 2032 for it to tell me?
Code:
Date.prototype.DaysInMonthOfYear=function() {
return new Date(this.getFullYear(),this.getMonth()+1,0).getDate();
}
If you used
Code:
Date.prototype.DaysInMonthOfYear=function(yr,mth){return 32 - new Date(yr, mth, 32).getDate();}
and passed the parameters as alert( new Date().DaysInMonthOfYear( 2032 , 1 ) ); // 1= Feb
your result would be... 29 and checking my Outlook Calendar Feb of 2032 is indeed a leap year.
As for errors? What errors? I think your confusing me with someone else.
We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
Code:
Youre = {
STILL_not_getting_it:function(){
alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
},
MissingThePoint:function(msg){
alert("You're missing the point. " + msg);
}
}
Youre.STILL_not_getting_it();
You're definitely confusing the issue as for the functions... if I wanted your function to tell me the number of days in February 2032 I would have to wait until 1st February 2032 for it to tell me? ... checking my Outlook Calendar Feb of 2032 is indeed a leap year.
As for errors? What errors? I think your confusing me with someone else.
You are right, I am confusing you with someone else.
However the code I am using does not depend on the current year only.
I can use the current year by default with new Date()
or a particular year with the desired specification as in the following.
Code:
<script type="text/javascript">
Date.prototype.DaysInMonthOfYear=function() {
return new Date(this.getFullYear(),this.getMonth()+1,0).getDate();
}
alert('Current month: '+(new Date()).DaysInMonthOfYear());
alert('Feb of 2032: '+(new Date(2032,1,1)).DaysInMonthOfYear());
</script>
Sorry for my confusion. Looking back I was originally commenting on '000Julien's code and then I did not recognize the poster had changed to you as 'JunkMale' (My bad)
Gotcha with the use now, much clearer and I can see more obvious now.
We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
Code:
Youre = {
STILL_not_getting_it:function(){
alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
},
MissingThePoint:function(msg){
alert("You're missing the point. " + msg);
}
}
Youre.STILL_not_getting_it();
Bookmarks