2013-08-21 14 views
5

mam format czasu jak tenjak dostać godzinę z datą formacie

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

z tym muszę tylko wartość HH.

jak uzyskać tę wartość

+0

http://stackoverflow.com/questions/10861433/in-objective-c-to-get-the- aktualna godzina i minuta jako liczby całkowite - potrzebujemy użyć -n odwiedź to –

+7

Wow ... 'HH: mm: ss' daje' 12: 45: 32', potrzebujesz tylko 'HH' ... nie mów mi, że szukałeś więcej niż 4 sekundy przed opublikowaniem tego. – Cyrille

Odpowiedz

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


NSString *datestringofHour= [timeFormatter stringFromDate: localDate]; 

Teraz w datestringofHour zmiennej będzie mieć wartość godzinny jako ciąg znaków.

Kolejnym jak następuje, i który może dać godziny, minuty, sekundy wartości całkowitej

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

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

Wystarczy przełączyć:

[df setDateFormat:@"HH:mm:ss"]; 

do:

[df setDateFormat:@"HH"];