sud*_*eer 5 objective-c nsdate nsdateformatter ios
我有这样的时间格式
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm:ss"];
Run Code Online (Sandbox Code Playgroud)
从这个我只需要HH值.
如何获得这个价值
Use*_*343 10
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease];
timeFormatter.dateFormat = @"HH";
NSString *datestringofHour= [timeFormatter stringFromDate: localDate];
Run Code Online (Sandbox Code Playgroud)
现在在datestringofHour变量中,您将小时值作为字符串.
另外一个如下,它可以给你整数值的小时,分钟,秒
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]];
NSInteger hour= [components hour];
NSInteger minute = [components minute];
NSInteger second = [components second];
Run Code Online (Sandbox Code Playgroud)