views:

197

answers:

2

Hello

I want to create a function which results the date of next Friday but i've no plan how to do it. Has anyone a good hint to me ?

Greets Simon

A: 

E.g. Get current date using NSDate, then use 'components>fromDate:' from NSCalendar to get the NSDateComponents, then add the time difference to next Friday and create a new NSDate and Bob's is your uncle.

Anders K.
i'll try to figure it out with that route. cheers
Simon
A: 

Here is my solution, and just to warn you, on a saturday is the friday before shown. cheers to all

NSDate *today = [[NSDate alloc] init];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:today];
int weekday = [weekdayComponents weekday];


int iOffsetToFryday = -weekday + 6;
weekdayComponents.weekday = iOffsetToFryday;

NSDate *nextFriday = [[NSCalendar currentCalendar] dateByAddingComponents:weekdayComponents toDate:today options:0];
Simon
How can you consider that to be correct if it returns the wrong answer when ran on a Saturday?
Adam Rosenfield
i see that this isnt the correct way to handle
Simon