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