【发布时间】:2018-08-10 15:23:35
【问题描述】:
如何在 Objective C 中将字符串转换为日期。
我尝试了以下但没有弄明白。
NSString *str = @"3/2/2018 11:44:32 AM";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"MM/d/yyyy h:mm:ss a"];
NSDate *resultDate = [[NSDate alloc] init];
resultDate = [df dateFromString:str];
NSLog(@"result date: %@", resultDate);
result date: 2018-03-02 06:14:32 +0000 ,but i need to get it as
result date: 3/2/2018 11:44:32 AM
【问题讨论】:
-
你期望的 OP 是什么
-
结果日期:3/2/2018 11:44:32 AM
-
NSDate 总是不返回确切的 OP
-
如果您使用之前定义的 DateFormatter,您可以看到这很好
NSLog(@"result date: %@", [df stringFromDate: resultDate]);
标签: ios objective-c iphone nsdate xcode9