iphone shortdate con anno a 4 cifre
-
21-08-2019 - |
Domanda
Sono sicuro che mi sto perdendo qualcosa. Ma ho cercato su Google questo per giorni cercando di trovare come mostrare un anno a 4 cifre quando si visualizza un NSDate con uno stile di NSDateFormatterShortStyle. Per favore fatemi sapere se avete una soluzione. Ecco il codice che sto usando ora.
[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease ];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
// add label for birth
UILabel *birthday = [[UILabel alloc] initWithFrame:CGRectMake(125, 10, 100, 25)];
birthday.textAlignment = UITextAlignmentRight;
birthday.tag = kLabelTag;
birthday.font = [UIFont boldSystemFontOfSize:14];
birthday.textColor = [UIColor grayColor];
birthday.text = [dateFormatter stringFromDate:p.Birth];
Soluzione
Se hai bisogno di anni a quattro cifre, imposta dateformat:
usando il formato esatto che desideri. Il rovescio della medaglia è che si perde la formattazione automatica delle impostazioni internazionali.
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
...
birthday.text = [dateFormatter stringFromDate:p.Birth];
Se hai bisogno di localizzazione, puoi provare a modificare il formato della data dello stile breve.
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease ];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
if ([[dateFormatter dateFormat] rangeOfString:@"yyyy"].location == NSNotFound) {
NSString *fourDigitYearFormat = [[dateFormatter dateFormat] stringByReplacingOccurrencesOfString:@"yy" withString:@"yyyy"];
[dateFormatter setDateFormat:fourDigitYearFormat];
}
Aggiornato con Bugfix da Max Macleod
Altri suggerimenti
Nel caso in cui qualcuno si imbattesse qui mentre lo facevo perché avevo bisogno di una cosa del genere con un anno a 2 cifre in un formato data localizzato e questa è una domanda molto vecchia, ecco la mia soluzione:
NSLocale* curLocale = [NSLocale currentLocale];
NSString* dateFmt = [NSDateFormatter dateFormatFromTemplate: @"ddMMyyyy"
options: 0
locale: curLocale];
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:dateFmt];
NSString* retText = [formatter stringFromDate:refDate];
return retText;
Forse qualcuno potrebbe usarlo qualche volta ...
Questo è un vecchio thread, ma poiché stavo cercando di fare ciò che Dave voleva e non poteva e perché nessuna delle risposte fornite era corretta, ecco la soluzione (nel caso in cui qualcuno volesse mai la stessa cosa):
NSString *FormatString = [NSDateFormatter dateFormatFromTemplate:@"MM/dd/yyyy HH:mm" options:0 locale:[NSLocale currentLocale]];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:FormatString];
//Parse date here with the formater (like [formatter stringFromDate:date])
[formatter release];
kCFDateFormatterShortStyle Specifica uno stile breve, in genere solo numerico, come & # 8220; 23/11/37 & # 8221; oppure & # 8220; 3: 30pm & # 8221 ;.
Disponibile in Mac OS X v10.3 e versioni successive.
Dichiarato in CFDateFormatter.h.