0

ユーザーが日付ピッカーから選択した特定の日付の7日前にアラームを設定する必要があるプロジェクトに取り組んでいます。

次のコードを使用して、ユーザーから日付を取得しました

NSDateFormatter *df = [[NSDateFormatter alloc]init];
[df setDateStyle:NSDateFormatterFullStyle];
NSString *dateStr = [NSString stringWithFormat:@"%@",[df stringFromDate:datePickerObj.date]];
[df release];

選択した日付の7日前の日付を取得する方法を教えてください。前もって感謝します

4

1 に答える 1

8

dateByAddingComponents:toDate:options:次のように使用します。

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = -7;
NSDate *earlierDate = [calendar dateByAddingComponents:components
    toDate:datePickerObj.date options:0];
于 2012-08-08T07:32:56.390 に答える