iPhone:使用NSDateFormatter将字符串解析为NSDate

时间:2012-06-06 01:56:00

标签: cocoa nsdate nsdateformatter

  

可能重复:
  Converting NSString to NSDate (and back again)

我有一个日期和时间的NSString表示:6/06/2012 5:55:15 PM

它来自外部来源,我无法编辑它。如果可能,我希望使用NSDateFormatter将其转换为NSDate对象。我尝试使用下面的代码,但我无法快速,结果日期对象为空,有没有人有任何想法?我猜我正在留下一些重要的东西来处理AM / PM。

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd/MM/yyyy HH:mm:ss"];
NSDate *currentDate = [formatter dateFromString:job.jobDate];

NSLog(@"currentDate = %@",currentDate); // is null

1 个答案:

答案 0 :(得分:0)

您的日期字符串中包含“PM”。格式说明符为a

[formatter setDateFormat:@"dd/MM/yyyy HH:mm:ss a"];