0

I'm doing this app where the user inputs some days (Using UITableView, monday - sunday). I then need the app to figure out which dates this matches with. Say it's the user sits on sunday the 15th and chooses monday and tuesday. The app will figure out the dates are monday 16th and tuesday 17th.

How would one go about that using NSDate and such? I know how to find a weekday using the date, but I want the opposite.

Of course it has to be the closest days, like not finding monday the 23rd, but finding 16th.

Hope that makes sense. :-)

2 Answers2

2

A direct method, without using a loop:

NSUInteger targetWeekday = ...; // 1 = Sunday, 2 = Monday, ...

// Date components for today:
NSCalendar *cal = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
NSDateComponents *comp = [cal components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSWeekdayCalendarUnit
                                fromDate:now];

// Adjust components for target weekday:
if (targetWeekday >= comp.weekday) {
    comp.day += (targetWeekday - comp.weekday);
} else {
    comp.day += (targetWeekday + 7 - comp.weekday); // Assuming 7 days per week.
}
comp.weekday = targetWeekday;

// And back to NSDate:
NSDate *targetDate = [cal dateFromComponents:comp];

Remark:

if (targetWeekday >= comp.weekday) {
    comp.day += (targetWeekday - comp.weekday);
} else {
    comp.day += (targetWeekday + 7 - comp.weekday); // Assuming 7 days per week.
}

can be replaced by the shorter, equivalent code

comp.day += (targetWeekday + 7 - comp.weekday) % 7;
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • You could replace the `if/else` statement with `comp.day += (targetWeekday - comp.weekday) % 7;` to make it even cleaner. – Tyler Sep 16 '13 at 17:27
  • 1
    @TylerAndFriends: You would have to add `(targetWeekday + 7 - comp.weekday) % 7`, because the modulus of a negative number is negative! - I had thought about that, but decided to post code that is more easily to understand. But thank you for the feedback, and I have added that now to the answer. – Martin R Sep 16 '13 at 17:34
  • Oh my mistake, I have been using python lately where the modulus of a negative is positive :) – Tyler Sep 16 '13 at 18:17
0

You can do it by following a simple procedure:

  1. Start with an NSDate that represents today
  2. Get the day of the week from it (here is how it is done)
  3. If the day of the week matches what's in the selected UITableViewCell, you are done.
  4. Otherwise, add one day to NSDate (here is how it is done), and go back to step 2.
Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • As noted, I already know how to get the weekday off of a current NSDate, so that's simple enough. So I'll try to just add one, check again, then add until it matches. Thanks a bunch. :-) – Christian Lundtofte Sep 16 '13 at 17:05
  • @ChristianLundtofte You are welcome! General questions like that tend to be found by search engines, so others are very likely to make some use of your question. That's why I added links to other SO questions - other users who find this through a search engine may not know how to get a week day from `NSDate`, or how to add a day. – Sergey Kalinichenko Sep 16 '13 at 17:10