任意の日付が与えられた場合、その月の次の週の最初の日付を見つける必要があります。月の最終週は 7 日未満になる場合があるため、現在の日付に 7 日を加算するだけの簡単な方法ではないことに注意してください。これが私が今使っているコードです:
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSWeekOfMonthCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSDayCalendarUnit fromDate:currentDate];
NSLog(@"week of month: %ld", [components weekOfMonth]);
[components setWeekOfMonth:[components weekOfMonth] + 1];
NSLog(@"new week of month: %ld", [components weekOfMonth]); //week of month is now 2
[components setWeekday:1];
NSDate *nextWeek = [calendar dateFromComponents:components];
例として、currentDate は 2012-10-01 に設定されます。この例では、nextWeek は常に 2012-10-01 です。送信しても、オブジェクトsetWeekOfMonth:
内の他の日付コンポーネントは増加しないようです。NSDateComponents
間違った日付コンポーネントを構成していますか、それともsetWeekOfMonth:
そのように機能するはずがありませんか?