2012-01-20 12 views
8

Stavo lavorando a un'applicazione per iPhone e ho bisogno di visualizzare l'ora in un'etichetta. Così ho scritto il seguente:Come visualizzare l'ora nel formato 12 ore in Objective-C?

NSDate *date = [NSDate date]; 
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"HH:mm a"]; 
NSString *formattedDateString = [dateFormatter stringFromDate:date]; 
timeLabel.text = formattedDateString;** 

Visualizza l'ora nell'etichetta ma nel formato 24 ore.

  • Esempio: 15.00 PM invece di 03:00

voglio visualizzare l'ora in formato 12 ore, vale a dire 15:00

Cosa devo fare?

Grazie in anticipo.

+2

Si dovrebbe visualizzare l'ora nel formato che l'utente preferisce - l'utente imposta questa preferenza in Impostazioni -> Generale -> Data e ora -> 24 ore - o quindi se imponi un display AM/PM di 12 ore, allora darai fastidio a molti utenti al di fuori degli Stati Uniti –

risposta

54

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

Annotare il minuscolo h.

+0

hey è così pazzo Tahnk ti eccita .. Adam –

+0

funziona solo se il tempo di 24 ore è ON su Impostazioni dispositivo- > generale-> data e ora – Mona

+0

@Mona che non è corretto. Devi ricontrollare ciò che hai fatto. E per la minima possibilità che sia corretto - deve essere su una versione specifica di iOS, nel qual caso si prega di presentare una segnalazione di bug ad Apple. –

2

Leggendo la documentazione è una buona pratica.

HH- sta semplicemente mostrando lo 0 quando è in 2,3,4 ore. NON è il formato 0-12 AM/PM.

Vedere "Formattazione data" per ulteriori dettagli.

nel tuo caso:

Appendice F: Patterns Data Format

h:mm a -> 3:00 PM 
+0

Il tempo è visualizzato in "16:04", il formato data è impostato su @ "h: mm a" –

+0

L'ora del dispositivo di controllo è impostata sul formato 12 ore o meno. – chetu

18
Date Formats a: AM/PM 
    A: 0~86399999 (Millisecond of Day) 

    c/cc: 1~7 (Day of Week) 
    ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat 
    cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday 

    d: 1~31 (0 padded Day of Month) 
    D: 1~366 (0 padded Day of Year) 

    e: 1~7 (0 padded Day of Week) 
    E~EEE: Sun/Mon/Tue/Wed/Thu/Fri/Sat 
    EEEE: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday 

    F: 1~5 (0 padded Week of Month, first day of week = Monday) 

    g: Julian Day Number (number of days since 4713 BC January 1) 
    G~GGG: BC/AD (Era Designator Abbreviated) 
    GGGG: Before Christ/Anno Domini 

    h: 1~12 (0 padded Hour (12hr)) 
    H: 0~23 (0 padded Hour (24hr)) 

    k: 1~24 (0 padded Hour (24hr) 
    K: 0~11 (0 padded Hour (12hr)) 

    L/LL: 1~12 (0 padded Month) 
    LLL: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec 
    LLLL: January/February/March/April/May/June/July/August/September/October/November/December 

    m: 0~59 (0 padded Minute) 
    M/MM: 1~12 (0 padded Month) 
    MMM: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec 
    MMMM: January/February/March/April/May/June/July/August/September/October/November/December 

    q/qq: 1~4 (0 padded Quarter) 
    qqq: Q1/Q2/Q3/Q4 
    qqqq: 1st quarter/2nd quarter/3rd quarter/4th quarter 
    Q/QQ: 1~4 (0 padded Quarter) 
    QQQ: Q1/Q2/Q3/Q4 
    QQQQ: 1st quarter/2nd quarter/3rd quarter/4th quarter 

    s: 0~59 (0 padded Second) 
    S: (rounded Sub-Second) 

    u: (0 padded Year) 

    v~vvv: (General GMT Timezone Abbreviation) 
    vvvv: (General GMT Timezone Name) 

    w: 1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year) 
    W: 1~5 (0 padded Week of Month, 1st day of week = Sunday) 

    y/yyyy: (Full Year) 
    yy/yyy: (2 Digits Year) 
    Y/YYYY: (Full Year, starting from the Sunday of the 1st week of year) 
    YY/YYY: (2 Digits Year, starting from the Sunday of the 1st week of year) 

    z~zzz: (Specific GMT Timezone Abbreviation) 
    zzzz: (Specific GMT Timezone Name) 
    Z: +0000 (RFC 822 Timezone) 

How to Use: 

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"]; 
NSLog(@"%@", [dateFormatter dateFromString:@"12/12/2012 12:12:12 AM"]); 
0

Swift 3,0

dateFormatter.dateFormat = "hh:mm a" 
Problemi correlati