简体   繁体   中英

NSDateFormatter dateFromString always returns (null)

I am trying to use NSDateFormatter's dateFromString but it always returns (null)

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSLog(@"%@", [dateFormatter dateFromString:[[myArray objectAtIndex:x] objectForKey:@"finish"]]);

The date in question is this: 2017-05-15 5:00:00 PM

But it always returns (null)

I have tried adding these:

[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setFormatterBehavior:NSDateFormatterBehaviorDefault];

But still returns null.

Please Help!

You dateFormat is wrong. You need to use:

[dateFormatter setDateFormat:@"yyyy-MM-dd h:mm:ss a"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM