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 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2012
    Location
    in the uk.
    Posts
    99
    Thanks
    2
    Thanked 6 Times in 4 Posts

    First 'x' day in the month & Last 'x' day in the month

    A prototype that determines the first day of the month that a given day of week is found.

    No frills, just nuts n bolts.

    Code:
    // routine for finding the first specified day of week in any given month of a year.
    Date.prototype.firstInMonth = function(dow,month,year){
    		var month = month-1 || this.getMonth();  // we are expecting 1=Jan, 2=Feb, so -1 from it.
    		var year = year || this.getFullYear();
    		for(dayNo=1; dayNo<9; dayNo++){
    			futureDate = new Date(year, month , dayNo);
    			// we only check the first 9 days, if we don't find anything then we have a problem!
    			if( futureDate.getDay()==dow ) return futureDate.getDate(); // return the "Day number"
    			}
    return false; // we return something because we have to!
    }
    alert( new Date().firstInMonth(5,7,2014) );// first Friday is 4th July 2014
    Those needing more output can substitute the line
    Code:
    if( futureDate.getDay()==dow ) return futureDate.getDate(); // return the "Day number"
    for something like
    Code:
    if( futureDate.getDay()==dow ) return futureDate.toString().slice(0,16); // return a date string
    Can't quite put my finger on it but using .toGMTString or toUTCString produces a date string for the day before the desired result.

    Tested in Opera, Chrome, Fire Fox, MSIE8 and as expected, MSIE's output differs from the others! But it still output.

    Where the others all out put Fri Jul 4 2014, MSIE outputs Fri Jul 4 00:00: when using the toString() method.

    The original script output a date using this as the line
    Code:
    if( futureDate.getDay()==dow ) return [dayNo,month+1,year].join("/");
    that output a dd/mm/yyyy format date which can easily be picked apart to be reused in a new Date() object or pumped directly in to another function.

    However, if I
    Code:
    if( futureDate.getDay()==dow ) return new Date( [ this.getFullYear(),this.getMonth()+1,dayNo].join("/") ).toGMTString();
    then the desired output is given...

    AND....
    Code:
    // routine for finding the last specified day of week in any given month of a year.
    Date.prototype.lastInMonth = function(dow,month,year){
    		var month = month-1 || this.getMonth();  // we are expecting 1=Jan, 2=Feb
    		var year = year || this.getFullYear();
    		var dim = new Date( year , month+1, 0).getDate();
    		for(dayNo=dim; dayNo>(dim-9); dayNo--){
    			futureDate = new Date(year, month , dayNo);
    			// we only check the last 9 days, if we don't find anything then we have a problem!
    			if( futureDate.getDay()==dow ) return futureDate.getDate(); // return the "Day number"
    			}
    return false; // we return something because we have to!
    }
    alert( new Date().lastInMonth(5,7) );// last Friday in month = 27 (27/7/2012)
    Again, can be modified to add more output.
    Last edited by Cremator; 02-05-2012 at 03:48 AM. Reason: Edited it!

  • #2
    New Coder
    Join Date
    Jan 2012
    Location
    in the uk.
    Posts
    99
    Thanks
    2
    Thanked 6 Times in 4 Posts
    Alternate version which uses the date object packed with a date ref
    Code:
    Date.prototype.firstInMonth = function(dow){
    		dow = dow || 0;
    		for(dayNo=1; dayNo<9; dayNo++){
    			futureDate = new Date(this.getFullYear(), this.getMonth() , dayNo);
    			// we only check the first 9 days, if we don't find anything then we have a problem!
    			if( futureDate.getDay()==dow ) return [dayNo, this.getMonth()+1,this.getFullYear()].join("/");
    			}
    return false; // we return something because we have to!
    }
    alert( new Date(2012,1,1).firstInMonth(5) );// first Friday
    alert( new Date("1 Feb 2012").firstInMonth(5) );

  • #3
    New Coder
    Join Date
    Jan 2012
    Location
    in the uk.
    Posts
    99
    Thanks
    2
    Thanked 6 Times in 4 Posts
    This might prove useful as it returns the day number of the day of week following that date, if dow happens to fall on that date at the start it should return that date instead.

    Code:
    Date.prototype.getNextDow = function(dow){
    		dow = dow || 0;
    		// finds first dow from day in date object in the month set in the date object and likewise the year.
    		for(dayNo=this.getDate(); (this.getDate()+9)>dayNo; dayNo++){
    			futureDate = new Date(this.getFullYear(), this.getMonth() , dayNo);
    			// we only check forward 9 days, if we don't find anything then we have a problem!
    			if( futureDate.getDay()==dow ) return futureDate.getDate(); //,futureDate.getMonth()+1,futureDate.getFullYear()].join("/");// return date string
    			}
    return false; // we return something because we have to!
    }
    alert( new Date(2012,1,5).getNextDow(5) );// first Friday on or after thr 5th Feb
    alert( new Date("5 Feb 2012").getNextDow(5) );
    The output is 10 = 10/2/2012

    Can be easily modified to give different output.
    Last edited by Cremator; 02-06-2012 at 01:04 PM.


  •  

    Posting Permissions

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