如何从中获取当前的小时、分钟和秒?
NSDate *now = [NSDate date];
谢谢!我想要 3 个 int 变量,其中存储有值,而不是显示值的字符串。
最佳答案
您需要使用NSDateComponents
,这有点棘手(更不用说冗长了!),因为您需要了解一点 Cocoa 处理日历的方式。 docs中有一个很棒的介绍。 ;这是修改后的摘录:
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];
关于iphone - 获取 iPhone 中当前的小时、分钟和秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2361087/