/    Sign up×
Community /Pin to ProfileBookmark

Trying to compute number of days for a given month

I know I’m probably missing something obvious, but I can’t see why this doesn’t work…..

// Constructor
function calendar(id, d){
this.id = id;
this.dateObject = d;
this.write = writeCalendar;
this.month = d.getMonth();
this.date = d.getDate();
this.day = d.getDay();
this.year = d.getFullYear();
[B]this.length = getLength();[/B]

this.getFormattedDate = getFormattedDate;
d.setDate(1);
this.firstDay = d.getDay();
d.setDate(this.date);
}

[B]function getLength()[/B]{
switch(this.month){
case 1:
if ((this.dateObject.getFullYear()%4==0 &&
this.dateObject.getFullYear()%100!=0) ||
this.dateObject.getFullYear()%400==0)
return 29; // leap year
else
return 28;
case 3:
return 30;
case 5:
return 30;
case 8:
return 30;
case 10:
return 30;
default:
return 31;
}
}

My getLength function is returning 31 for April and I can’t figure out why this doesn’t work. 0 = Jan, so 3 should = April….Can anyone tell me what I’m doing wrong here ??? Thanks. ?

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@liezicaApr 12.2005 — Have you tried to alert (this.month)? Does it give you what you expect it to be (3 for april I assume) Apparently that is where your problem must be;

Or maybe you should call the function like this:

this.length = getLength(this.month);

function getLength(this.month){...

I can't see any other problems.
Copy linkTweet thisAlerts:
@snyder2005authorApr 12.2005 — Yes, I did add an alert to display the month just prior to calling the getLength function and it displays '3'. I'll try calling the function the way you suggested and see if that makes a difference. Thanks for your help !
Copy linkTweet thisAlerts:
@Orc_ScorcherApr 12.2005 — The way you're calling getLength, |this| will not be bound to your date object.

Change this line

this.length = getLength();

to

this.length = getLength.call(this);

And a shorter way to get the number of days in a month:function getLength() {
var _
return (_ = new Date(this), _.setDate(32), 32 - _.getDate())
}
Copy linkTweet thisAlerts:
@A1ien51Apr 12.2005 — I personally would do this to find the number of days in the month. No calculations of leap year required with this method...

<i>
</i>&lt;script type="text/javascript"&gt;

Date.prototype.DaysCount = function(){
var dateNew = new Date((this.getMonth()+2) + "/0/" + this.getFullYear());
return dateNew.getDate();
}

var today = new Date();

var numDays = today.DaysCount();
alert("There are " + numDays + " days!");

&lt;/script&gt;


Eric
Copy linkTweet thisAlerts:
@snyder2005authorApr 12.2005 — Thanks ORC SCORCHER, changing the call to getLength, worked great ! I tried your suggestion, for a more elegant way of computing the number of days in month, but the getLength function returns 'NaN'.... Can you see something I'm doing wrong ?

function calendar(id, d){

this.id = id;

this.dateObject = d;

this.write = writeCalendar;

this.month = d.getMonth();

this.date = d.getDate();

this.day = d.getDay();

this.year = d.getFullYear();

this.length = getLength.call(this);

alert(this.length);

this.getFormattedDate = getFormattedDate;

this.getFormattedDate2 = getFormattedDate2;

// get the first day of the month's day

d.setDate(1);

this.firstDay = d.getDay();

// then reset the date object to the correct date

d.setDate(this.date);

}

function getLength(){

var nbrDays;

return(nbrDays = new Date(this), nbrDays.setDate(32), 32 - nbrDays.getDate())

}
Copy linkTweet thisAlerts:
@snyder2005authorApr 12.2005 — Thanks for the suggestion, Alien 51, I'll try to incorporate this. I always like to reduce lines of code.
Copy linkTweet thisAlerts:
@Orc_ScorcherApr 12.2005 — The problem is my function requires the date object, not the calendar object as its |this|, so you'd have to call it as getLength.call(d) or getLength.call(this.dateObject)
Copy linkTweet thisAlerts:
@snyder2005authorApr 12.2005 — Duh....I'm blind, thanks for all your help....works great now !
Copy linkTweet thisAlerts:
@CharlesApr 12.2005 — &lt;script type="text/javascript"&gt;
&lt;!--
Date.prototype.getDaysInMonth = function() {return new Date (this.getFullYear(), this.getMonth() + 1, 0).getDate()}

alert (new Date().getDaysInMonth())
// --&gt;
&lt;/script&gt;
Copy linkTweet thisAlerts:
@CharlesApr 12.2005 — &lt;script type="text/javascript"&gt;
&lt;!--
Date.getDaysInMonth = function(year, monthIndex) {return new Date (year, monthIndex + 1, 0).getDate()}

alert (Date.getDaysInMonth(2005, 0))
// --&gt;
&lt;/script&gt;
×

Success!

Help @snyder2005 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.17,
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,
)...