2011-08-24 12 views
10
NSDateFormatter *formatter1=[[NSDateFormatter alloc]init]; 
[formatter1 setDateFormat:@"HHmmss MMddyyyy"]; 
NSDate *finalDate =[formatter1 dateFromString:testTime]; 

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setTimeStyle:NSDateFormatterShortStyle]; 
[lblpickUpTime setText:[formatter stringFromDate:finalDate]]; 
[formatter release]; 
[formatter1 release]; 

dove testTime = @ "201518 07122011"; e voglio il risultato in 24 oreiphone: NSDateFormatter come mostrare l'ora nel formato 24 ore?

ad es. 20.15 anziché 8:15 PM

e c'è un modo migliore per farlo?

risposta

25

Prova impostando

[formatter setDateFormat:@"HH:mm"]; // replacement for [formatter setTimeStyle:NSDateFormatterShortStyle]; 

Metodo alternativo (senza l'uso di formattatori):

NSRange range; 
range.location = 2; 
range.length = 2; 
NSString *dateString = [NSString stringWithFormat:@"%@:%@",[myString substringToIndex:2],[myString substringWithRange:range]]; 
+0

Voglio solo il tempo non la data – Azhar

+0

@azar controllare la mia risposta a cura – KingofBliss

+0

per quanto riguarda il mio secondo qutions che c'è un modo migliore per fare questa conversione come ho fatto due formattatori? – Azhar

4

Se si specifica un formato invece di utilizzare NSDateFormatterShortStyle, si può avere l'output in qualsiasi formato tu vuoi. Per esempio:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"]; 
[lblpickUpTime setText:[formatter stringFromDate:finalDate]]; 
[formatter release]; 
Problemi correlati