有更好的方法找到明天午夜吗? [英] Is there a better way to find midnight tomorrow?
问题描述
有更好的方法吗?
- (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]
小时:0
分钟:0
second:0
timeZone:[tomorrow timeZone]];
}
请注意,我总是希望下一个午夜,即使它恰好是午夜当我打电话时,如果它恰好是23:59:59,我当然希望在一秒钟的午夜。
自然语言功能似乎片状,我不知道可可会做什么,如果我通过32在日字段。 (如果可以,我可以放弃[现在dateByAddingYears:...]调用)
=http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSCalendarDate_Class/Reference/Reference.html =nofollow noreferrer>文档:
使用NSCalendarDate强烈的
。它不是过时的,
,但它可能在Mac OS X v10.5之后的下一个主要OS
版本。对于
日历计算,您应该
使用
NSCalendar,NSDate和
的合适组合,如
中所述$ d
日历日期和时间
Cocoa的编程主题。
遵循以下建议:
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:
可能容许超出值,但不能保证。强烈建议您不要依赖它。
Is there a better way to do this?
-(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]];
}
Note that I always want the next midnight, even if it happens to be midnight when I make that call, however if it happens to be 23:59:59, I of course want the midnight that is coming in one second.
The natural language functions seem flaky, and I'm not sure what Cocoa would do if I pass 32 in the "day" field. (If that'd work I could drop the [now dateByAddingYears:...] call)
From the documentation:
Use of NSCalendarDate strongly discouraged. It is not deprecated yet, however it may be in the next major OS release after Mac OS X v10.5. For calendrical calculations, you should use suitable combinations of NSCalendar, NSDate, and NSDateComponents, as described in Calendars in Dates and Times Programming Topics for Cocoa.
Following that advice:
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];
(I'm not sure offhand if this is the most efficient implementation, but it should serve as a pointer in the right direction.)
Note: In theory you can reduce the amount of code here by allowing a date components object with values greater than the range of normal values for the component (e.g. simply adding 1 to the day component, which might result in its having a value of 32). However, although dateFromComponents:
may tolerate out-of-bounds values, it's not guaranteed to. You're strongly encouraged not to rely on it.
这篇关于有更好的方法找到明天午夜吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!