Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

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

You are here: irt.org | FAQ | JavaScript | Date | Q1688 [ previous next ]

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)?'

©2018 Martin Webb