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 6 of 6
  1. #1
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,291
    Thanks
    13
    Thanked 345 Times in 341 Posts

    How many Days in a Month

    based on felgall’s idea:
    PHP Code:
    function daysInAMonth(monthyear)
    {
      return (
    32 - new Date(yearmonth-132).getDate());

    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,969
    Thanks
    56
    Thanked 557 Times in 554 Posts
    why not just
    Code:
    function DaysInMonth(yr,mth) { 
    return new Date(yr,mth,0).getDate(); 
    }
    ?

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,291
    Thanks
    13
    Thanked 345 Times in 341 Posts
    interesting, never thought to use underflow.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Dormilich View Post
    At least that post of mine got you thinking about it even if you didn't quite get to the simplest solution.
    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.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Just for the record:-

    Code:
    <script type = "text/javascript">
    
    Date.prototype.daysinMonth=function(){
    var d = new Date(this.getFullYear(), this.getMonth()+1, 0);
    return d.getDate();
    }
    
    var d = new Date();
    alert("Number of days in this month:- " + d.daysinMonth());
    
    var Mth = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
    var i = 0;
    var yr = 2012;
    var Arr = [];
    var dt = new Date(yr, 0, 1);
    while(i < 12) {
    Arr[i]= Mth[i]+' has '+dt.daysinMonth()+' days';
    dt.setMonth(++i);
    }
    
    alert('Days in each Month of '+yr+'\n\t'+Arr.join('\n\t'))
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,291
    Thanks
    13
    Thanked 345 Times in 341 Posts
    Quote Originally Posted by felgall View Post
    At least that post of mine got you thinking about it even if you didn't quite get to the simplest solution.
    it’s still considerably shorter than anything I had seen until then. but nevertheless, your leap year solution got me thinking how to tackle this problem in the not-so-obvious way.

    @Philipp_M: I also considered extending Date, but left it to the reader (i.e. you) as it should pose no problem to rewrite it.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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