有没有更好的方法来找到明天的午夜? [英] Is there a better way to find midnight tomorrow?

查看:145
本文介绍了有没有更好的方法来找到明天的午夜?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有更好的方法来实现?

   - (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
秒:0
timeZone:[tomorrow timeZone]];
}

请注意,我一直想要下一个午夜,即使是恰好是午夜当我打电话时,如果恰好是23:59:59,我当然想要一秒钟的午夜。



自然语言功能似乎是片面的,如果我在天字段中通过了32号,我不知道Cocoa会做什么。 (如果这样工作,我可以删除[now dateByAddingYears:...] call)

解决方案

文档


使用NSCalendarDate强烈地
不鼓励。它还不被弃用,
但是它可能是在Mac OS X v10.5之后的下一个主要OS
版本。对于
calendrical计算,您应该
使用
NSCalendar,NSDate和
NSDateComponents的适当组合,如$ a
$ a $ href =http: /developer.apple.com/documentation/Cocoa/Conceptual/DatesAndTimes/DatesAndTimes.htmlrel =noreferrer>日期和时间
可可的编程主题。


遵循该建议:

  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];
[组件释放];

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

NSDate * tomorrowMidnight = [gregorian dateFromComponents:components];

[gregorian 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆