[javascript] Calculate last day of month in JavaScript

If you provide 0 as the dayValue in Date.setFullYear you get the last day of the previous month:

d = new Date(); d.setFullYear(2008, 11, 0); //  Sun Nov 30 2008

There is reference to this behaviour at mozilla. Is this a reliable cross-browser feature or should I look at alternative methods?

This question is related to javascript date

The answer is


set month you need to date and then set the day to zero ,so month begin in 1 - 31 in date function then get the last day^^

_x000D_
_x000D_
var last = new Date(new Date(new Date().setMonth(7)).setDate(0)).getDate();_x000D_
console.log(last);
_x000D_
_x000D_
_x000D_


const today = new Date();

let beginDate = new Date();

let endDate = new Date();

// fist date of montg

beginDate = new Date(

  `${today.getFullYear()}-${today.getMonth() + 1}-01 00:00:00`

);

// end date of month 

// set next Month first Date

endDate = new Date(

  `${today.getFullYear()}-${today.getMonth() + 2}-01 :23:59:59`

);

// deducting 1 day

endDate.setDate(0);

This one works nicely:

Date.prototype.setToLastDateInMonth = function () {

    this.setDate(1);
    this.setMonth(this.getMonth() + 1);
    this.setDate(this.getDate() - 1);

    return this;
}

function getLastDay(y, m) {
   return 30 + (m <= 7 ? ((m % 2) ? 1 : 0) : (!(m % 2) ? 1 : 0)) - (m == 2) - (m == 2 && y % 4 != 0 || !(y % 100 == 0 && y % 400 == 0)); 
}

My colleague stumbled upon the following which may be an easier solution

function daysInMonth(iMonth, iYear)
{
    return 32 - new Date(iYear, iMonth, 32).getDate();
}

stolen from http://snippets.dzone.com/posts/show/2099


I find this to be the best solution for me. Let the Date object calculate it for you.

var today = new Date();
var lastDayOfMonth = new Date(today.getFullYear(), today.getMonth()+1, 0);

Setting day parameter to 0 means one day less than first day of the month which is last day of the previous month.


I know it's just a matter of semantics, but I ended up using it in this form.

var lastDay = new Date(new Date(2008, 11+1,1) - 1).getDate();
console.log(lastDay);

Since functions are resolved from the inside argument, outward, it works the same.

You can then just replace the year, and month / year with the required details, whether it be from the current date. Or a particular month / year.


This works for me. Will provide last day of given year and month:

var d = new Date(2012,02,0);
var n = d.getDate();
alert(n);

A slight modification to solution provided by lebreeze:

function daysInMonth(iMonth, iYear)
{
    return new Date(iYear, iMonth, 0).getDate();
}

I recently had to do something similar, this is what I came up with:

/**
* Returns a date set to the begining of the month
* 
* @param {Date} myDate 
* @returns {Date}
*/
function beginningOfMonth(myDate){    
  let date = new Date(myDate);
  date.setDate(1)
  date.setHours(0);
  date.setMinutes(0);
  date.setSeconds(0);   
  return date;     
}

/**
 * Returns a date set to the end of the month
 * 
 * @param {Date} myDate 
 * @returns {Date}
 */
function endOfMonth(myDate){
  let date = new Date(myDate);
  date.setMonth(date.getMonth() +1)
  date.setDate(0);
  date.setHours(23);
  date.setMinutes(59);
  date.setSeconds(59);
  return date;
}

Pass it in a date, and it will return a date set to either the beginning of the month, or the end of the month.

The begninngOfMonth function is fairly self-explanatory, but what's going in in the endOfMonth function is that I'm incrementing the month to the next month, and then using setDate(0) to roll back the day to the last day of the previous month which is a part of the setDate spec:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate https://www.w3schools.com/jsref/jsref_setdate.asp

I then set the hour/minutes/seconds to the end of the day, so that if you're using some kind of API that is expecting a date range you'll be able to capture the entirety of that last day. That part might go beyond what the original post is asking for but it could help someone else looking for a similar solution.

Edit: You can also go the extra mile and set milliseconds with setMilliseconds() if you want to be extra precise.


The accepted answer doesn't work for me, I did it as below.

_x000D_
_x000D_
$( function() {
  $( "#datepicker" ).datepicker();
  $('#getLastDateOfMon').on('click', function(){
    var date = $('#datepicker').val();

    // Format 'mm/dd/yy' eg: 12/31/2018
    var parts = date.split("/");

    var lastDateOfMonth = new Date();
                lastDateOfMonth.setFullYear(parts[2]);
                lastDateOfMonth.setMonth(parts[0]);
                lastDateOfMonth.setDate(0);

     alert(lastDateOfMonth.toLocaleDateString());
  });
});
_x000D_
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
 
<p>Date: <input type="text" id="datepicker"></p>
<button id="getLastDateOfMon">Get Last Date of Month </button>
 
 
</body>
</html>
_x000D_
_x000D_
_x000D_


You can get the First and Last Date in the current month by following the code:

var dateNow = new Date();
var firstDate = new Date(dateNow.getFullYear(), dateNow.getMonth(), 1);
var lastDate = new Date(dateNow.getFullYear(), dateNow.getMonth() + 1, 0);

or if you want to format the date in your custom format then you can use moment js

var dateNow= new Date();
var firstDate=moment(new Date(dateNow.getFullYear(),dateNow.getMonth(), 1)).format("DD-MM-YYYY");
var currentDate = moment(new Date()).format("DD-MM-YYYY"); //to get the current date var lastDate = moment(new Date(dateNow.getFullYear(), dateNow.getMonth() + 1, 0)).format("DD-MM-YYYY"); //month last date


I would use an intermediate date with the first day of the next month, and return the date from the previous day:

int_d = new Date(2008, 11+1,1);
d = new Date(int_d - 1);

const today = new Date();

let beginDate = new Date();

let endDate = new Date();

// fist date of montg

beginDate = new Date(

  `${today.getFullYear()}-${today.getMonth() + 1}-01 00:00:00`

);

// end date of month 

// set next Month first Date

endDate = new Date(

  `${today.getFullYear()}-${today.getMonth() + 2}-01 :23:59:59`

);

// deducting 1 day

endDate.setDate(0);

If you need exact end of the month in miliseconds (for example in a timestamp):

_x000D_
_x000D_
d = new Date()_x000D_
console.log(d.toString())_x000D_
d.setDate(1)_x000D_
d.setHours(23, 59, 59, 999)_x000D_
d.setMonth(d.getMonth() + 1)_x000D_
d.setDate(d.getDate() - 1)_x000D_
console.log(d.toString())
_x000D_
_x000D_
_x000D_


Below function gives the last day of the month :

_x000D_
_x000D_
function getLstDayOfMonFnc(date) {_x000D_
  return new Date(date.getFullYear(), date.getMonth(), 0).getDate()_x000D_
}_x000D_
_x000D_
console.log(getLstDayOfMonFnc(new Date(2016, 2, 15))) // Output : 29_x000D_
console.log(getLstDayOfMonFnc(new Date(2017, 2, 15))) // Output : 28_x000D_
console.log(getLstDayOfMonFnc(new Date(2017, 11, 15))) // Output : 30_x000D_
console.log(getLstDayOfMonFnc(new Date(2017, 12, 15))) // Output : 31
_x000D_
_x000D_
_x000D_

Similarly we can get first day of the month :

_x000D_
_x000D_
function getFstDayOfMonFnc(date) {_x000D_
  return new Date(date.getFullYear(), date.getMonth(), 1).getDate()_x000D_
}_x000D_
_x000D_
console.log(getFstDayOfMonFnc(new Date(2016, 2, 15))) // Output : 1
_x000D_
_x000D_
_x000D_


I recently had to do something similar, this is what I came up with:

/**
* Returns a date set to the begining of the month
* 
* @param {Date} myDate 
* @returns {Date}
*/
function beginningOfMonth(myDate){    
  let date = new Date(myDate);
  date.setDate(1)
  date.setHours(0);
  date.setMinutes(0);
  date.setSeconds(0);   
  return date;     
}

/**
 * Returns a date set to the end of the month
 * 
 * @param {Date} myDate 
 * @returns {Date}
 */
function endOfMonth(myDate){
  let date = new Date(myDate);
  date.setMonth(date.getMonth() +1)
  date.setDate(0);
  date.setHours(23);
  date.setMinutes(59);
  date.setSeconds(59);
  return date;
}

Pass it in a date, and it will return a date set to either the beginning of the month, or the end of the month.

The begninngOfMonth function is fairly self-explanatory, but what's going in in the endOfMonth function is that I'm incrementing the month to the next month, and then using setDate(0) to roll back the day to the last day of the previous month which is a part of the setDate spec:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate https://www.w3schools.com/jsref/jsref_setdate.asp

I then set the hour/minutes/seconds to the end of the day, so that if you're using some kind of API that is expecting a date range you'll be able to capture the entirety of that last day. That part might go beyond what the original post is asking for but it could help someone else looking for a similar solution.

Edit: You can also go the extra mile and set milliseconds with setMilliseconds() if you want to be extra precise.


The accepted answer doesn't work for me, I did it as below.

_x000D_
_x000D_
$( function() {
  $( "#datepicker" ).datepicker();
  $('#getLastDateOfMon').on('click', function(){
    var date = $('#datepicker').val();

    // Format 'mm/dd/yy' eg: 12/31/2018
    var parts = date.split("/");

    var lastDateOfMonth = new Date();
                lastDateOfMonth.setFullYear(parts[2]);
                lastDateOfMonth.setMonth(parts[0]);
                lastDateOfMonth.setDate(0);

     alert(lastDateOfMonth.toLocaleDateString());
  });
});
_x000D_
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
 
<p>Date: <input type="text" id="datepicker"></p>
<button id="getLastDateOfMon">Get Last Date of Month </button>
 
 
</body>
</html>
_x000D_
_x000D_
_x000D_


Try this:

function _getEndOfMonth(time_stamp) {
    let time = new Date(time_stamp * 1000);
    let month = time.getMonth() + 1;
    let year = time.getFullYear();
    let day = time.getDate();
    switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            day = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            day = 30;
            break;
        case 2:
            if (_leapyear(year))
                day = 29;
            else
                day = 28;
            break
    }
    let m = moment(`${year}-${month}-${day}`, 'YYYY-MM-DD')
    return m.unix() + constants.DAY - 1;
}

function _leapyear(year) {
    return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0);
}

try this one.

lastDateofTheMonth = new Date(year, month, 0)

example:

new Date(2012, 8, 0)

output:

Date {Fri Aug 31 2012 00:00:00 GMT+0900 (Tokyo Standard Time)}

Here is an answer that conserves GMT and time of the initial date

_x000D_
_x000D_
var date = new Date();_x000D_
_x000D_
var first_date = new Date(date); //Make a copy of the date we want the first and last days from_x000D_
first_date.setUTCDate(1); //Set the day as the first of the month_x000D_
_x000D_
var last_date = new Date(first_date); //Make a copy of the calculated first day_x000D_
last_date.setUTCMonth(last_date.getUTCMonth() + 1); //Add a month_x000D_
last_date.setUTCDate(0); //Set the date to 0, this goes to the last day of the previous month_x000D_
_x000D_
console.log(first_date.toJSON().substring(0, 10), last_date.toJSON().substring(0, 10)); //Log the dates with the format yyyy-mm-dd
_x000D_
_x000D_
_x000D_


My colleague stumbled upon the following which may be an easier solution

function daysInMonth(iMonth, iYear)
{
    return 32 - new Date(iYear, iMonth, 32).getDate();
}

stolen from http://snippets.dzone.com/posts/show/2099


This will give you current month first and last day.

If you need to change 'year' remove d.getFullYear() and set your year.

If you need to change 'month' remove d.getMonth() and set your year.

_x000D_
_x000D_
var d = new Date();_x000D_
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];_x000D_
var fistDayOfMonth = days[(new Date(d.getFullYear(), d.getMonth(), 1).getDay())];_x000D_
 var LastDayOfMonth = days[(new Date(d.getFullYear(), d.getMonth() + 1, 0).getDay())]; _x000D_
console.log("First Day :" + fistDayOfMonth); _x000D_
console.log("Last Day:" + LastDayOfMonth);_x000D_
alert("First Day :" + fistDayOfMonth); _x000D_
alert("Last Day:" + LastDayOfMonth);
_x000D_
_x000D_
_x000D_


set month you need to date and then set the day to zero ,so month begin in 1 - 31 in date function then get the last day^^

_x000D_
_x000D_
var last = new Date(new Date(new Date().setMonth(7)).setDate(0)).getDate();_x000D_
console.log(last);
_x000D_
_x000D_
_x000D_


I would use an intermediate date with the first day of the next month, and return the date from the previous day:

int_d = new Date(2008, 11+1,1);
d = new Date(int_d - 1);

Below function gives the last day of the month :

_x000D_
_x000D_
function getLstDayOfMonFnc(date) {_x000D_
  return new Date(date.getFullYear(), date.getMonth(), 0).getDate()_x000D_
}_x000D_
_x000D_
console.log(getLstDayOfMonFnc(new Date(2016, 2, 15))) // Output : 29_x000D_
console.log(getLstDayOfMonFnc(new Date(2017, 2, 15))) // Output : 28_x000D_
console.log(getLstDayOfMonFnc(new Date(2017, 11, 15))) // Output : 30_x000D_
console.log(getLstDayOfMonFnc(new Date(2017, 12, 15))) // Output : 31
_x000D_
_x000D_
_x000D_

Similarly we can get first day of the month :

_x000D_
_x000D_
function getFstDayOfMonFnc(date) {_x000D_
  return new Date(date.getFullYear(), date.getMonth(), 1).getDate()_x000D_
}_x000D_
_x000D_
console.log(getFstDayOfMonFnc(new Date(2016, 2, 15))) // Output : 1
_x000D_
_x000D_
_x000D_


I would use an intermediate date with the first day of the next month, and return the date from the previous day:

int_d = new Date(2008, 11+1,1);
d = new Date(int_d - 1);

Here is an answer that conserves GMT and time of the initial date

_x000D_
_x000D_
var date = new Date();_x000D_
_x000D_
var first_date = new Date(date); //Make a copy of the date we want the first and last days from_x000D_
first_date.setUTCDate(1); //Set the day as the first of the month_x000D_
_x000D_
var last_date = new Date(first_date); //Make a copy of the calculated first day_x000D_
last_date.setUTCMonth(last_date.getUTCMonth() + 1); //Add a month_x000D_
last_date.setUTCDate(0); //Set the date to 0, this goes to the last day of the previous month_x000D_
_x000D_
console.log(first_date.toJSON().substring(0, 10), last_date.toJSON().substring(0, 10)); //Log the dates with the format yyyy-mm-dd
_x000D_
_x000D_
_x000D_


I would use an intermediate date with the first day of the next month, and return the date from the previous day:

int_d = new Date(2008, 11+1,1);
d = new Date(int_d - 1);

If you need exact end of the month in miliseconds (for example in a timestamp):

_x000D_
_x000D_
d = new Date()_x000D_
console.log(d.toString())_x000D_
d.setDate(1)_x000D_
d.setHours(23, 59, 59, 999)_x000D_
d.setMonth(d.getMonth() + 1)_x000D_
d.setDate(d.getDate() - 1)_x000D_
console.log(d.toString())
_x000D_
_x000D_
_x000D_


You can get the First and Last Date in the current month by following the code:

var dateNow = new Date();
var firstDate = new Date(dateNow.getFullYear(), dateNow.getMonth(), 1);
var lastDate = new Date(dateNow.getFullYear(), dateNow.getMonth() + 1, 0);

or if you want to format the date in your custom format then you can use moment js

var dateNow= new Date();
var firstDate=moment(new Date(dateNow.getFullYear(),dateNow.getMonth(), 1)).format("DD-MM-YYYY");
var currentDate = moment(new Date()).format("DD-MM-YYYY"); //to get the current date var lastDate = moment(new Date(dateNow.getFullYear(), dateNow.getMonth() + 1, 0)).format("DD-MM-YYYY"); //month last date


In computer terms, new Date() and regular expression solutions are slow! If you want a super-fast (and super-cryptic) one-liner, try this one (assuming m is in Jan=1 format). I keep trying different code changes to get the best performance.

My current fastest version:

After looking at this related question Leap year check using bitwise operators (amazing speed) and discovering what the 25 & 15 magic number represented, I have come up with this optimized hybrid of answers:

function getDaysInMonth(m, y) {
    return m===2 ? y & 3 || !(y%25) && y & 15 ? 28 : 29 : 30 + (m+(m>>3)&1);
}

Given the bit-shifting this obviously assumes that your m & y parameters are both integers, as passing numbers as strings would result in weird results.

JSFiddle: http://jsfiddle.net/TrueBlueAussie/H89X3/22/

JSPerf results: http://jsperf.com/days-in-month-head-to-head/5

For some reason, (m+(m>>3)&1) is more efficient than (5546>>m&1) on almost all browsers.

The only real competition for speed is from @GitaarLab, so I have created a head-to-head JSPerf for us to test on: http://jsperf.com/days-in-month-head-to-head/5


It works based on my leap year answer here: javascript to find leap year this answer here Leap year check using bitwise operators (amazing speed) as well as the following binary logic.

A quick lesson in binary months:

If you interpret the index of the desired months (Jan = 1) in binary you will notice that months with 31 days either have bit 3 clear and bit 0 set, or bit 3 set and bit 0 clear.

Jan = 1  = 0001 : 31 days
Feb = 2  = 0010
Mar = 3  = 0011 : 31 days
Apr = 4  = 0100
May = 5  = 0101 : 31 days
Jun = 6  = 0110
Jul = 7  = 0111 : 31 days
Aug = 8  = 1000 : 31 days
Sep = 9  = 1001
Oct = 10 = 1010 : 31 days
Nov = 11 = 1011
Dec = 12 = 1100 : 31 days

That means you can shift the value 3 places with >> 3, XOR the bits with the original ^ m and see if the result is 1 or 0 in bit position 0 using & 1. Note: It turns out + is slightly faster than XOR (^) and (m >> 3) + m gives the same result in bit 0.

JSPerf results: http://jsperf.com/days-in-month-perf-test/6


This one works nicely:

Date.prototype.setToLastDateInMonth = function () {

    this.setDate(1);
    this.setMonth(this.getMonth() + 1);
    this.setDate(this.getDate() - 1);

    return this;
}

This will give you current month first and last day.

If you need to change 'year' remove d.getFullYear() and set your year.

If you need to change 'month' remove d.getMonth() and set your year.

_x000D_
_x000D_
var d = new Date();_x000D_
var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];_x000D_
var fistDayOfMonth = days[(new Date(d.getFullYear(), d.getMonth(), 1).getDay())];_x000D_
 var LastDayOfMonth = days[(new Date(d.getFullYear(), d.getMonth() + 1, 0).getDay())]; _x000D_
console.log("First Day :" + fistDayOfMonth); _x000D_
console.log("Last Day:" + LastDayOfMonth);_x000D_
alert("First Day :" + fistDayOfMonth); _x000D_
alert("Last Day:" + LastDayOfMonth);
_x000D_
_x000D_
_x000D_


In computer terms, new Date() and regular expression solutions are slow! If you want a super-fast (and super-cryptic) one-liner, try this one (assuming m is in Jan=1 format). I keep trying different code changes to get the best performance.

My current fastest version:

After looking at this related question Leap year check using bitwise operators (amazing speed) and discovering what the 25 & 15 magic number represented, I have come up with this optimized hybrid of answers:

function getDaysInMonth(m, y) {
    return m===2 ? y & 3 || !(y%25) && y & 15 ? 28 : 29 : 30 + (m+(m>>3)&1);
}

Given the bit-shifting this obviously assumes that your m & y parameters are both integers, as passing numbers as strings would result in weird results.

JSFiddle: http://jsfiddle.net/TrueBlueAussie/H89X3/22/

JSPerf results: http://jsperf.com/days-in-month-head-to-head/5

For some reason, (m+(m>>3)&1) is more efficient than (5546>>m&1) on almost all browsers.

The only real competition for speed is from @GitaarLab, so I have created a head-to-head JSPerf for us to test on: http://jsperf.com/days-in-month-head-to-head/5


It works based on my leap year answer here: javascript to find leap year this answer here Leap year check using bitwise operators (amazing speed) as well as the following binary logic.

A quick lesson in binary months:

If you interpret the index of the desired months (Jan = 1) in binary you will notice that months with 31 days either have bit 3 clear and bit 0 set, or bit 3 set and bit 0 clear.

Jan = 1  = 0001 : 31 days
Feb = 2  = 0010
Mar = 3  = 0011 : 31 days
Apr = 4  = 0100
May = 5  = 0101 : 31 days
Jun = 6  = 0110
Jul = 7  = 0111 : 31 days
Aug = 8  = 1000 : 31 days
Sep = 9  = 1001
Oct = 10 = 1010 : 31 days
Nov = 11 = 1011
Dec = 12 = 1100 : 31 days

That means you can shift the value 3 places with >> 3, XOR the bits with the original ^ m and see if the result is 1 or 0 in bit position 0 using & 1. Note: It turns out + is slightly faster than XOR (^) and (m >> 3) + m gives the same result in bit 0.

JSPerf results: http://jsperf.com/days-in-month-perf-test/6


try this one.

lastDateofTheMonth = new Date(year, month, 0)

example:

new Date(2012, 8, 0)

output:

Date {Fri Aug 31 2012 00:00:00 GMT+0900 (Tokyo Standard Time)}

I find this to be the best solution for me. Let the Date object calculate it for you.

var today = new Date();
var lastDayOfMonth = new Date(today.getFullYear(), today.getMonth()+1, 0);

Setting day parameter to 0 means one day less than first day of the month which is last day of the previous month.


Try this:

function _getEndOfMonth(time_stamp) {
    let time = new Date(time_stamp * 1000);
    let month = time.getMonth() + 1;
    let year = time.getFullYear();
    let day = time.getDate();
    switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            day = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            day = 30;
            break;
        case 2:
            if (_leapyear(year))
                day = 29;
            else
                day = 28;
            break
    }
    let m = moment(`${year}-${month}-${day}`, 'YYYY-MM-DD')
    return m.unix() + constants.DAY - 1;
}

function _leapyear(year) {
    return (year % 100 === 0) ? (year % 400 === 0) : (year % 4 === 0);
}

I know it's just a matter of semantics, but I ended up using it in this form.

var lastDay = new Date(new Date(2008, 11+1,1) - 1).getDate();
console.log(lastDay);

Since functions are resolved from the inside argument, outward, it works the same.

You can then just replace the year, and month / year with the required details, whether it be from the current date. Or a particular month / year.


function getLastDay(y, m) {
   return 30 + (m <= 7 ? ((m % 2) ? 1 : 0) : (!(m % 2) ? 1 : 0)) - (m == 2) - (m == 2 && y % 4 != 0 || !(y % 100 == 0 && y % 400 == 0)); 
}

This works for me. Will provide last day of given year and month:

var d = new Date(2012,02,0);
var n = d.getDate();
alert(n);

A slight modification to solution provided by lebreeze:

function daysInMonth(iMonth, iYear)
{
    return new Date(iYear, iMonth, 0).getDate();
}