Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get hour with date format

I have a time format like this

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm:ss"];

from this, I need only HH value.

How to get that value?

like image 231
sudheer Avatar asked Aug 21 '13 12:08

sudheer


1 Answers

NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease];
timeFormatter.dateFormat = @"HH";


NSString *datestringofHour= [timeFormatter stringFromDate: localDate];

Now in datestringofHour variable you will have hour value as a string.

Another one as follows which can give you hour,minute,seconds in integer value

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit fromDate:[NSDate date]]; 

NSInteger hour= [components hour];   
NSInteger minute = [components minute];
NSInteger second = [components second]; 
like image 159
User 1531343 Avatar answered Sep 21 '22 12:09

User 1531343