www.webdeveloper.com
Page 15 of 15 FirstFirst ... 5131415
Results 211 to 221 of 221

Thread: Here are 2 prototype-functions I created.

  1. #211
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,271
    Or with an unique line
    Code:
    Date.prototype.numberOfDays=function(){return new Date(this.getFullYear(),this.getMonth()+1,0).getDate()}

  2. #212
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434

    Lightbulb

    Quote Originally Posted by 007Julien View Post
    Or with an unique line
    Code:
    Date.prototype.numberOfDays=function(){return new Date(this.getFullYear(),this.getMonth()+1,0).getDate()}
    For the beginners in the crowd, I always like to include an example of the use.
    I also took the liberty of renaming it to be more descriptive of what it does.

    Code:
    <script type="text/javascript">
    Date.prototype.DaysInMonth=function() {
      return new Date(this.getFullYear(),this.getMonth()+1,0).getDate();
    }
    
    var today = new Date();
    var Months = ['Jan','Feb','Mar','Apr','May','Jun',
                  'Jul','Aug','Sep','Oct','Nov','Dec'];
    var tmp;
    var str = '';
    for (var i=0; i<12; i++) { 
      tmp = new Date(2011,i,1);
      str += Months[i]+' : '+tmp.DaysInMonth()+'\n';
    }
    alert(today.DaysInMonth()+'\n\n'+str);
    </script>

  3. #213
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Much Simpler
    Code:
    Date.prototype.DaysInMonthOfYear=function(yr,mth){return 32 - new Date(yr, mth, 32).getDate();}
    obj = new Date();
    alert( obj.HelloAreWePayingAttention( 2011 , 1 ) );
    Returns the result 28
    Last edited by JunkMale; 11-23-2011 at 03:15 AM.

  4. #214
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434

    Arrow

    Personally, I liked your first version better because you could get the days in current month of year
    by default without specifying in the function.
    Code:
    <script type="text/javascript">
    Date.prototype.DaysInMonthOfYear=function() {
      return new Date(this.getFullYear(),this.getMonth()+1,0).getDate();
    }
    
    // demonstration of use
    var Months = ['Jan','Feb','Mar','Apr','May','Jun',
                  'Jul','Aug','Sep','Oct','Nov','Dec'];
    var str = 'This month: '+(new Date()).DaysInMonthOfYear()+'\n\n';
    for (var i=0; i<12; i++) { 
      str += Months[i]+' : '+(new Date(2011,i,1)).DaysInMonthOfYear()+'\n';
    }
    alert(str);
    </script>

  5. #215
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Ahhh but the problem with year only functions is that you are stuck to one year.

    If you want to get the month name in the fashion you have, then...

    Code:
    str = new Date().toGMTString();
    monthName = str.split(" ")[2];
    alert( monthName );
    Might interest you.

  6. #216
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434

    Lightbulb

    Quote Originally Posted by JunkMale View Post
    Ahhh but the problem with year only functions is that you are stuck to one year.

    If you want to get the month name in the fashion you have, then...

    Code:
    str = new Date().toGMTString();
    monthName = str.split(" ")[2];
    alert( monthName );
    Might interest you.
    I'm not sure of the problem you see with the year.
    Below is a test to show that the year is not stuck (note leap year of 2012 results)
    and that the default condition is for the current year using your original code.
    Code:
    <html>
    <head>
    <script type="text/javascript">
    Date.prototype.DaysInMonthOfYear=function() {
      return new Date(this.getFullYear(),this.getMonth()+1,0).getDate();
    }
    
    // demonstration of use
    var Months = ['Jan','Feb','Mar','Apr','May','Jun',
                  'Jul','Aug','Sep','Oct','Nov','Dec'];
    function showYear(year) {
      var str = '';
      for (var i=0; i<12; i++) { 
        str += Months[i]+' : '+(new Date(year,i,1)).DaysInMonthOfYear()+'<br>';
      }
      return str;
    }
    
    window.onload = function() {
     var str = new Date().toGMTString().split(' ')[2];
         str = 'This month: '+str+' has '+(new Date()).DaysInMonthOfYear()+' days<p>';
     document.getElementById('test').innerHTML += str;
     document.getElementById('test').innerHTML += '<h3>2010</h3>'+showYear('2010');
     document.getElementById('test').innerHTML += '<h3>2011</h3>'+showYear('2011');
     document.getElementById('test').innerHTML += '<h3>2012</h3>'+showYear('2012');
     document.getElementById('test').innerHTML += '<h3>2013</h3>'+showYear('2013');
    }
    </script>
    </head>
    <body>
    <div id="test"></div>
    </body>
    </html>

  7. #217
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    You misunderstand me, you can only obtain the current days information from the function you showing. I am showing an alternative method which allows the user the flexibility to return the right number of days for any given year and month.

  8. #218
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434

    Question

    Quote Originally Posted by JunkMale View Post
    You misunderstand me, you can only obtain the current days information from the function you showing. I am showing an alternative method which allows the user the flexibility to return the right number of days for any given year and month.
    Well, that is obvious!

    The example I posted does show for any given year and month.
    What is the error there when I'm using your code?

    Provide an example of what you are talking about please.

  9. #219
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    You're definitely confusing the issue as for the functions... if I wanted your function to tell me the number of days in February 2032 I would have to wait until 1st February 2032 for it to tell me?
    Code:
    Date.prototype.DaysInMonthOfYear=function() {
      return new Date(this.getFullYear(),this.getMonth()+1,0).getDate();
    }
    If you used
    Code:
    Date.prototype.DaysInMonthOfYear=function(yr,mth){return 32 - new Date(yr, mth, 32).getDate();}
    and passed the parameters as alert( new Date().DaysInMonthOfYear( 2032 , 1 ) ); // 1= Feb

    your result would be... 29 and checking my Outlook Calendar Feb of 2032 is indeed a leap year.

    As for errors? What errors? I think your confusing me with someone else.

  10. #220
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,434

    Red face

    Quote Originally Posted by JunkMale View Post
    You're definitely confusing the issue as for the functions... if I wanted your function to tell me the number of days in February 2032 I would have to wait until 1st February 2032 for it to tell me? ... checking my Outlook Calendar Feb of 2032 is indeed a leap year.

    As for errors? What errors? I think your confusing me with someone else.
    You are right, I am confusing you with someone else.

    However the code I am using does not depend on the current year only.
    I can use the current year by default with new Date()
    or a particular year with the desired specification as in the following.
    Code:
    <script type="text/javascript">
    Date.prototype.DaysInMonthOfYear=function() {
      return new Date(this.getFullYear(),this.getMonth()+1,0).getDate();
    }
    
    alert('Current month: '+(new Date()).DaysInMonthOfYear());
    alert('Feb of 2032: '+(new Date(2032,1,1)).DaysInMonthOfYear());
    
    </script>
    Sorry for my confusion. Looking back I was originally commenting on '000Julien's code and then I did not recognize the poster had changed to you as 'JunkMale' (My bad)

  11. #221
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    Gotcha with the use now, much clearer and I can see more obvious now.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles