Last Friday of this Month

283 views
Skip to first unread message

J_Mo

unread,
Sep 5, 2010, 9:57:23 AM9/5/10
to Datejs - A JavaScript Date Library
Hi,

I think this will be an easy one... How do you return Last Friday of
this Month??

Thanks
J_mo

geoffrey.mcgill

unread,
Sep 6, 2010, 9:07:14 PM9/6/10
to Datejs - A JavaScript Date Library
Date.today().final().fri()

Hope this helps.

J_Mo

unread,
Sep 13, 2010, 8:39:14 AM9/13/10
to Datejs - A JavaScript Date Library
Hi again, I have one more question. I am trying to say 'last weekday
of the month'

I have: var d1=Date.today().final().weekday();

This is returning a value of 'false' though. Can someone help please?

Phil

geoffrey.mcgill

unread,
Sep 13, 2010, 10:51:40 AM9/13/10
to Datejs - A JavaScript Date Library
For final friday of next month:

Date.today().next().month().final().friday()


The "weekday" function is used in conjunction with the "is" function
to determine if a Date "is a" weekday. It will not return the
weekday.

Date.today().is().weekday() // true|false

Hope this helps.

J_Mo

unread,
Sep 13, 2010, 10:58:03 AM9/13/10
to Datejs - A JavaScript Date Library
Hi again,

Thanks for that. I managed to get the final friday for next month
myself...go me!!

How would I return an actual date for the final weekday of the month
rather than a true or false statement?

Phil

Chad

unread,
Sep 13, 2010, 3:53:51 PM9/13/10
to Datejs - A JavaScript Date Library
Phil,

Create a function that will get the dates of the last Monday, Tuesday,
Wednesday, Thursday and Friday and put them into an array. Sort the
array and return the last element (or sort the array descending and
return the first element).

HTH,
Chad

geoffrey.mcgill

unread,
Sep 13, 2010, 4:36:39 PM9/13/10
to Datejs - A JavaScript Date Library
Here's a function to return the last weekday, or current date if it is
a weekday.

Example

var lastWeekday = function (d) {
return d.is().weekday() ? d : lastWeekday(d.add(-1).day());
};

To find the last weekday of a month, just pass in the last day of the
month.

Example

lastWeekday(Date.today().moveToLastDayOfMonth())

To find the last day of next month...

Example

lastWeekday(Date.today().add(1).month().moveToLastDayOfMonth())

Hope this helps.

RobG

unread,
Sep 21, 2010, 2:34:29 AM9/21/10
to Datejs - A JavaScript Date Library


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);
Reply all
Reply to author
Forward
0 new messages