Question

J'ai une chaîne comme ça :

NSString *string = @"2012-06-14T00:00:00+03:00";

J'ai essayé ceci et j'ai obtenu "2012-06-14 21:00:00 GMT".Mais j'ai besoin de "14.06.2012".Est-il possible?

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";

NSDate *date;
NSError *error;
[formatter getObjectValue:&date forString:string range:nil error:&error];
Était-ce utile?

La solution

Vous devrez en créer un deuxième NSDateFormatter pour le format souhaité (dans ce cas "jj.MM.aaaa"), et utilisez son stringFromDate: méthodes sur votre objet date pour obtenir une chaîne du format souhaité.Essayez d'ajouter ce code après votre code existant.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"dd.MM.yyyy";
NSString *result = [formatter stringFromDate:date];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top