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 5 of 5
  1. #1
    Dat
    Dat is offline
    Regular Coder
    Join Date
    Oct 2007
    Posts
    147
    Thanks
    28
    Thanked 0 Times in 0 Posts

    Correct Day, for Month

    How can I change the day when the user select a month?
    When they select Feb ->29 days, select May -> 31 days.

    Like what facebook does.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    Code:
    // we will suppose that you have a <select> for the year and one for the <month>
    //
    var form = document.YourFormName;
    var yr = form.yearSelect[form.yearSelect.selectedIndex].value; // any valid 4 digit year
    var mn = form.yearSelect[form.monthSelect.selectedIndex].value; // 1 - 12 
    
    var firstOfNextMonth = new Date( yr, mn, 1 ); // in JS month goes 0 to 11, so this works
    var lastOfGivenMonth = new Date( firstOfNextMonth.getTime() - 24 * 60 *60 * 1000 ); 
    
    var lastDayOfGivenMonth = lastOfGivenMonth.getDate();
    ...
    Note: without the year, you can't get the right number of days for February. Though of course it would work for any other month with any year.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Dates, unlike months and weekdays are not 0 based.

    The last day of a month is the zeroth day of the next month.

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

    Code:
    var M= ['Jan','Feb','Mar','Apr','May','Jun','Jul',
    'Aug','Sep','Oct','Nov','Dec'],
    i= 0, y= 2012, A= [], d= new Date(y, 0, 1);
    while(i<12){
    	A[i]= M[i]+' has '+d.daysinMonth()+' days';
    	d.setMonth(++i);
    }
    alert('Days in each Month of '+y+'\n\t'+A.join('\n\t'))
    Last edited by mrhoo; 12-08-2009 at 07:48 AM.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Check out the OO Dropdown Date Picker script from my sig. Scroll down to post #15 for the latest version.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,447
    Thanks
    76
    Thanked 4,372 Times in 4,337 Posts
    Never tried using 0 for last-day-of-prior month. Had a hunch it would work, but hadn't tested it so gave the next-best answer.

    It works with DateSerial() in VB/VBS/Access, by the by, if anybody cares.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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