Convertir une NSString en NSDate
Question
j'ai un NSString
comme ça 05/09/2015
alors je veux le convertir dans ce format 2014-07-28 18:30:00 +0000
cette deuxième date était celle que j'obtiens de la date du système.Cette date peut être bien placée sur Tapku
calandre.mais ma chaîne est la première.Je souhaite convertir cette première date comme le format de date que j'obtiens de la date système.Comment puis je faire ça.
S'il vous plaît aidez-moi.Merci
La solution
NSString vers NSDate
NSString *dateString = @"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// voila!
dateFromString = [dateFormatter dateFromString:dateString];
NSDateFormatter *otherFormatter = [[NSDateFormatter alloc] init];
[otherFormatter setDateFormat:@"yyyy-MM-dd"];
//[otherFormatter setTimeStyle:NSDateFormatterMediumStyle];
NSString *resultString = [otherFormatter stringFromDate:dateFromString];
NSLog(@"%@", resultString);
Et puis vous pouvez le reconvertir en NSDate ou le conserver en tant que NSString.
Regarde ça: Cliquez ici
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow