2012-01-30 101 views
0

我有以下代码。当我在我的iOS 3.1.3设备上运行它时(它克服它!)它按预期工作,但是当我在模拟器(iOS 5和4.3)上运行它时,它是1分15秒(9:01:15 )。这是因为我的代码,模拟器中的错误还是iOS 4+中的错误?NSDateComponents在模拟器上运行

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
NSDateComponents *components = [calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:[NSDate date]]; 
components.hour = 9; 
components.minute = 0; 
NSDate *date = [calendar dateFromComponents:components]; // <-- date is off on simulator 

感谢您的帮助!

回答

2

我不完全理解为什么会出现这种情况,但是这种情况发生在使用dateFromComponents:很久以前创建日期时发生。具体来说,截止日期似乎是1847年12月1日。早于上午12点的任何事情似乎最终都会在15分钟后关闭。

在你的情况下,这是因为如果你只设置components.hourcomponents.minute,你会得到一个日期从1月1日,1AD。

如果你设定年,月,日的东西更近,这样的:

components.year = 2012; 
components.month = 2; 
components.day = 6; 

然后你会得到正确的输出(我在的iOS模拟器得到):

2012-02-06 09:00:00 +0000 
+0

啊,这可以解决它。谢谢! – jrtc27 2012-02-06 16:36:15

+1

可能是因为(根据维基百科)这是公司切换到英国的标准时间(http://en.wikipedia.org/wiki/Railway_time#Great_Britain)。 – jrtc27 2012-02-06 16:40:16

+0

哎呀 - 忘了接受你的答案! – jrtc27 2012-02-06 21:40:58