我有这样的时间格式
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm:ss"];
由此,我只需要HH值。
如何获取该值?
最佳答案
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease];
timeFormatter.dateFormat = @"HH";
NSString *datestringofHour= [timeFormatter stringFromDate: localDate];
现在在 datestringofHour
变量中,您将拥有字符串形式的小时值。
下面的另一个可以给你小时,分钟,秒的整数值
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
NSInteger hour= [components hour];
NSInteger minute = [components minute];
NSInteger second = [components second];
关于ios - 如何使用日期格式获取小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18357246/