Location: JavaScript

JavaScript function for getting the last day in month

JavaScript function for getting the last day in month
Sometimes you need to know the last day in month of a given month.
This is a nice JavaScript Solution to this case.

function getDaysInMonth(aDate){
   // returns the last day of a given month
    var m = new Number(aDate.getMonth());
    var y = new Number(aDate.getYear());

    var tmpDate = new Date(y, m, 28);
    var checkMonth = tmpDate.getMonth();
    var lastDay = 27;

    while(lastDay <= 31){
        temp = tmpDate.setDate(lastDay + 1);
        if(checkMonth != tmpDate.getMonth())
            break;
        lastDay++
    }
    return lastDay;
}

Rate:  Be the first to Rank

Add To Facebook Share on Twitter Add To Google Bookmarks Add To Linkedin Add To Del.icio.us Add To digg Add To Stumble Upon Add To Yahoo My Web Add To Technorati

Surfers Feedbacks

01. Thanks! that is exactly what i was looking for... / nick (08/03/2010)