在iPhone中获取当前小时、分钟和秒

在iPhone中获取当前小时、分钟和秒,iphone,datetime,time,timezone,timestamp,Iphone,Datetime,Time,Timezone,Timestamp,如何从中获取当前的小时、分钟和秒 NSDate *now = [NSDate date]; 谢谢!我希望有3个int变量,其中包含存储的值,而不是显示值的字符串。您需要使用,这有点棘手,更不用说冗长了!因为您需要了解一下Cocoa处理日历的方式。有一个伟大的介绍中;这里有一段经过修改的摘录: NSDate *today = [NSDate date]; NSCalendar *gregorian = [[[NSCalendar alloc] i

如何从中获取当前的小时、分钟和秒

NSDate *now = [NSDate date];
谢谢!我希望有3个int变量,其中包含存储的值,而不是显示值的字符串。

您需要使用,这有点棘手,更不用说冗长了!因为您需要了解一下Cocoa处理日历的方式。有一个伟大的介绍中;这里有一段经过修改的摘录:

NSDate *today = [NSDate date];
NSCalendar *gregorian = [[[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *components =
                    [gregorian components:(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:today];

NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger second = [components second];

[gregorian autorelease]最后,为了防止内存泄漏,NSHourCalendarUnit、NSMinuteCalendarUnit和NSSecondCalendarUnit都不推荐使用。