2012-07-23 9 views
45

Sto lavorando su un'app per iPhone con NSDateFormatter. Sto ottenendo il tempo dal webservice come "00:00:00", "16:00:00","15:30:00" and so on. Devo convertire questi orari a se l'ora è "00:00:00" to "12 AM", se l'ora è "16:00:00" to "4 PM", se l'ora è "15:30:00" a "15.30 PM". Ho usato il codice qui sotto per questo, ma quando ho passare "16:00:00" the date returns null.Come ottenere il tempo in formato am/pm iphone NSDateFormatter?

NSString *dats1 = @"16:00:00"; 
NSDateFormatter *dateFormatter3 = [[[NSDateFormatter alloc] init] autorelease]; 
[dateFormatter3 setDateStyle:NSDateFormatterMediumStyle]; 
[dateFormatter3 setDateFormat:@"hh:mm:ss"]; 
NSDate *date1 = [dateFormatter3 dateFromString:dats1]; 
NSLog(@"date1 : %@", date1); **// Here returning (null)** 

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"hh:mm a"]; 
NSLog(@"Current Date: %@", [formatter stringFromDate:date1]); **// Here also returning (null)** 
[formatter release]; 

Quando sto passando "00:00:00" sta tornando "00:00" in modo corretto. Qualcuno può aiutarmi a risolvere questo problema? Grazie in anticipo.

+1

Grazie a tutti coloro che postano la tua risposta e mi aiutano. Tutti voi avete risposto correttamente e mi avete aiutato a risolvere questo problema. Grazie ancora. – Gopinath

+2

Ho dato l'uptote a tutte le tue risposte corrette. Grazie mille per la tua mano. – Gopinath

+1

In realtà avresti dovuto dedicare del tempo a leggere la documentazione di 'NSDateFormatter' e avresti potuto rispondere da solo. –

risposta

104

Se il formato dell'ora è in 24hrs si prega di utilizzare il formato "HH:mm:ss". Si prega di testare il codice e fammi sapere se funziona per voi. Ho testato e sta tornando "4 PM"

NSString *dats1 = @"16:00:00"; 
NSDateFormatter *dateFormatter3 = [[[NSDateFormatter alloc] init] autorelease]; 
[dateFormatter3 setDateStyle:NSDateFormatterMediumStyle]; 
[dateFormatter3 setDateFormat:@"HH:mm:ss"]; 
NSDate *date1 = [dateFormatter3 dateFromString:dats1]; 
NSLog(@"date1 : %@", date1); **// Here returning (null)** 

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"hh:mm a"]; 
NSLog(@"Current Date: %@", [formatter stringFromDate:date1]); **// Here also returning (null)** 
[formatter release]; 

Grazie.

+0

12:12 qui, ma questo è tornato 12:12 pm ..Hmmm –

+0

restituisce 16:00 in ios9 –

+0

@PatelJigar aggiungi questo [dateFormatter setLocale: [[NSLocale alloc] initWithLocaleIdentifier: @ "en_US_POSIX"]]; –

16

Perché allocare nuovamente lo NSDateFormatter?

Hai solo bisogno di formattarlo per NSDate e poi di nuovo a NSString,

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

    NSDate *date = [dateFormatter dateFromString:dats1]; 

    [dateFormatter setDateFormat:@"hh:mm a"]; 

    NSString *formattedDate = [dateFormatter stringFromDate:date]; 

    NSLog(@"%@",formattedDate); 
+0

Bel codice ... +1, ma prima penso 'dateFormatter setDateFormat: @" HH: mm: ss "];' non è necessario. – Dilip

+6

È necessario perché ha il formato delle ore di input in formato HH, cioè il formato 24 ore – Charan

14

Prova HH:mm:ss (notare i capitelli nel formato ora)

+4

ingrandito per H vs h. –

7

speranza che qualcuno sarebbe utile che vorrebbero farlo in Swift

Per Swift

let dateFormatter = NSDateFormatter() 
//if some one want 12AM,5PM then use "hh mm a" 
//if some one want 00AM,17PM then use "HH mm a" 
dateFormatter.dateFormat = "hh mm a" 
let stringDate = dateFormatter.stringFromDate(NSDate()) 
println(stringDate) 
+0

non funziona amico mio –

6

Questo codice Di seguito tenere sempre in formato 12 ore:

NSLocale *12hourFomrat = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]; 
df.locale = 12hourFomrat; 

Questo tenere sempre in formato 24 ore

NSLocale *24hourFormat = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"]; 
df.locale = 24hourFormat 
11

È possibile utilizzare il .ShortStyle, che si adatterà alla locale corrente:

let timeFormatter = NSDateFormatter() 
timeFormatter.dateStyle = .NoStyle 
timeFormatter.timeStyle = .ShortStyle 
timeFormatter.stringFromDate(NSDate()) 

Che restituisce 17:12 o 5:12 PM a seconda delle impostazioni locali del dispositivo.

Problemi correlati