Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2012
    Location
    in the uk.
    Posts
    99
    Thanks
    2
    Thanked 6 Times in 4 Posts

    Get Days in a month

    Snippet to get the number of days,
    Code:
    Date.prototype.daysInMonth=function(mth,yr){ 
    yr = yr || this.getFullYear();
    mth = mth || this.getMonth();
    return 32 - new Date(yr, mth, 32).getDate();
    }
    alert( new Date().daysInMonth() );
    pass month & year to get the days in a month for that year.
    pass month to get the number of days in the month in the current year
    pass no parameters to get the number of days in the current month in the current year.

    or

    Code:
    Date.prototype.daysInMonthPartRFC=function(partRfc){ 
    partRfc = (partRfc)? partRfc+"/"+32  : [this.getFullYear(),this.getMonth(),32].join("/");
    return 32 - new Date(partRfc).getDate();
    }
    alert( new Date().daysInMonthPartRFC("2012/0") );
    pass a part date string made up of the year and javascript month separated by a slash or pass no argument to get days in current month in current year.

    months being passed will be JavaScript months, 0 to 11.
    Last edited by Cremator; 01-29-2012 at 12:02 AM.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    The easiest way to get the number of days in a month using JavaScript is to set the date to the zeroth day of the following month then return the day. No need to mess around with 32 or subtraction.

    Code:
    Date.prototype.daysInMonth=function(mth,yr){ 
    yr = yr || this.getFullYear();
    mth = mth || this.getMonth();
    return new Date(yr, mth+1, 0).getDate();
    }
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    New Coder
    Join Date
    Jan 2012
    Location
    in the uk.
    Posts
    99
    Thanks
    2
    Thanked 6 Times in 4 Posts
    Yep I know, I have that prototype too.

    Code:
    Date.prototype.daysInThisMonthOfThisYear=function() {
      return new Date(this.getFullYear(),this.getMonth()+1,0).getDate();
    }
    
    alert( new Date().daysInThisMonthOfThisYear() );


    Added:
    Also modded this...
    Code:
    Date.prototype.daysInThisMonthOfThisYearPartRFC=function(partRfc){ 
    partRfc = (partRfc)? partRfc+"/0"  : [this.getFullYear(),this.getMonth()+1,0].join("/");
    return new Date(partRfc).getDate();
    }
    alert( new Date().daysInThisMonthOfThisYearPartRFC("2012/1") );
    Last edited by Cremator; 01-29-2012 at 10:55 AM. Reason: Added the part Added:

  • #4
    Regular Coder
    Join Date
    Sep 2010
    Location
    Far far away
    Posts
    122
    Thanks
    0
    Thanked 16 Times in 16 Posts
    Some time ago I extended for myself the Date object with the same methods. Those are:

    Code:
    Date.prototype.isLeapYear = function()
    {
    	var y = this.getFullYear();
    	return y % 4 == 0 && y % 100 != 0 || y % 400 == 0;
    };
    
    Date.prototype.getDaysInMonth = function()
    {
    	return (new Date(this.getFullYear(), this.getMonth() + 1, 0)).getDate();
    };
    
    Date.prototype.getDaysInYear = function()
    {
    	return this.isLeapYear() ? 366 : 365;
    };
    
    Date.prototype.getFirstDay = function()
    {
    	return (new Date(this.getFullYear(), this.getMonth(), 1)).getDay();
    };
    
    Date.prototype.getLastDay = function()
    {
    	return (new Date(this.getFullYear(), this.getMonth() + 1, 0)).getDay();
    };
    There are another methods that I can public here if it will be interesting.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •