您必须设置年份,否则日期将显示为空。您可以使用当前年份并将您的字符串分成月份和日期,然后像这样"dd-MM-yyyy"
或您想要的任何其他方式进行设置。
NSString *string = @"16/10 23:59:49";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd"];
NSArray *arrayDateData = [string componentsSeparatedByString:@"/"];
NSString *dayStr =[arrayDateData objectAtIndex:0];
NSString *string1 =[arrayDateData objectAtIndex:1];
NSString *monStr = [[string1 componentsSeparatedByString:@" "] objectAtIndex:0];
NSString *timeString;
timeString=[NSString stringWithFormat:@"%d-%@-%@",2012,monStr,dayStr];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString=[df dateFromString:timeString];
NSLog(@"date from string: %@",dateFromString);
我想这会对你有所帮助。