Question

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
Était-ce utile?

La solution 2

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];

Autres conseils

This:

NSDate *referenceDate = [NSDate date];
...
NSDateComponents *dateComponents = [calendar components:(NSYearCalendarUnit|NSMonthCalendarUnit | NSWeekCalendarUnit |NSWeekdayCalendarUnit) fromDate:referenceDate];

Returns a date components populated correctly for today because you've asked for today's date, then asked the calendar to break that down as year, month, week and weekday.

Following that, this:

[dateComponents setWeekday:day];
[dateComponents setWeek:dateComponents.week+(week-1)];
[dateComponents setWeek:dateComponents.week];

Sets the day. Then it reads the current set week and adds week-1 to it (so week 1 will be this week, week 2 will be next week, etc). It then redundantly reads the week again and sets it again.

Your output shows that the code works correctly. Apple is an American company so weekday 0 is always Sunday to them. But they're aware that Sunday isn't the first day of the week in most places so your code asks for "Sunday (day 0), this week" — not "Sunday this week if we pretend that the week started on Sunday". It says Sunday is the 11th. Allowing for timezone differences, that's correct.

Notice that NSDates do not have a time zone. An NSDate is an opaque record of a particular moment in time. How you would communicate that moment is immaterial. They log in GMT for the sake of being able to say something.

You then ask for Monday, Tuesday and Wednesday and it returns the 5th, 6th and 7th. All correct.

Side note: just use:

NSCalendar *calendar = [NSCalendar currentCalendar];

To use the current system calendar.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top