1

明日の曜日名を返したいです (つまり、今日が日曜日の場合、月曜日が必要です)。今日の曜日名を生成するコードは次のとおりです。

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"EEEE"];
weekDay =  [formatter stringFromDate:[NSDate date]];

NSCalendar をいじるのではなく、これを行う簡潔な方法は何でしょうか (ここにあるコードに従ってください)。

ありがとうございました。

4

3 に答える 3

3

ユーザーの言語対応:

NSDateFormatter * df = [[NSDateFormatter alloc] init];
NSArray *weekdays = [df weekdaySymbols];

NSDateComponents *c = [[NSDateComponents alloc] init];
c.day = 1;
NSDate *tomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:c
                                                                 toDate:[NSDate date]
                                                                options:0];
c = [[NSCalendar currentCalendar] components:NSWeekdayCalendarUnit
                                    fromDate:tomorrow];

NSString *tomorrowname = weekdays[c.weekday-1];// the value of c.weekday may 
                                               // range from 1 (sunday) to 7 (saturday)    
NSLog(@"%@", tomorrowname);

特定の言語で名前を付ける必要がある場合は、追加します

[df setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];

日付フォーマッタの作成後。

于 2013-06-16T23:18:59.950 に答える
1

アビザンさん、コメントありがとうございます。これが私がしたことです:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"EEEE"];
    weekDay = [formatter stringFromDate:[NSDate date]];

    NSDateComponents *tomorrowComponents = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];

    NSDate *compDate = [[NSCalendar currentCalendar] dateFromComponents:tomorrowComponents];

    NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
    offsetComponents.day = 1;
    NSDate *tomorrow = [[NSCalendar currentCalendar] dateByAddingComponents:offsetComponents toDate:compDate options:0];

    nextWeekDay = [formatter stringFromDate:tomorrow];

2 つの NSString オブジェクト (weekDay と nextWeekDay) は、今日と明日 (現在は日曜日と月曜日) の曜日名を格納するようになりました。これはうまくいきますが、もっと簡単な方法があるのではないかと思います。Objective-C の日付はかなり面倒です :(

再度、感謝します。

于 2013-06-16T23:18:25.270 に答える
1

NSCalendar を「いじる」ことを恐れているのはなぜですか? NSDateComponents メソッドを使用するには、実際には NSCalendar を使用する必要があります。これがあなたの問題に対する私の解決策です。

// Get the current weekday
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents = [calendar components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
// !! Sunday = 1
NSInteger weekday = [weekdayComponents weekday];

これで、インデックス 0 で日曜日から始まる平日を含むNSDateFormatterメソッド-(NSArray *) weekdaySymbolsを使用できます。 :

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// !! Sunday = 0
NSUInteger symbolIndex = (weekday < 7) ? weekday : 0;
NSString *weekdaySymbol = [[formatter weekdaySymbols] objectAtIndex:(NSUInteger)symbolIndex];

ある程度NSCalendarを使っていますが、参考になれば幸いです。

編集: glektrik のソリューションは非常に簡単です。ただし、NSDate クラス リファレンスの- dateByAddingTimeInterval:の次のステートメントに注意してください。

戻り値: 受信者に対して秒数秒に設定された新しい NSDate オブジェクト。返された日付は、受信者のものとは異なる表現を持つ場合があります。

于 2013-06-16T22:49:24.583 に答える