2012-03-12 15 views
13

Ho questo codice in cui sto cercando di ottenere la data corrente e formattarla nella locale corrente.Come si formatta la data corrente per le impostazioni locali dell'utente?

NSDate *now = [NSDate date]; // gets current date 
NSString *sNow = [[NSString alloc] initWithFormat:@"%@",now]; 
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"mm-dd-yyyy"]; 
insertCmd = [insertCmd stringByAppendingString: formatter setDateFormat: @"MM.dd.yyyy"]; 

so l'ultima riga è sbagliato, ma non riesco a capirlo ... "insertCmd" è una NSString che sto costruendo per un comando FMDB.

L'aiuto sarebbe molto apprezzato, o un puntatore al "doc" dove è descritto.

risposta

29

In questo caso non userei setDateFormat perché limita il formattatore della data a un formato di data specifico (doh!) - si desidera un formato dinamico in base alle impostazioni locali dell'utente.

NSDateFormatter vi offre una serie di built-in di data/ora stili che si può scegliere, vale a dire NSDateFormatterMediumStyle, NSDateFormatterShortStyle e così via.

Che cosa si deve fare è:

NSDate* now = [NSDate date]; 
NSDateFormatter* df = [[NSDateFormatter alloc] init]; 
[df setDateStyle:NSDateFormatterMediumStyle]; 
[df setTimeStyle:NSDateFormatterShortStyle]; 
NSString* myString = [df stringFromDate:now]; 

Questo vi fornirà una stringa con una data di media lunghezza e un tempo breve di lunghezza, tutti a seconda locale dell'utente. Sperimenta con le impostazioni e scegli quello che preferisci.

Ecco un elenco di stili disponibili: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/c/tdef/NSDateFormatterStyle

3

Se si desidera che la data e l'ora localizzata questo darà a voi:

NSString *localizedDateTime = [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]; 

Il codice di cui sopra non ti dà la data e l'ora localizzata .

5

Oltre a jiayow risposta è possibile specificare il 'modello' personalizzato per ottenere la versione localizzata:

+ (NSString *)formattedDate:(NSDate *)date usingTemplate:(NSString *)template { 
    NSDateFormatter* formatter = [NSDateFormatter new]; 

    formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:template options:0 locale:formatter.locale]; 

    return [formatter stringFromDate:date]; 
} 

utilizzo esempio per noi/DE locale:

NSLocale *enLocale = [NSLocale localeWithLocaleIdentifier:@"en_US"]; 
NSLocale *deLocale = [NSLocale localeWithLocaleIdentifier:@"de"]; 

// en_US: MMM dd, yyyy 
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"ddMMMyyyy" options:0 locale:enLocale]; 
// de:  dd. MMM yyyy 
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"ddMMMyyyy" options:0 locale:deLocale]; 

// en_US: MM/dd/yyyy 
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"ddyyyyMM" options:0 locale:enLocale]; 
// de:  dd.MM.yyyy 
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"ddyyyyMM" options:0 locale:deLocale]; 

// en_US MM/dd 
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"MMdd" options:0 locale:enLocale]; 
// de:  dd.MM. 
formatter.dateFormat = [NSDateFormatter dateFormatFromTemplate:@"MMdd" options:0 locale:deLocale]; 
Problemi correlati