Je suis sûr que je manque juste quelque chose. Mais j'ai googlé cela pendant des jours en essayant de trouver comment montrer une année à 4 chiffres lors de l'affichage d'un NSDate avec un style de NSDateFormatterShortStyle. S'il vous plaît laissez-moi savoir si vous avez une solution. Voici le code que j'utilise maintenant.iphone shortdate avec l'année à 4 chiffres
[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];
merci mais j'ai besoin localisée – Dave
pour localiser, ajouter la ligne dateFormatter.locale = [NSLocale autoupdatingCurrentLocale]; –
En fait, vous aurez également besoin d'une vérification dans le cas où le format spécifique au lieu est déjà aaaa, par ex. if ([formatString rangeOfString: @ "aaaa"]. location == NSNotFound) { NSString * fourDigitYearFormat = [[dateFormatter dateFormat] stringByReplacingOccurrencesOfString: @ "yy" withString: @ "aaaa"]; [dateFormatter setDateFormat: fourDigitYearFormat]; } –