Search code examples
objective-cnscalendarnsdatecomponents

Get NSDate from week and day


I am trying to get an NSDate from a day and week post to today.

E.g. week = 2 and day = 4, which would be thursday in two weeks from now (given the week starts on Monday).

I tried a bunch of different things using NSCalendar and NSDateComponents, but nothing came close. Searched a bit as well, but haven't found any other topic regarding my problem.

The last thing I tried was the following:

- (NSDate *)getDateForIndexPath:(NSIndexPath *)indexPath
{
    int week = ((indexPath.row / 7) % 5);
    int day = (indexPath.row % 7);

    NSDate *referenceDate = [NSDate date];

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    [calendar setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"es_ES"]];
    [calendar setTimeZone:[NSTimeZone localTimeZone]];

    NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit | NSWeekCalendarUnit |NSWeekdayCalendarUnit) fromDate:referenceDate];
    [dateComponents setWeekday:day];
    [dateComponents setWeek:dateComponents.week+(week-1)];
    [dateComponents setWeek:dateComponents.week];

    NSDate *followingDate = [calendar dateFromComponents:dateComponents];

    NSLog(@"Week: %d - Day: %d", week, day);
    NSLog(@"followingDate: %@", followingDate);

    return followingDate;
}

This gave me the same date over and over again, besides the fact that it sees day 0 as sunday:

2014-04-08 00:02:33.706 TVSports[79878:60b] Week: 1 - Day: 0
2014-04-08 00:02:33.707 TVSports[79878:60b] followingDate: 2014-04-11 22:00:00 +0000
2014-04-08 00:02:33.708 TVSports[79878:60b] Week: 1 - Day: 1
2014-04-08 00:02:33.708 TVSports[79878:60b] followingDate: 2014-04-05 22:00:00 +0000
2014-04-08 00:02:33.709 TVSports[79878:60b] Week: 1 - Day: 2
2014-04-08 00:02:33.709 TVSports[79878:60b] followingDate: 2014-04-06 22:00:00 +0000
2014-04-08 00:02:33.710 TVSports[79878:60b] Week: 1 - Day: 3
2014-04-08 00:02:33.710 TVSports[79878:60b] followingDate: 2014-04-07 22:00:00 +0000

Solution

  • You can do this to get the Thursday in two weeks:

    typedef NS_ENUM(NSUInteger, WeekDay){
        WeekDaySunday = 1,
        WeekDayModay,
        WeekDayTuesday,
        WeekDayWensday,
        WeekDayThursday,
        WeekDayFriday,
        WeekDaySaturday
    };
    
    
    NSDate *now = [NSDate date];
    NSDate *beginOfThisWeek;
    NSCalendar *cal = [NSCalendar currentCalendar];
    [cal setFirstWeekday:WeekDayModay];
    [cal rangeOfUnit:NSWeekCalendarUnit // get the beginning of this week
           startDate:&beginOfThisWeek   // save the beginning to this variable
            interval:NULL               // we do not care for the weeks length
             forDate:now];
    NSDateComponents *twoWeeks = [[NSDateComponents alloc] init];
    twoWeeks.week = 2;
    twoWeeks.day = WeekDayThursday - WeekDaySunday -1 ; // we need the difference between thursday and the beginning of the week
    
    NSDate *thurdayInTwoWeeks = [cal dateByAddingComponents:twoWeeks 
                                                     toDate:beginOfThisWeek
                                                    options:0];