从NSString获取月份,返回null值

时间:2013-12-17 11:45:49

标签: ios date nsdateformatter

我很久以来就陷入困境

NSString *time = "2013/12/17 06:38:09 AM EST";是一个字符串,我希望从中获取month

 NSDateFormatter *df = [[NSDateFormatter alloc] init];
 [df setDateFormat:@"yyyy/MM/dd hh:mm a"];
 NSDate* b = [df dateFromString:time];
 [df setDateFormat:@"MM"];
 NSString* month = [df stringFromDate:b];
 NSLog(@"month = %@", month);

1 个答案:

答案 0 :(得分:3)

您没有正确格式化日期,忘记了秒和时区

[df setDateFormat:@"yyyy/MM/dd hh:mm:ss a zzz"]