Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

Q1688 How do I find the number of days in a month (or last day of a month)?

irt.org | Knowledge Base | JavaScript | Date | Q1688 [ previous next ]

Q1688 How do I find the number of days in a month (or last day of a month)?

Simply hold the number of days in each month in two arrays, one for non-leap years, and another for leap years:

<script language="JavaScript"><!--
var daysofmonth   = new makeArray( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var daysofmonthLY = new makeArray( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

function LeapYear(year) {
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}

function y2k(number)    { return (number < 1000) ? number + 1900 : number; }

var today = new Date();
var year = y2k(today.getYear());

if (LeapYear(year) {
  daysofmonth = daysofmonthLY;
}

alert('February ' + year + ' has ' + daysofmonth[1] + ' days');
//--></script>

Feedback on 'Q1688 How do I find the number of days in a month (or last day of a month)?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 30th March 2008. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2008 irt.org, All Rights Reserved.