12

これを行うためのより良い方法はありますか?

-(NSDate *)getMidnightTommorow {
    NSCalendarDate *now = [NSCalendarDate date];
    NSCalendarDate *tomorrow = [now dateByAddingYears:0 months:0 days:1 hours:0 minutes:0 seconds:0];
    return [NSCalendarDate dateWithYear:[tomorrow yearOfCommonEra]
                                  month:[tomorrow monthOfYear]
                                    day:[tomorrow dayOfMonth]
                                   hour:0
                                 minute:0
                                 second:0
                               timeZone:[tomorrow timeZone]];
}

その電話をかけるときにたまたま真夜中であっても、常に次の真夜中が欲しいことに注意してください。ただし、23:59:59の場合は、もちろん1秒で来る真夜中が欲しいです。

自然言語の機能は不安定に見えます。「日」フィールドで32を渡すと、Cocoaが何をするかわかりません。(それがうまくいけば、[now dateByAddingYears:...]呼び出しを削除できます)

4

6 に答える 6

25

ドキュメントから:

NSCalendarDate の使用は強くお勧めしません。まだ非推奨ではありませんが、Mac OS X v10.5 の後の次のメジャー OS リリースでサポートされる可能性があります。カレンダー計算では、NSCalendar、NSDate、および NSDateComponents の適切な組み合わせを使用する必要があります。これについては、Cocoa の Dates and Times Programming Topics のCalendars で説明されています。

そのアドバイスに従って:

NSDate *today = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
NSDate *tomorrow = [gregorian dateByAddingComponents:components toDate:today options:0];
[components release];

NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
components = [gregorian components:unitFlags fromDate:tomorrow];
components.hour = 0;
components.minute = 0;

NSDate *tomorrowMidnight = [gregorian dateFromComponents:components];

[gregorian release];
[components release];

(これが最も効率的な実装であるかどうかはわかりませんが、正しい方向へのポインターとして機能するはずです。)

注: 理論的には、コンポーネントの通常の値の範囲よりも大きな値を持つ日付コンポーネント オブジェクトを許可することで、ここでコードの量を減らすことができます (たとえば、単純に日付コンポーネントに 1 を追加すると、値が 32 になる可能性があります)。 )。ただし、範囲外の値を許容するdateFromComponents: 場合もありますが、保証はされていません。それに頼らないことを強くお勧めします。

于 2008-10-08T05:56:35.640 に答える
7

いいえ、今日の真夜中を見つけるのと同じ方法です。

于 2008-10-08T06:06:31.970 に答える
4
NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];

NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:(24 * 60 * 60)];

NSDateComponents *components = [gregorian components:(NSYearCalendarUnit |
                                                      NSMonthCalendarUnit |
                                                      NSDayCalendarUnit)
                                            fromDate:tomorrow];

NSDate *midnight = [gregorian dateFromComponents:components];
于 2009-08-06T07:25:55.660 に答える
1
[NSDate dateWithNaturalLanguageString:@"midnight tomorrow"];
于 2009-05-13T20:55:38.647 に答える
0

現在の日付と時刻を Unix 日付 (1970 年以降の秒数) または DOS スタイル (1980 年以降) に変換し、24 時間を追加して元に戻します。次に、時、分、秒をゼロにリセットして、真夜中にします。

于 2008-10-08T06:01:24.400 に答える
0

この方法を試すことができます:

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];
NSDate *tomorrow = [calendar dateByAddingComponents:comps toDate:[NSDate date] options:0]; //it gives us tomorrow with current time
NSDate *midnight = [calendar startOfDayForDate:tomorrow]; //here we get next midnight

NSTimer を設定する必要がある場合は、秒間隔を取得するのも簡単です。

double intervalToMidnight = midnight.timeIntervalSinceNow;
于 2016-02-21T11:16:26.773 に答える