...

View Full Version : First 'x' day in the month & Last 'x' day in the month



Cremator
02-05-2012, 04:09 AM
A prototype that determines the first day of the month that a given day of week is found.

No frills, just nuts n bolts.


// 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
if( futureDate.getDay()==dow ) return futureDate.getDate(); // return the "Day number" for something like
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
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
if( futureDate.getDay()==dow ) return new Date( [ this.getFullYear(),this.getMonth()+1,dayNo].join("/") ).toGMTString(); then the desired output is given...

AND....

// 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.

Cremator
02-05-2012, 04:20 AM
Alternate version which uses the date object packed with a date ref

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) );

Cremator
02-05-2012, 05:48 AM
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.


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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum