/    Sign up×
Community /Pin to ProfileBookmark

Get Number of Days In Month Function

Does anyone have a function that returns the number of days in a specific month in a specific year?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@khalidali63Mar 11.2003 — getting days of a month is not problem

create an array for 12 months with their respective number of days

getting days for a specif month could get a tat bit tricky for month of february,where you have to incorporate some calculations for leap years.

I hhope this guides you.

Cheers

Khalid
Copy linkTweet thisAlerts:
@davidcholleyauthorMar 11.2003 — I was having problems adapting the original code referenced, however I was able to sort out the problems. I'm not happy the I don't fully understand the logic, however I have verified that the function does return the correct number of days across non-leap & leap years.

function getDaysInMonth()

{

<!-- Original : Ben McFarlin ([email protected]) -->

<!-- Web Site : http://sites.netscape.net/mcfarlin -->

<!-- Modified By: David Holley ([email protected]) -->

<!-- Web Site : http://www.gatewayorlando.com -->

<!-- This script and many more are available free online at -->

<!-- The JavaScript Source!! http://javascript.internet.com -->

var startDate = new Date(document.frmSelectMonth.year.value, document.frmSelectMonth.month.value, 1);

timeDifference = startDate - 86400000;

var result = new Date(timeDifference);

return result.getDate();

}
Copy linkTweet thisAlerts:
@davidcholleyauthorMar 11.2003 — Actually I just got a better picture of how the code works...

The option list referenced uses the actual month ordinal positions instead of the JavaScript ordinal positions. (1-12 versus 0-11).

The startDate value is actually the first day of the month that follows the selected month. Once the DATE object is created, the total number of milliseconds in a day (86400000)is subtracted from the date to obtain the day prior to the first day of the month. Then the getDate() function return the day of the month which happens to represent the number of days in the month.

In short, the logic is

...go to the first day of the next month

...go back 1 day

...get the date

David H.

(Cannot take credit for the logic though)
Copy linkTweet thisAlerts:
@gil_davisMar 11.2003 — You should try December. I believe your routine will fail, because it is not considering a year wrap. Here is a snip of a routine I use to calculate days in a month (among other things): var _year = val.year.value;
var _first = new Date(val.year.value, val.month.selectedIndex, 1);
var _month = val.month.options[val.month.selectedIndex].text;
var _title = _month + ", " + _year;
var _start = _first.getDay();
var _nmo = (val.month.selectedIndex + 1) % 12;
var _nyr = (_nmo == 0) ? Number(_year) + 1 : Number(_year);
var _end = new Date(_nyr, _nmo, 1);
var _mlen = Math.round((_end - _first) / 86400000);
To see the code at work:

http://gil.davis.home.att.net/calgen.htm
Copy linkTweet thisAlerts:
@davidcholleyauthorMar 11.2003 — ---Message Deleted---

Information out of date

David H
Copy linkTweet thisAlerts:
@davidcholleyauthorMar 19.2003 — ...and the final (99.3%) version in action

http://www.gatewayorlando.com/content/reservationrequest.asp

I was having problems getting the calendar to automatically load to the current month due to another script on the page (the onLoad event for the body tag would not fire) which is why the calendar visiblity changes. I'm about to shift the validation to server-side due to the amount of code on the page so I should be able show the calendar when the page loads.
×

Success!

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