Convertire un NSString in un NSDate
Domanda
Ho un NSString
come questo 05/09/2015
, quindi voglio convertirlo in questo formato 2014-07-28 18:30:00 +0000
Quella seconda data è stata quella che ottengo dalla data di sistema.Quella data può essere posizionata bene sul calendario Tapku
.Ma la mia stringa è la prima.Voglio convertire quella prima data come come il formato della data che sto ottenendo dalla data di sistema.Come posso farlo.
per favore aiutami.Grazie
Soluzione
nsstring a 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);
.
E poi puoi convertirlo su un NSDate o mantenere un NSString.
Vedi questo: clicca qui
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow