2011-01-02 15 views

答えて

4

[NSDate dateWithNaturalLanguageString: @"next Saturday"]


編集:

:OS X 10.9とiOS 8以降では、あなたがこれを行うことができます
NSCalendar *calendar = [NSCalendar currentCalendar]; 

NSDate *now = [NSDate date]; 
NSDateComponents *saturday = [[NSDateComponents alloc] init]; 
saturday.weekday = 7 /* Saturday */; 

NSDate *nextSaturday = [calendar nextDateAfterDate:now matchingComponents:saturday options:0]; 
+3

公式ドキュメントから:このメソッド(dateWithNaturalLanguageString :)は、限定された口語訳のフレーズ(主に英語)をサポートしています。予期しない結果が出る可能性があり、その使用は強くお勧めします。 –

+0

@Bavarious - 私はそれを見ました。それは私がやろうとしていたことよりずっと簡単で、私の場合にはうまくいきます。 – Moshe

+0

たとえそれが英語で限定された口語のフレーズしかサポートしないとしても、「次の土曜日」はどんなアプリケーションでも動作します。 –

10
NSCalendar *gregorian = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDate *date = [NSDate date]; 
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date]; 
NSInteger todayWeekday = [weekdayComponents weekday]; 

enum Weeks { 
    SUNDAY = 1, 
    MONDAY, 
    TUESDAY, 
    WEDNESDAY, 
    THURSDAY, 
    FRIDAY, 
    SATURDAY 
}; 

NSInteger moveDays=SATURDAY-todayWeekday; 
if (moveDays<=0) { 
    moveDays+=7; 
} 

NSDateComponents *components = [NSDateComponents new]; 
components.day=moveDays; 

NSCalendar *calendar=[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar]; 
NSDate* newDate = [calendar dateByAddingComponents:components toDate:date options:0]; 

NSLog(@"%@",newDate); 
+0

これは正解とマークする必要があります。 – BFar

関連する問題