On Sep 5, 11:57 pm, J_Mo <
p...@iamphiljames.co.uk> wrote:
> Hi,
>
> I think this will be an easy one... How do you return Last Friday of
> this Month??
Have a play with stuff like this.
// Return a date object set to last day of supplied month
// and year. Months are 1 to 12 for Jan to Dec.
// If no year supplied, current year is used.
// If no month supplied, current month is used.
function getLastDayOfMonth(monthNumber, year) {
var d = new Date();
d.setHours(0, 0, 0, 0);
if (monthNumber) {
if (year) {
d.setFullYear(year, monthNumber, 0);
} else {
d.setMonth(monthNumber, 0);
}
} else {
d.setMonth(d.getMonth() + 1, 0);
}
return d;
}
// Return date object set to last week day (Monday to
// Friday inclusive).
// Uses getLastDayOfMonth()
function getLastWeekDayOfMonth(monthNumber, year) {
var d = getLastDayOfMonth(monthNumber, year);
while (d.getDay() == 0 || d.getDay() == 6) {
d.setDate(d.getDate() - 1);
}
return d;
}
// Return date object set to last Friday of month.
// Uses getLastDayOfMonth()
function getLastFridayOfMonth(monthNumber, year){
var d = getLastDayOfMonth(monthNumber, year);
while (d.getDay() != 5) {
d.setDate(d.getDate() - 1);
}
return d;
}
// Some tests
var data = {
'getLastFridayOfMonth': ['9 2006','12 2009','1 2010','2 2000',''],
'getLastWeekDayOfMonth': ['9 2006','12 2009','1 2010','2 2000','']
};
var globalObj = this;
function runTest(data) {
var results = [];
var re = /00:00:00.+/;
var dat, fn, p, s;
for (p in data) {
results.push(p);
fn = globalObj[p];
dat = data[p];
for (var i=0, iLen=dat.length; i<iLen; i++) {
s = data[p][i].split(' ');
results.push((s[0]? s : 'This month') + ': ' + String(fn(s[0],
s[1])).replace(re,''));
}
results.push('');
}
alert(results.join('\n'));
}
runTest(data);