1

日付間の間隔を取得する特定のメソッドのドキュメントを確認しました。

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate

特定の日付からの年数と日数を提供するために、このメソッド (または代替手段) が必要です。

どうすればこれを計算できますか?日/365 を使用して、残りを日数にすることもできますが、これはあまり正確ではなく、これには正確さが必要です。

ありがとう!

4

1 に答える 1

3

- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate多くの場合(夏時間、うるう秒など)、使用は失敗します。カレンダーを考慮に入れる必要があります。

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [gregorianCalendar components:(NSDayCalendarUnit |NSYearCalendarUnit )
                                                    fromDate:startDate
                                                      toDate:[NSDate date]
                                                     options:0];

NSLog(@"%ld", [components day]);
NSLog(@"%ld", [components year]);
于 2013-02-17T13:51:36.027 に答える