davidcholley
03-10-2003, 06:50 PM
Does anyone have a function that returns the number of days in a specific month in a specific year?
|
Click to See Complete Forum and Search --> : Get Number of Days In Month Function davidcholley 03-10-2003, 06:50 PM Does anyone have a function that returns the number of days in a specific month in a specific year? khalidali63 03-10-2003, 07:05 PM 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 davidcholley 03-10-2003, 08:38 PM 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 (mcfarlin@netscape.net) --> <!-- Web Site : http://sites.netscape.net/mcfarlin --> <!-- Modified By: David Holley (davidcholley@netscape.net) --> <!-- 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(); } davidcholley 03-10-2003, 09:06 PM 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) gil davis 03-11-2003, 05:40 AM 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 davidcholley 03-11-2003, 09:26 AM ---Message Deleted--- Information out of date David H davidcholley 03-18-2003, 07:01 PM ...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. webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |