所以你设置的格式是 [df setDateFormat:@"HH:mm, MMMM d, yyyy"];
字符串 myString 是 @"2012-11-07 15:22:06"
=> 不匹配
---所以我收集到阅读您对其他答案的评论:
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
NSString *myString = @"2012-11-07 15:22:06"; //yyyy-MM-dd HH:mm:ss
//convert to date
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-DD HH:mm:ss"];
NSDate *myDate = [df dateFromString:myString];
NSLog(@"%@", myDate); //2012-01-07 14:22:06 +0000
//output it in natural language
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[df setTimeStyle:NSDateFormatterNoStyle];
[df setDateStyle:NSDateFormatterMediumStyle];
[df setLocale:usLocale];
NSLog(@"%@", [df stringFromDate:myDate]); //Jan 7, 2012
}
}